LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   Backup script (http://www.linuxquestions.org/questions/linux-software-2/backup-script-730572/)

camry 06-04-2009 12:56 AM

Backup script
 
Hi all
I want to make a simple backup script to compress my directories under parent directory "servers" and make the backup with there names.tar.gz and then remove the old data, I make the following just for bigining:
-----------
dires=`ls servers`
for i in $dires
do tar -czf $i.tar.qz $i
done
-----------
But It doesn't work , can u help

minimol 06-04-2009 01:51 AM

Try this

dires=`ls servers`
for i in $dires
do tar -czf ${i}.tar.gz $i
done

to debug scripts use sh -x <script_name>

i92guboj 06-04-2009 03:20 AM

Quote:

Originally Posted by camry (Post 3562539)
But It doesn't work

That's an incredible amount of info you give there :p

Which is what doesn't work? How do you use it? What results do you get? What did you expect it to do? Any error message? Please, info. Otherwise we can only guess which is what "doesn't work".

Just a note, when dealing with dir and file names use quotations around them. Otherwise if a name has spaces you are screwed.

Another note, I usually disregard the usage of the "ls" command for this purpose. It creates more trouble than it's worth. If you want to do so, use find with -exec instead.

Code:

find servers -exec tar -czf '{}'.tar.gz '{}' \;
If you are in bash you can just do this:

Code:

for i in servers/*
  do tar -czf "$i".tar.gz "$i"
done

Curly brackets, as someone suggested, won't harm. But they are not needed in this case since the dot is not a character that can be used in an identifier (variable name in this case).


All times are GMT -5. The time now is 12:16 AM.