For these past couple of weeks there have been a lot of changes on my blog. Starting from being suspended because of high traffic, moving to another hosting server and then optimizing the blog with caching to improve performance, and changing themes to optimize for Internet Explorer. Since then I have been doing a lot of backups before doing anything.
I made a script to backup all files and database and upload to HotFile, you can read that post here. And now I rent another hosting server to mirror my website. So if something happens with this host, I just repoint my nameserver and it would be up again. Yes, since I got suspended from the old host, I have become a backup freak.
So to mirror your website content, you can use rsync. This useful program can copy all your files over SSH between Linux or Unix servers.
Okay, now let’s prepare. I give the name CurrentUser, CurrentPassword and CurrentHost for the current server. And RemoteUser, RemotePassword and RemoteHost for the remote server.
Configure Current Server
1. Open your terminal and login to your SSH server.
2. Generate public/private key pairs to allow SSH connection without asking for a password.
$ ssh-keygen -t dsa -b 2048 -f /home/CurrentUser/cron/CurrentHost-rsync-key
Generating public/private dsa key pair.
Enter passphrase (empty for no passphrase): [press enter here]
Enter same passphrase again: [press enter here]
Your identification has been saved in /home/CurrentUser/cron/CurrentHost-rsync-key.
Your public key has been saved in /home/CurrentUser/cron/CurrentHost-rsync-key.pub.
The key fingerprint is:
2e:28:d9:ec:85:21:e7:ff:73:df:2e:07:78:f0:d0:a0 thisuser@thishost
3. Copy the .pub file to the RemoteHost.
$ scp /home/CurrentUser/cron/CurrentHost-rsync-key.pub RemoteUser@RemoteHost :/home/RemoteUser/
Now we are done with the configuration on the current host. Now we move to the remote host.
Configure RemoteServer
1. Connect to the remote host with SSH:
$ ssh RemoteUser@RemoteHost
RemoteUser@RemoteHost's password: [RemotePassword]
2. Move the CurrentHost-rsync-key.pub to the .ssh folder:
$ mv CurrentHost-rsync-key.pub .ssh/
cd .ssh/
3. Append CurrentHost-rsync-key.pub to authorized_keys in the .ssh folder:
cat CurrentHost-rsync-key.pub >> authorized_keys
4. Edit the authorized_keys with vim:
vim authorized_keys
5. Change from:
ssh-dss AAAAB3NzaC1kc3MAAAEBAKYJenaYvMG3nHwWxKwlWLjHb77CT2hXwmC8Ap+fG8wjlaY/9t4u
A+2qx9JNorgdrW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Mcy763RjYGkR/FkLJ611HWIA= CurrentUser@CurrentHost
Setup on the remote server is done. Now you can test it from your current server — try running this command:
$ rsync -avz -e "ssh -i /home/CurrentUser/cron/CurrentHost-rsync-key" RemoteUser@RemoteHost:/remote/dir/backup /this/dir/backup
Setup Cron
1. Create a bash file called backup-rsync.sh
2. Copy this script:
#!/bin/sh
RSYNC=/usr/bin/rsync
SSH=/usr/bin/ssh
KEY=/home/CurrentUser/cron/thishost-rsync-key
RUSER=RemoteUser
RHOST=RemoteHost
RPATH=/remote/dir
LPATH=/this/dir/
$RSYNC -az -e "$SSH -i $KEY" $RUSER@$RHOST:$RPATH $LPATH
3. Register to cron (I run it once a week):
crontab -e
0 0 * * 0 bash /path/to/backup-rsync.sh
To run on another schedule see the cron manual by using man crontab for more details.
Troubleshooting:
1. Check the permissions. On the current host, the private key file should be chmod 600. And on the remote host, the authorized_keys should be chmod 600.
2. Check the log files in the /var/log directory.
3. Verbose the rsync command to see where the command goes wrong:
$ rsync -avvvz -e "ssh -i /home/CurrentUser/cron/CurrentHost-rsync-key" RemoteUser@RemoteHost:/remote/dir/backup /this/dir/backup
