You are on page 1of 5

Mirror A Web Site With rsync

1> Main server: server1.example.com (server1) - IP address: 203.197.32.159


Mirror/backup server: mirror.example.com (mirror) - IP address: 192.168.0.2
The web site that is to be mirrored is in /var/www/Project on
server1.example.com.

2> server1/mirror:
yum install rsync

3> server1:
useradd -d /home/rsync -m -s /bin/bash rsync
passwd rsync

4> mirror:
rsync -avz -e ssh rsync@203.197.32.159:/var/www/Project
/samba1/LiveBackup/Baybuy

You should see something like this. Answer with yes:

The authenticity of host 'server1.example.com (192.168.0.100)' can't be


established.
RSA key fingerprint is 32:e5:79:8e:5f:5a:25:a9:f1:0d:ef:be:5b:a6:a6:23.
Are you sure you want to continue connecting (yes/no)?

<-- yes

password for rsync user:

5> mirror:

mkdir /root/rsync
ssh-keygen -t dsa -b 2048 -f /root/rsync/mirror-rsync-key

Generating public/private dsa key pair.


Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/rsync/mirror-rsync-key.
Your public key has been saved in /root/rsync/mirror-rsync-key.pub.
The key fingerprint is:
89:92:64:49:18:e2:e0:03:ac:c3:ac:94:37:34:97:f0 root@sandbox

It is important that you do not enter a passphrase otherwise the mirroring will
not work without human interaction so simply hit enter!

6> mirror:

scp /root/rsync/mirror-rsync-key.pub rsync@203.197.32.159:/home/rsync/

7> server1:

(Please do this as user (i.e. rsync))


mkdir ~/.ssh
chmod 700 ~/.ssh
mv ~/mirror-rsync-key.pub ~/.ssh/
cd ~/.ssh
touch authorized_keys
chmod 600 authorized_keys
cat mirror-rsync-key.pub >> authorized_keys

By doing this, we have appended the contents of mirror-rsync-key.pub to the file


/home/someuser/.ssh/authorized_keys. /home/someuser/.ssh/authorized_keys should
look similar to this:

8> server1:

(Still as someuser(i.e.rsync))
vi /home/rsync/.ssh/authorized_keys

O/p Looks like this:

ssh-dss AAAAB3NzaC1kc3MAAA[...]lSUom root@mirror

Now we want to allow connections only from mirror.example.com, and the connecting
user should be allowed to use only rsync, so we add

command="/home/someuser(rsync)/rsync/checkrsync",from="192.168.0.2",no-port-
forwarding,no-X11-forwarding,no-pty

9> server1:

(Still as someuser (i.e. rsync))

vi /home/rsync/.ssh/authorized_keys

10> server1:

(Still as someuser(i.e. rsync))

vi /home/rsync/.ssh/authorized_keys

O/P should be:

command="/home/someuser/rsync/checkrsync",from="mirror.example.com",no-port-
forwarding,no-X11-forwarding,no-pty ssh-dss AAAAB3NzaC1kc3MAAA[...]lSUom root@
mirror

It is important that you use a FQDN like mirror.example.com instead of an IP


address after from=, otherwise the automated mirroring will not work!

Now we create the script /home/someuser/rsync/checkrsync that rejects all commands


except rsync.

11> server1:

(We still do this as someuser (i.e. rsync))

mkdir ~/rsync
vi ~/rsync/checkrsync

#!/bin/sh

case "$SSH_ORIGINAL_COMMAND" in
*\&*)
echo "Rejected"
;;
*\(*)
echo "Rejected"
;;
*\{*)
echo "Rejected"
;;
*\;*)
echo "Rejected"
;;
*\<*)
echo "Rejected"
;;
*\`*)
echo "Rejected"
;;
rsync\ --server*)
$SSH_ORIGINAL_COMMAND
;;
*)
echo "Rejected"
;;
esac

chmod 700 ~/rsync/checkrsync

12> Test rsync On mirror:

Now we must test on mirror.example.com if we can mirror server1.example.com


without being prompted for someuser's password. We do this:

mirror:

(We do this as root!)

rsync -avz --delete --exclude=**/stats --exclude=**/error --


exclude=**/files/pictures -e "ssh -p222 -i /root/rsync/mirror-rsync-key"
rsync@203.197.32.159:/var/www/Project /samba1/LiveBackup/Baybuy

rsync -avz --delete --exclude=**/stats --exclude=**/error --


exclude=**/files/pictures -e "ssh -p222 -i /root/rsync/mirror-rsync-key"
rsync@203.197.32.159:/home/mes_admin/backup/database /samba1/LiveBackup/Baybuy

You should now see that the mirroring takes place:

receiving file list ... done

sent 71 bytes received 643 bytes 476.00 bytes/sec


total size is 64657 speedup is 90.56

13> mirror:

(We do this as root!)

crontab -e
and create a cron job like this:

30 20 * * 0 /usr/bin/rsync -azq --delete --exclude=**/stats --exclude=**/error --


exclude=**/files/pictures -e "ssh -p222 -i /root/rsync/mirror-rsync-key"
rsync@203.197.32.159:/var/www/Project /samba1/LiveBackup/Baybuy/sunday

0 20 * * 0 /usr/bin/rsync -azq --delete --exclude=**/stats --exclude=**/error --


exclude=**/files/pictures -e "ssh -p222 -i /root/rsync/mirror-rsync-key"
rsync@203.197.32.159:/home/rsync/backup/database /samba1/LiveBackup/Baybuy/sunday

30 20 * * 1 /usr/bin/rsync -azq --delete --exclude=**/stats --exclude=**/error --


exclude=**/files/pictures -e "ssh -p222 -i /root/rsync/mirror-rsync-key"
rsync@203.197.32.159:/var/www/Project /samba1/LiveBackup/Baybuy/monday

0 20 * * 1 /usr/bin/rsync -azq --delete --exclude=**/stats --exclude=**/error --


exclude=**/files/pictures -e "ssh -p222 -i /root/rsync/mirror-rsync-key"
rsync@203.197.32.159:/home/rsync/backup/database /samba1/LiveBackup/Baybuy/monday

30 20 * * 2 /usr/bin/rsync -azq --delete --exclude=**/stats --exclude=**/error --


exclude=**/files/pictures -e "ssh -p222 -i /root/rsync/mirror-rsync-key"
rsync@203.197.32.159:/var/www/Project /samba1/LiveBackup/Baybuy/tuesday

0 20 * * 2 /usr/bin/rsync -azq --delete --exclude=**/stats --exclude=**/error --


exclude=**/files/pictures -e "ssh -p222 -i /root/rsync/mirror-rsync-key"
rsync@203.197.32.159:/home/rsync/backup/database
/samba1/LiveBackup/Baybuy/tuesday

30 20 * * 3 /usr/bin/rsync -azq --delete --exclude=**/stats --exclude=**/error --


exclude=**/files/pictures -e "ssh -p222 -i /root/rsync/mirror-rsync-key"
rsync@203.197.32.159:/var/www/Project /samba1/LiveBackup/Baybuy/wednesday

0 20 * * 3 /usr/bin/rsync -azq --delete --exclude=**/stats --exclude=**/error --


exclude=**/files/pictures -e "ssh -p222 -i /root/rsync/mirror-rsync-key"
rsync@203.197.32.159:/home/rsync/backup/database
/samba1/LiveBackup/Baybuy/wednesday

30 20 * * 4 /usr/bin/rsync -azq --delete --exclude=**/stats --exclude=**/error --


exclude=**/files/pictures -e "ssh -p222 -i /root/rsync/mirror-rsync-key"
rsync@203.197.32.159:/var/www/Project /samba1/LiveBackup/Baybuy/thursday

0 20 * * 4 /usr/bin/rsync -azq --delete --exclude=**/stats --exclude=**/error --


exclude=**/files/pictures -e "ssh -p222 -i /root/rsync/mirror-rsync-key"
rsync@203.197.32.159:/home/rsync/backup/database
/samba1/LiveBackup/Baybuy/thursday

30 20 * * 5 /usr/bin/rsync -azq --delete --exclude=**/stats --exclude=**/error --


exclude=**/files/pictures -e "ssh -p222 -i /root/rsync/mirror-rsync-key"
rsync@203.197.32.159:/var/www/Project /samba1/LiveBackup/Baybuy/friday

0 20 * * 5 /usr/bin/rsync -azq --delete --exclude=**/stats --exclude=**/error --


exclude=**/files/pictures -e "ssh -p222 -i /root/rsync/mirror-rsync-key"
rsync@203.197.32.159:/home/rsync/backup/database /samba1/LiveBackup/Baybuy/friday

30 20 * * 6 /usr/bin/rsync -azq --delete --exclude=**/stats --exclude=**/error --


exclude=**/files/pictures -e "ssh -p222 -i /root/rsync/mirror-rsync-key"
rsync@203.197.32.159:/var/www/Project /samba1/LiveBackup/Baybuy/saturday

0 20 * * 6 /usr/bin/rsync -azq --delete --exclude=**/stats --exclude=**/error --


exclude=**/files/pictures -e "ssh -p222 -i /root/rsync/mirror-rsync-key"
rsync@203.197.32.159:/home/mes_admin/backup/database
/samba1/LiveBackup/Baybuy/saturday

30 10 * * 1 sh /home/mes_admin/backup

This would run rsync every day at 8 and 8.30 pm

On server:
cd /home/rsync
vi dbbackup

mysqldump -u root -p********* rm > /home/rsync/backup/database/rm.sql


mysqldump -u root -p********* pm > /home/rsync/backup/database/pm.sql
mysqldump -u root -p********* ts > /home/rsync/backup/database/ts.sql
mysqldump -u root -p********* PromotionalPurchase >
/home/rsync/backup/database/PromotionalPurchase.sql
mysqldump -u root -p********* BayerBangalore >
/home/rsync/backup/database/BayerBangalore.sql
mysqldump -u root -p********* BayerHyderabad >
/home/rsync/backup/database/BayerHyderabad.sql
mysqldump -u root -p********* vendx_common_bayer >
/home/rsync/backup/database/vendx_common_bayer.sql
mysqldump -u root -p********* VendorAdmin_bayer >
/home/rsync/backup/database/VendorAdmin_bayer.sql
mysqldump -u root -p********* external_bayer >
/home/rsync/backup/database/external_bayer.sql
mysqldump -u root -p********* ebizchem_bayer >
/home/rsync/backup/database/ebizchem_bayer.sql
mysqldump -u root -p********* sms_common >
/home/rsync/backup/database/sms_common.sql
mysqldump -u root -p********* mds_desk_bayer >
/home/rsync/backup/database/mds_desk_bayer.sql
mysqldump -u root -p********* VendorApproval_bayer >
/home/rsync/backup/database/VendorApproval_bayer.sql

30 19 * * * sh /home/rsync/dbbackup

Follow this link if having any problems:


http://www.howtoforge.com/mirroring_with_rsync