Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple backup script 1

Status
Not open for further replies.

fluid11

IS-IT--Management
Jan 22, 2002
1,627
US
I'm a newbie to scripting/programming and have a question on a simple backup script.

Basically, all this script does is tar a bunch of config files into a single tarball and then email root on whether or not it backed up all of the files. My problem is that it says it runs successfully everytime, even if I remove one of the files so that it can't back it up. It looks something like this...



#!/bin/bash

# Create a file with todays date inside
echo `date` > /home/linux_backups/date.txt

# Remove the old tar archive
rm -f /home/linux_backups/backup.tar

# Tar it up, while retaining permissions...
tar cpvf /home/linux_backups/backup.tar /home/linux_backups/date.txt /file1 /file2

# Change the permissions on the backup folder so that only root can access it
chmod -R 700 /home/linux_backups/
chown -R root.root /home/linux_backups/

# Did the script execute successfully?
echo $?
if [ `echo $?` = 0 ]
then
echo "The backup script on Voyager completed successfully" | mail root
else
echo "The backup script on Voyager failed" | mail root
fi



Thanks,
Chris
 
$? will give you the exit status of the last command executed, in your case echo $?.

Try something like ...

#!/bin/bash

# Create a file with todays date inside
echo `date` > /home/linux_backups/date.txt

# Remove the old tar archive
rm -f /home/linux_backups/backup.tar

# Tar it up, while retaining permissions...
tar cpvf /home/linux_backups/backup.tar /home/linux_backups/date.txt /file1 /file2

TAR_STAT=$?

# Change the permissions on the backup folder so that only root can access it
chmod -R 700 /home/linux_backups/
chown -R root.root /home/linux_backups/

# Did the script execute successfully?
if [ $TAR_STAT = 0 ]
then
echo "The backup script on Voyager completed successfully" | mail root
else
echo "The backup script on Voyager failed" | mail root
fi

Greg.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top