How to set up auto rsync backups using ssh

If you need to create an rsync backup over ssh, Jack Wallen walks you through the quick and easy process.

Image: Jack Wallen

Linux has all the tools you need to create a simple, automated network backup. Even better, it's very easy to set up, and it will serve you without fail (as long as your network is up and running) until you no longer have a need for this backup.

How do you make this magic happen? With ease, my friend. I'll show you how.

SEE: Data Backup Policy (Tech Pro Research)

What you'll need

First and foremost, you're going to be doing this from and to Linux. It doesn't matter what distribution you're using, so long as it's Linux. Both source and target will need to have openssh-server installed. If you need to install openssh-server on either machine, you can do this from the GUI package manager, or run the installation from the command line.

For example, installing on Ubuntu would require the command:

sudo apt-get install openssh-server

You will also need to generate an ssh key. This is important: We will be generating a passwordless ssh key. Yes, this could be considered a security issue, but I assume this is being used on a secure network; if your network (or your source and target) isn't secure, you probably shouldn't be considering such an option as passwordless backups. However, if you have a need for an automated rysnc backup from one machine to another, here's how to do it.

Generate an ssh key

The first step is to generate a passwordless ssh key. Follow these steps.

  1. Open a terminal window.
  2. Issue the command ssh-keygen -t rsa.
  3. When prompted for a storage location, hit Enter on your keyboard.
  4. When prompted for a password, hit Enter on your keyboard.
  5. When prompted to verify your password, hit Enter on your keyboard.

The above steps will result in the generation of an ssh keypair. If you look in ~/.ssh, you'll find two keys with the following names (if you chose the defaults during the key generation process).

  • id_rsa

Copying the pub key

We now have to copy the key from the source to the target; this is managed with the help of ssh. Here's what you do.

  1. Open a terminal window.
  2. Issue the command ssh-copy-id -i ~/.ssh/ USERNAME@REMOTE_HOST_IP (USERNAME is the name of the user on the remote host, and REMOTE_HOST_IP is the IP address of the remote host).
  3. When prompted, enter the password for the remote host.

The ssh key will automatically copy into the necessary location on the target, and you're ready to build a backup script.

The backup script

We'll create a very simple backup script for this process. I'm going to assume the source folder to backup is ~/TEST and the location for the backup on the target is ~/BACKUPS/—you'll want to modify this as necessary. The simple script looks like this:

rsync -e 'ssh -p 22' -avzp /home/USER1/TEST REMOTE_HOST_IP:/home/USER2/BACKUPS

  • USER1 is the name of a user on the source machine
  • TEST is the directory to be backed up
  • REMOTE_HOST_IP is the actual IP address of the target machine
  • USER2 is the name of a user on the target machine
  • BACKUPS is the directory to house the backups

Save that file with the name rsync_script and then give it executable permissions with the command chmod +x rsync_script.

Before we set up the automation process, test the script with the command ./rsync_script. It should complete without errors, and the backup directory should be found on the target machine.

Automating the backup

We'll use cron to automate the backup of the script. To make this incredibly easy, you can drop your rsync_script into one of the following directories:

  • /etc/cron.hourly (for hourly runs)
  • /etc/cron.daily (for daily run)
  • /etc/cron.weekly (for weekly runs)
  • /etc/cron.monthly (for monthly runs)

If you need the backup script run at a specific time, you'll have to manually create a cron job by issuing the command crontab -e and then adding a line such as:

00 00 * * * /usr/local/rsync_script

The above crontab entry assumes you moved the rsync_script into /usr/local/ and that you want the script to run every day at midnight. Save the cron file (usually by hitting [Ctrl]+[x]) and it will start running the next time the clock strikes midnight.

As simple as it gets

This method of backing up with rsync over ssh is incredibly simple; you can make it far more complicated and even create scripts to run multiple backups over ssh.

Remember: This is a passwordless backup system, so if you're dealing with a network or systems that haven't been secured, you might want to look elsewhere. Otherwise, this is a very quick and easy solution for backing up data from one machine to another.

Also see

About Jack Wallen

Jack Wallen is an award-winning writer for TechRepublic and He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website

Editor's Picks

Free Newsletters, In your Inbox