tags:

views:

45

answers:

4

Hi all. I've written a script that backs up my financial spreadsheet document to another hard drive and another computer. I have also set up my server with email to send cronjob messages to my email instead of system mail. In my script I can't figure out how to use if then to check date of backed up file and current date. Here is the script

#!/bin/bash
mount -t cifs //IP/Share /Drive/Directory -o username=username,password=password

cp /home/user/Desktop/finances10.ods /media/MediaConn/financesbackup/Daily\ Bac$
cp /home/user/Desktop/finances10.ods /Drive/Directory/FinancesBackup/Daily\ Backup/

umount /Drive/Directory
export i=`stat -c %y /media/MediaConn/financesbackup/Daily\ Backup/finances10.o$
export j=`date +%d`
if ["$i"="$j"]; then
 echo Your backup has completed successfully.  Please check the Daily Backup fo$
 echo This message is automated by the system and mail is not checked.
else
 echo Your backup has failed.  Please manually backup the financial file to the$
 echo drive.  This message is automated by the system and mail is not checked.
fi

Pretty simple script. The output is sent by email because it's a cronjob. If anyone could help I would greatly appreciate it. Thanks in advance

+3  A: 

Your code is all messed up in the post, but anyway... you should probably compare the output of 'stat -c %Y' (not %y) to the output of 'date +%s' (not %d).

But, even better, use something like md5sum or sha1sum to make sure the backed up file really matches the original.

vanza
+1  A: 

I would strongly recommend checking that each command in your script has succeeded. Otherwise the script will carry on blindly and (at best) exit with a success code or (at worst) do something completely unexpected.

Here's a decent tutorial on capturing and acting on exit codes.

Brian Agnew
+1  A: 

This line needs spaces around the brackets and equal sign:

if [ "$i" = "$j" ]; then

There's no need to export the variables in this context.

You should use the format codes that vanza suggested, since they correspond to the same format which is the number of seconds since the Unix epoch.

You should put quotes around the messages that you echo:

echo "some message"

When you pasted your code (apparently copied from nano), it got truncated. It might work better if you list it using less and copy it from there since less is likely to wrap lines rather than truncate them.

Dennis Williamson
A: 

Thanks for all of the input. Sorry, I did copy and paste from nano, lol, didn't realized it was truncated. All of your advise was very helpful. I was able to do what I wanted using the format I had but just putting the spaces between the brackets and equal sign. I've never used md5sum or sha1sum but will check it out. Thanks again for all your help, it's working great now!

spxxn