Sep 28
Simple backup script with notification
So I recently had to come up with a simple backup solution that didn’t involve any fancy system and basically just gave one the opportunity to choose what directories to backup, where to back them up and notify if something goes wrong.
So I came up with a solution which, in my own view, could be better and more “clean”, however this is how it is right now. You’re welcome to take the script, change and redistribute it – it’s released under the BSD license.
You might notice it’s refering to a bunch of files – these files are the error messages sent in e-mails and need to be placed somewhere, with corresponding changes in the script. If they don’t, e-mail notification won’t work.
Right now it’s sending e-mails when it’s succesful in making a backup. Depending on how often you want your cron job to do a backup, it might be irritating to get that many e-mails, so all you need to do is basically just remove the last part so that it only notifies you if something goes wrong.. That will however create the possibility that the e-mail functionality of the server will stop working and you won’t notice.
But yeah.. Here’s the script + all the error-messages zipped for download: backup_script
I’ll be the first to admit it’s not optimal in any way.. it’s a quick solution and has a bunch of DRY (Don’t Repeat Yourself) violations… but hey.. it works and given it’s low complexity I don’t wanna prioritize cleaning it up that much – but you are more than welcome to do so!
Here’s the code copied into the post:
#!/bin/bash # Copyright (c) 2008, Michael Lind Mortensen # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # * Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # * Neither the name of Michael Lind Mortensen nor the # names of his contributors may be used to endorse or promote products # derived from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY MICHAEL LIND MORTENSEN ''AS IS'' AND ANY # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL MICHAEL LIND MORTENSEN BE LIABLE FOR ANY # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # Define settings DATA="/home/user1 /home/user2 /etc /var/www/virtual /var/log" #Location of backup files. DISC="/mnt/backup" # Mount location of backup disc DEVICE="/dev/sdb1" # Backup devicename MAILREC="info@example.com" # Define message recipients. Comma-separate for more recipients. SENDER="noreply@example.com" # Define sender e-mail for error-messaging. LOG="logfile.txt" # Location of the error logfile DBPASS="databasepassword" # The root password to the mysql server set $(date) ##################################################### # Define email functionality for error messaging # # # # Parameters: MESSAGE - E-mail content from file # # # ##################################################### function error_mailer { MESSAGE=$1 SUBJECT="Backup process encountered errors!" # Send the email using mail if mail -s "$SUBJECT" -r "$SENDER" "$MAILREC" < $MESSAGE then : else echo "$3-$2-$6-$4 - ERROR: Mail functionality not working!" >> $LOG fi } ##################################################### # Define email functionality for success messaging # # # # Parameters: MESSAGE - E-mail content from file # # # ##################################################### function success_mailer { MESSAGE=$1 SUBJECT="Backup created succesfully!" # Send the email using mail if mail -s "$SUBJECT" -r "$SENDER" "$MAILREC" < $MESSAGE then : else echo "$3-$2-$6-$4 - ERROR: Mail functionality not working!" >> $LOG fi } # Mount backup disc if mount $DEVICE $DISC then : else echo "$3-$2-$6-$4 - ERROR: error while mounting disc!" >> $LOG error_mailer "/etc/backup_inc/errors/mount_error.txt" fi # Perform daily full backup if tar cfz "$DISC/data/data_$3-$2-$6.tgz" $DATA then echo "Data saved successfully!" else echo "$3-$2-$6-$4 - ERROR: error while saving data!" >> $LOG error_mailer "/etc/backup_inc/errors/data_error.txt" fi # Create SQL dumps of databases if mysqldump -u root --password=$DBPASS --all-databases > "$DISC/database/db_$3-$2-$6.sql" then : else echo "$3-$2-$6-$4 - ERROR: error while making mysqldump!" >> $LOG error_mailer "/etc/backup_inc/errors/mysqldump_error.txt" fi if gzip "$DISC/database/db_$3-$2-$6.sql" then echo "MySQL databases saved and compressed succesfully!" else echo "$3-$2-$6-$4 - ERROR: error while saving mysql databases!" >> $LOG error_mailer "/etc/backup_inc/errors/db_error.txt" fi # Unmount backup disc if umount $DISC then success_mailer "/etc/backup_inc/success.txt" else echo "$3-$2-$6-$4 - ERROR: error while unmounting disc!" >> $LOG error_mailer "/etc/backup_inc/error/umount_error.txt" fi
The fast person will probably notice that Wordpress writes < and > as < and > .. that’s just the code plugin messing things up and I hope you can figure out how to fix that yourselves.. if not, the zipfile has the code without any odd layout errors.
All in all the code should be fairly self-explanatory.. if not, there are plenty of comments to help you use the script or change it to whatever you need.
Last but not least – Remember to add a cron job with the script.. If you don’t there won’t be much automatic backup about it.
That’s just about it.. Enjoy!

