Home / Professional hosting / How to Automate Backups with Rsync

How to Automate Backups with Rsync

Backing up your website is one of the most important things you can do for it. For all of the security measures and updates you make, a simple thunderstorm could wipe away all of your work. But with backups, particularly remote backups, you can always have insurance for your server and all of its data.

Several backup systems exist, some free and others quite costly. One of the most reliable free systems available is one that you can setup on your own server without much effort or cost. Rsync is a free and open source incremental file transfer tool that many system administrators depend on for remote backups. Used in conjunction with cron, you can automate the backup process.

Basic Backup

You can use Rsync to perform a one-time backup of any file or directory with a single command string. For example, to backup a directory called “plants” on your local server to a remote server called “backupserver”, you would issue a command like this:

rsync -avz /path/to/plants username@backupserver:/path/to/backup

The “username” is the normal SSH username that you would use to login to the backup server. After pressing Enter, it will prompt you for the SSH password for that account.

Details:

  • The “a” option tells rsync to create an archive of the backed up files.
  • The “v” option tells rsync to be verbose, providing output of its progress
  • The “z” option will compress the archive to make it take up less space on the backup server.
  • The final backup file should be in tar.gz format.

Backup Automation

In order to make your backups automated, you need to do two things:

  • Create an SSH key for password-less login. You can learn how to do that on this site.
  • Create a script for crontab that tells rsync to run automatically at the times you specify.

Cron is a task automation system that you can use for just about anything you want to run automatically in the background. It requires no user interaction once your commands have been entered into /etc/crontab and the crond service is running.

Using your preferred text editor, create a file called “mybackup” (or whatever you want to name it) and make the contents look something like this:

#!/bin/sh

# This is an rsync script to run automated backups of my server

rsync -avz /path/to/plants username@backupserver:/path/to/backup

Save the file and then make it executable:

chmod a+x mybackup

You can then either manually enter it into crontab or drop it into one of the cron directories (if available), such as cron.daily, cron.hourly, cron.weekly, or cron.monthly. You may even want to create separate instances of the script that copy into separate directories on the backup server: one for daily, one for weekly, and one for monthly.

Once you finish setting up the cron job, your backups should run on schedule automatically with no further intervention on your part. You should, however, periodically check your backups to verify that they have been archived, compressed, and copied intact.

Rsync is free and available on most servers. Some web hosts may also include control panel tools for rsync backup and/or cron job creation. For more information about Rsync, see the online documentation.

About the author: Tavis J. Hampton is a Linux system administrator and writer for TavisOnline.com.

About sfilippi

Simone Filippi è un SEO italiano, specializzato nella gestione e ottimizzazione delle strategie web marketing di siti web per i motori di ricerca.

Check Also

Xen Virtual Servers – Why Favor the Virtual Option?

When it comes to the online world and the internet of the 21st century in general, …