If you need to create an rsync backup over ssh, Jack Wallen walks you through the quick and easy process.
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)
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.
The first step is to generate a passwordless ssh key. Follow these steps.
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).
We now have to copy the id_rsa.pub key from the source to the target; this is managed with the help of ssh. Here’s what you do.
The ssh key will automatically copy into the necessary location on the target, and you’re ready to build a 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:
#!/bin/sh
rsync -e ‘ssh -p 22’ -avzp /home/USER1/TEST REMOTE_HOST_IP:/home/USER2/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.
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:
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.
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.