views:

50

answers:

1

Hi, my server is using CentOS 5.5 (which is almost Red Hat Linux).

I want to backup a set of pictures into time-stamped files. This code would work:


z_cmd1=$(tar cvzf /home/user1/public_ftp/misc/pics_20100925_142230.tar.gz /home/user1/public_html/misc/_pics_var/F???????.jpg)
echo "tar output =[${z_cmd1}]"

but of course I want the time stamp to be automatic.

The following code does not work. Somehow, the third line (the one with the tar) fails. 'tar' does something, but it does not create any file at the expected destination folder. Why?


z_fname=$(date +"/home/user1/public_ftp/misc/pics_%Y%m%d_%H%M%S.tar.gz")
echo "File name =[${z_fname}]"
z_cmd1=$(tar cvzf ${z_fname} /home/user1/public_html/misc/_pics_var/F???????.jpg)
echo "tar output =[${z_cmd1}]"

Thank you.

A: 

Try this something like this:

mydate=`date +"%m-%d-%Y"`;
input="/home/user1/public_html/misc/_pics_var/F???????.jpg";
output="/home/user1/public_ftp/misc/pics_$mydate.tar.gz";
tar cvzf $output $input && echo "$output created succesfully!";
if [ ! $? == 0 ]; then echo "$output failed"; fi;

We create a date as variable. We create the input and output variables, using the date variable in the output variable. Then execute the command and check if it executed without errors.

CodeReaper
The backticks around the `tar` command shouldn't be there.
Dennis Williamson
You are right, I have corrected it.
CodeReaper