Wednesday, November 25, 2009

How to back up a system to another server using rsync and get an email with a result

1. Create a trust relationship between the system which needs to backed up and the system where the files will get backed up.

Replace ALPHA and BETA with the proper server names. (to add more server names, "sudo nano /etc/hosts" and add the ip and the name you wish to assign to each server)

ALPHA = server 1, where to log in from (on ALPHA, do this as user root)
BETA = server 2, destination where we log in

ALPHA: ssh-keygen -t rsa
BETA: mkdir .ssh
ALPHA: cat .ssh/id_rsa.pub | ssh user@BETA 'cat >> .ssh/authorized_keys'
BETA: chmod 644 .ssh/authorized_keys

2. As root, create a backup script

Replace "abc" with the name of your server which you are backing up.

Create the file: "nano /usr/bin/backupabc" and paste the script below, change:
- the backup server name, ex: mybackupserver
- the user id you use on the backup server, ex: my-user-id-on-backup-server
- the backup paths on the backup server, ex: /mnt/mybigdrive/backups/abc/
- your email address, ex: my.lovely.email@gmail.com (make sure you install mail: "sudo apt-get install mailutils" )


#!/bin/sh

LOG=/tmp/backupabc.log

START=$(date +%s)
echo "" > $LOG
echo "Start " >> $LOG
echo `date` >> $LOG

rsync --verbose --links --recursive --delete-during --human-readable --progress --itemize-changes /bin/ my-user-id-on-backup-server@mybackupserver:/mnt/mybigdrive/backups/abc/bin/ >> $LOG
rsync --verbose --links --recursive --delete-during --human-readable --progress --itemize-changes /boot/ my-user-id-on-backup-server@mybackupserver:/mnt/mybigdrive/backups/abc/boot/ >> $LOG
rsync --verbose --links --recursive --delete-during --human-readable --progress --itemize-changes /etc/ my-user-id-on-backup-server@mybackupserver:/mnt/mybigdrive/backups/abc/etc/ >> $LOG
rsync --verbose --links --recursive --delete-during --human-readable --progress --itemize-changes /home/ my-user-id-on-backup-server@mybackupserver:/mnt/mybigdrive/backups/abc/home/ >> $LOG
rsync --verbose --links --recursive --delete-during --human-readable --progress --itemize-changes /lib/ my-user-id-on-backup-server@mybackupserver:/mnt/mybigdrive/backups/abc/lib/ >> $LOG
rsync --verbose --links --recursive --delete-during --human-readable --progress --itemize-changes /opt/ my-user-id-on-backup-server@mybackupserver:/mnt/mybigdrive/backups/abc/opt/ >> $LOG
rsync --verbose --links --recursive --delete-during --human-readable --progress --itemize-changes /root/ my-user-id-on-backup-server@mybackupserver:/mnt/mybigdrive/backups/abc/root/ >> $LOG
rsync --verbose --links --recursive --delete-during --human-readable --progress --itemize-changes /sbin/ my-user-id-on-backup-server@mybackupserver:/mnt/mybigdrive/backups/abc/sbin/ >> $LOG
rsync --verbose --links --recursive --delete-during --human-readable --progress --itemize-changes /srv/ my-user-id-on-backup-server@mybackupserver:/mnt/mybigdrive/backups/abc/srv/ >> $LOG
rsync --verbose --links --recursive --delete-during --human-readable --progress --itemize-changes /usr/ my-user-id-on-backup-server@mybackupserver:/mnt/mybigdrive/backups/abc/usr/ >> $LOG
rsync --verbose --links --recursive --delete-during --human-readable --progress --itemize-changes /var/ my-user-id-on-backup-server@mybackupserver:/mnt/mybigdrive/backups/abc/var/ >> $LOG

END=$(date +%s)
DIFF=$(( $END - $START ))

echo "I have ran the /usr/bin/backupabc script and it took $DIFF seconds" >> $LOG
echo "\nEnd " >> $LOG
echo `date` >> $LOG

cat $LOG |  mail -s "mybackupserver: backed up abc" my.lovely.email@gmail.com



3. As root, run the script manually:
/usr/bin/backupabc
OR
add the script to the crontab to run every day at 10 pm (22 hrs) (as root):
crontab -e   (if prompted, use "nano" as the editor)
0 22 * * * /usr/bin/backupabc

To see the log while it's being built, open another shell and:
tail -f /tmp/backupabc

No comments:

Post a Comment