[ubuntu-uk] Simple backup script

Tony Arnold tony.arnold at manchester.ac.uk
Fri Jun 3 17:09:39 UTC 2011


Chris,

Sorry if this has been mentioned before but have you tried sbackup?

Regards,
Tony.

On 03/06/11 14:49, Chris Rowson wrote:
> 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 <mailto: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
> 
> 

-- 
Tony Arnold,                        Tel: +44 (0) 161 275 6093
Head of IT Security,                Fax: +44 (0) 705 344 3082
University of Manchester,           Mob: +44 (0) 773 330 0039
Manchester M13 9PL.                 Email: tony.arnold at manchester.ac.uk



More information about the ubuntu-uk mailing list