I recently signed up for a VPS for my hosting instead of the usual shared host environment. I run Linux at home and am quite comfortable with Linux servers.
On this new VPS, a traditional full backup is going to cost me a lot in terms of bandwidth and the more frequently I backed up the more the bandwidth would cost so I researched a better way for me and this may help you.
rsync is a Linux tool that performs folder to folder synchronisation. The first time it is run the synchronisation copies everything. But the magic comes on subsequent backups - only the parts of changed files transfer! This means very much smaller backups each time.
The problem with rsync is it relies on understanding a few things that you may not have had the time or motivation to learn before.
In this article I am going to explore how I got my backups off the VPS server and onto a remote webserver. You can choose to back up just a few critical folders but I chose a full system backup from / excluding only /dev /proc & /sys as these caused me permission problems.
I chose rsync.net but there are other similar dedicated remote back up companies.
1) log in as root to your server and make sure rsync is installed with this command:
rsync --version
To prevent the need to enter a password on the remote server you need to exchange rsa keys to so each server can trust each other.
2) enter this command and follow the prompts.
ssh-keygen -t rsa
Accept the defaults and do not enter a password just leave it empty.
This will create an 'id_rsa.pub' key in your /root/.ssh folder
3) You need to now upload this key to your remote server. Use the following SCP command (SCP stands for Secure Copy)
scp ~/.ssh/id_rsa.pub username@remoteserver.com:.ssh/auth_keys
This exact line will depend on your remote server setting and the location of your remote authorized keys location. Your server admin will know.
4) After copying your key test your remote server login without your password:
ssh username@remoteserver.com ls
This should ls or list the remote files to show you have connected without a password.
The next step is to check your rsync command. rsync syntax is as follows:
rsync [options] [localfiles] [remoteserver]
A simple command to use would be:
rsync --dry-run -e ssh --verbose --progress --stats --compress --recursive --times --perms --links --delete --exclude "/sys" --exclude "/dev" --exclude "/proc" --exclude "*bak" /* user@remoteserver.com:backupfolder
Notice the --dry-run option to just test the transfer without actually sending files.
Note also the --delete option to delete any files in the remote location that no longer exist on the local server.
Finally note the -e ssh to ensure the transfer is actually secured.
Once you are happy the process is working simple remove the --dry-run
The first time you run this will take some time. I copied over 3gb and this took just 30 minutes. The second time I ran it took less than 1 minute!
Once you are happy your backups are transferring edit your crontab
crontab -e
And add your rsync command to the file
59 04 * * * rsync -e ssh --verbose --progress --stats --compress --recursive --times --perms --links --delete --exclude "/sys" --exclude "/dev" --exclude "/proc" --exclude "*bak" /* user@remoteserver.com:backupfolder
This will run the rsync for you automatically every morning at 04:59am.
The next step in my quest for a live secure backup is to synchronise this data to my home PC. To Linux is easy because rsync is built in but the challenge will be for a local Windows XP backup which you can read about in my article Easy rsync backups to your Windows XP machine.
Multiple Users?
If you want to rsync from more than one computer it is possible to upload multiple public keys however, you cannot just follow the above instructions over and over again, because each time you follow them, you will overwrite the
previous key.
For each new user replace the scp step above with this:
cat ~/.ssh/id_rsa.pub | ssh user@remoteserver.com 'dd of=.ssh/authorized_keys oflag=append conv=notrunc'
You can now repeat this process for each user.