Quick and dirty Backups with rsync and the mail command
This easy script allows us to backup large directories from a remote machine using crontab and rysnc. I use it to backup my EC2 instances to my local network at home. I love rsync because it conserves bandwidth by only downloading recently added or changes files.
My crontab on my machine at home looks like this.
#backup domainx 0 0 1 * * /usr/bin/rsync -arv --delete domainx:/home1/domainx /backup-evalv-others 2>&1 | mail -s "Just ran monthly rsync from sporkit.com and grabbed domainx.com" youremaill@domain.com 0 0 1 * * /usr/bin/ssh -C domainx mysqldump -u domainx_theuser --password=asdf1234 domainx_thedb > /backup-evalv-others/_sql/domainx_thedb.sql 2>&1 | mail -s "Just ran monthly sql sync from sporkit.com and grabbed domainx.com sql" youremaill@domain.com #backup domainy 0 0 2 * * /usr/bin/rsync -arv --delete domainy:/home1/admin /backup-evalv-others 2>&1 | mail -s "Just ran monthly rsync from sporkit.com and grabbed element-distribution.com" youremaill@domain.com 0 0 2 * * /usr/bin/ssh -C domainy mysqldump -u admin_theuser --password=asdf1234 admin_thedb > /backup-evalv-others/_sql/admin_thedb.sql 2>&1 | mail -s "Just ran monthly sql sync from sporkit.com and grabbed domainy.com sql" youremaill@domain.com #backup evalv-prd 0 0 3 * * /usr/bin/rsync -arv --delete evalv-prd:/evalv-data /backup-evalv-prd 2>&1 | mail -s "Just ran monthly rsync from sporkit.com and grabbed evalv-prd" youremaill@domain.com 0 0 3 * * /usr/bin/ssh -C evalv-prd mysqldump -u root --password=asdf1234 --all-databases > /backup-evalv-prd/_sql/all-databases.sql 2>&1 | mail -s "Just ran monthly sql sync from sporkit.com and grabbed evalv-prd sql" youremaill@domain.com
As you can see I've got three domains I take a backup of every month. I'll go through each of them line by line.
0 0 1 * *
At min 0, hour 0, and day 1 of every month
/usr/bin/rsync -arv --delete domainx:/home1/domainx /backup-evalv-others
Run the sync command and -a "archive", -r "recursivly", and -v "verbosely" download from the remote destination. We also --delete any files that we have locally, that don't appear remotely anymore. Also notice domainx is an ssh config alias setup with keyless log in. /home1/domainx is the directory i want to back up, and /back-evalv-others is the directory I want to put it in.
2>&1 | mail -s "Just ran monthly rsync from sporkit.com and grabbed domainx.com" youremaill@domain.com
This pipe "2>&1 |" will redirect stdout and stderr to the mail command. The "mail -s" command allows me to specify the a subject for the email and of course my email address is at the end. The mail I get will contain information about how the process I went. It usually looks something like this and include any errors rsync might encounter.
domainx/mail/.Drafts/new/
domainx/mail/.Drafts/tmp/
domainx/mail/.Sent/
domainx/mail/.Sent/cur/
domainx/mail/.Sent/new/
domainx/mail/.Sent/tmp/
domainx/mail/.Trash/
domainx/mail/.Trash/cur/
domainx/mail/.Trash/new/
domainx/mail/.Trash/tmp/
domainx/mail/.spam/
domainx/mail/.spam/maildirsize
domainx/mail/.spam/cur/
domainx/mail/.spam/new/
domainx/mail/.spam/new/1374433444.H999927P25256.gator3083.hostgator.com,S=5664
domainx/mail/.spam/new/1381240675.H99183P24947.gator3083.hostgator.com,S=5602
domainx/mail/.spam/new/1381241081.H813768P10875.gator3083.hostgator.com,S=5627
domainx/mail/.spam/new/1381241503.H198043P31112.gator3083.hostgator.com,S=5642
domainx/mail/.spam/new/1381241508.H895709P32094.gator3083.hostgator.com,S=5621
domainx/mail/.spam/new/1381241731.H295764P25693.gator3083.hostgator.com,S=5571
domainx/mail/.spam/new/1381242569.H800325P30314.gator3083.hostgator.com,S=5633