Tutorials and How-tos/Backup with rsync and rsync.net

From BubbaWiki
Jump to navigation Jump to search

If you are serious about keeping data on your server safe, you should consider deploying an offsite backup solution. So if anything goes wrong, you still have all your data up in the cloud. One cloud-based backup solution is to use the good old rsync tool with the rsync.net backup service.

First off, you have to sign up for the service and obtain three important pieces of information: a host name, a user name, and a password (they will be emailed to you as soon as your account is activated and configured).

Next, you have to generate an SSH key and upload it to your rsync.net storage space. This way, you don't have to enter the password every time you perform backup. To generate an SSH key, run the following command on your server:

 ssh-keygen -t rsa

Accept the defaults, and do not enter any password when prompted. Upload then the generated key to rsync.net (replace USER and ch-s009.rsync.net with your actual user name and host name):

 scp ~/.ssh/id_rsa.pub USER@ch-s009.rsync.net:.ssh/authorized_keys

Once the key has been uploaded, you can use the rsync tool to back up your files to rsync.net. The command below, for example, backs up the pictures directory:

 rsync -avhe ssh --delete /home/storage/pictures/ USER@ch-s009.rsync.net:pictures

If you want the backup operation to run automatically on a regular basis, you can set up a cron job for that. But before you do that, you have to create a simple backup script. Create a new text file and enter the script:

rsync -avhe ssh --delete /home/storage/pictures/ USER@ch-s009.rsync.net:pictures

Save the file as rsync.sh in the home directory. Run then the crontab -e command and specify the following job:

 @hourly /home/USER/rsync.sh >> /home/USER/logs/rsync.log

This job will execute the rsync.sh script every hour and save the log in the /logs/rsync.log file.