Tuesday, July 10, 2012

RSYNC + Email Notify

Here is a friendly little walkthrough on how to back up data, log it and send an email notification.

Our goal is to accomplish the following:
1. Backing up your data to a Windows share.
(I do not show how to set up a share or mount that share. And I use rsync which will not be
covered here. Feel free to Google and then ask if you get stuck)

2. Updating a log of the time the backup occured.

3. Send an email to notify of successful backup.
(I used to use Perl but now use mutt...there are plenty of google pages for gmail configuration of mutt, so don't ask me)


========================================================================
First create the message of your body:
nano message

Type in what you want to get in your email, save and exit:
Data Backup Success!
========================================================================

Next create a bash script like so:
nano backups.sh

type the following, save and exit:
rsync -ra /your/files/to/be/backed/up /mnt/yourbackuplocation/
mutt -s "`date`" -- youremail@gmail.com < /location/of/message/
date >> /mnt/yourbackuplocation/backup_log

then make backups.sh executable:
chmod +x backups.sh

fyi backups.sh does the following:
-Syncs your files to a backup location you specify using the -r and -a tags which
-a, --archive               archive mode; equals -rlptgoD (no -H,-A,-X)
-r, --recursive             recurse into directories
more on rsync
-mutt emails you an email with the date and time to your email (mutt needs to be configured)
mutt and gmail
-appends the current date and time of script completion to file in your backup location 

========================================================================

Setup cron to run your back up script at a designated time:
crontab -e


Then set it up like so: (example sets backup for 5 am every day)
0 5 * * *  /bin/bash /location/of/backups.sh

 ========================================================================


The end result is that you have your data backed up daily, it keeps a log of it, and sends you an email daily letting you know it took place.


Thanks and have fun!

2 comments:

  1. Good info. I use the long options --verbose --progress --stats --compress --rsh=/usr/bin/ssh --recursive --times --perms --owner --group --links --exclude for backing up to a remote ssh server. Also, the --exclude flag is useful.

    ReplyDelete
    Replies
    1. Thanks for the advice...also I am creating a Blogger just for BASH if you want I can make you an admin so we can post scripts to it...I would like that very much.

      it is at http://bashbang.blogspot.com/

      Delete