[ubuntu-uk] Simple backup script
Chris Rowson
christopherrowson at gmail.com
Fri Jun 3 10:35:47 UTC 2011
Thanks so much for all of your suggestions guys.
I ended up with something like this which resulted in part from a need to
get my backups onto a Windows Server and to get an email when it happens (or
if it doesn't).
#!/bin/bash
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
#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
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/434821b9/attachment.html>
More information about the ubuntu-uk
mailing list