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