-   Linux - Software (
-   -   Backup script (

camry 06-04-2009 01: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
But It doesn't work , can u help

minimol 06-04-2009 02:51 AM

Try this

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

to debug scripts use sh -x <script_name>

i92guboj 06-04-2009 04:20 AM


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.


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


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

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 08:31 AM.