Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I am doing backup using

for a in /home/*
do
//do backup
done

I want to exclude some folders in /home . but for loop takes all folders i there.

is there any way so that i can specify exclude list at top which can be used in loop

share|improve this question

2 Answers 2

up vote 2 down vote accepted

You need some sort of evaluation in order to check what to exclude from the list of folders you get in the "for"; just using bash this could be (you can use whatever you feel comfortable to evaluate what to exclude (ie sed, grep, awk,...)):

exclusiondirs="/home/foo /home/bar" # list of folders to exclude from backup

for a in /home/*; do
if ! [[ "$exclusiondirs" =~ $a ]]; then
    ...do backup...
fi
done

However I would have use some other tools for backup such rsync which can exclude by itself, for example:

rsync -av /home/* --exclude /home/foo --exclude /home/var destination

Edited:

Since the bash regex is a bit tricky and difficult to implement in some circumstances I've replaced it with a more robust grep statement:

if ! echo "$exclusiondirs" | grep -q "\b$a\b" ; then

This grep statement will work in a home dir with names such as (where some dirs share common part names and include spaces within):

mkdir -p home/{foo,bar,foobar,a,b,c,a\ b
tree
`-- home
    |-- a
    |-- a b
    |-- b
    |-- bar
    |-- c
    |-- foo
    `-- foobar

~$ echo $exclusiondirs 
/home/foo /home/bar /home/a
~$ for a in /home/*; do if ! echo "$exclusiondirs" | grep -q "\b$a\b" ; then echo $a; fi; done
/home/a b
/home/b
/home/c
/home/foobar
share|improve this answer
    
thanks for that , i can use rsync but can i want separate users home dir backups in separate folders in compressed format like .tar.gz can i do that in rsync –  Mirror51 May 4 '11 at 9:34

Go to Skipping multiple files and folders section in this page.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.