[ubuntu-uk] Simple backup script
Chris Rowson
christopherrowson at gmail.com
Fri Jun 3 13:49:08 UTC 2011
I promise to stop spamming the list now! Final draft.
I know the email component should be put into a function but I'm a bad man
and didn't do it!
Just added an extra section that checks to make sure that tar didn't throw
an error code for some reason when it exited.
Chris
BACKUPDIR="/home/username/backup"
WHATTOBACKUP="/var/www"
SERVERNAME="servername"
BACKUPADMIN="email at domain.com"
MESSAGE="/tmp/message.txt"
if [ -d $BACKUPDIR -a -d $WHATTOBACKUP ] *#make sure the source & dest dirs
exist*
then
*#backup the directory*
/bin/tar -cpzf $BACKUPDIR/`date +%a"-"%d"-"%b"-"%Y"-"`backup.tar.gz
$WHATTOBACKUP
RESULT=$? *#grab the tar exit code*
if [ $RESULT -eq 0 ] *#tar will return non-zero if an error occurs.
If we have a 0 exit code continue*
then
* #then remove anything over 7 days old*
find $BACKUPDIR/*.tar.gz -mtime +7 -exec rm -f {} \;
* #And let us know what happened*
SUBJECT="Backup Completed"
TO=$BACKUPADMIN
echo "Backup of $SERVERNAME completed" >> $MESSAGE
echo "Result of backup" >> $MESSAGE
echo "`ls -alt` $BACKUPDIR" >> $MESSAGE
/usr/bin/mail -s "$SUBJECT" "$TO" < $MESSAGE
rm $MESSAGE
exit
else
* #send a mail if tar failed*
SUBJECT="Backup Failure"
TO=$BACKUPADMIN
echo "Backup of $SERVERNAME failed" >> $MESSAGE
echo "Unable to create tar archive. Tar exit code was
$RESULT" >> $MESSAGE
/usr/bin/mail -s "$SUBJECT" "$TO" < $MESSAGE
rm $MESSAGE
exit
fi
else
* #if backup dir does not exist, tell us*
SUBJECT="Backup Failure"
TO=$BACKUPADMIN
echo "Error backing up $SERVERNAME" >> $MESSAGE
echo "One of the following directories is missing: $BACKUPDIR
$WHATTOBACKUP " >> $MESSAGE
echo "Date: `date`" >> $MESSAGE
/usr/bin/mail -s "$SUBJECT" "$TO" < $MESSAGE
rm $MESSAGE
fi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/ubuntu-uk/attachments/20110603/2cae356a/attachment.html>
More information about the ubuntu-uk
mailing list