1

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.

3
  • You've got the v erbose flag set, what does it say? set -xv is often helpful in debugging shell scripts. Commented Sep 25, 2010 at 12:52
  • You say tar does something. What does it do? What error messages do you get? What is output when you echo ${z_cmd1}? Even though it doesn't appear to be necessary in this case, it's a good idea to quote variables that contain filenames. Commented Sep 25, 2010 at 14:56
  • The code 'works' for me - the output file name is generated plausibly, and then tar fails horribly because I don't have a /home directory on my machine, let alone the directories underneath it. Why are you capturing the output of tar like that? Why not just let it run? And you might want to capture the errors too. Commented Sep 25, 2010 at 16:38

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.