Use Rsync To Backup Data
Rsync is a command line utility that is used to synchronize files between two computers over a network to synchronize files between two computers, or servers. Presently, I am using it as a way to backup data from one server to a backup server. My goal here is to show a way to set up a linux server to fully, and automatically backup another server.
I’m sure there are better ways to do this with more security. However given my particular application, a very small network, with two separate firewalls to the internet, and just two users, I felt it acceptable to take this approach. Each situation must be taken into account if this is an acceptable method.
Rsync command options that I use are as follows:
- -a: Archive mode
- -v: Verbose output
- -e ssh: Specify the remote shell as ssh
- -p Tells ssh what non-standard port to use (greater security)
- –delete: Removes any files that have been removed on the remote machine
- –progress: displays file progress
- machine1:/home: The remote machine name, then the directory to be backed up
- /machine1: The directory to place the backup
After the command line options the source and the target machine and/or directory are given.
The command I use for rsync is as follows:
rsync -av -e “ssh -p 1234″firstname.lastname@example.org:/home2/data/ /home2/data/
This example is being run from the backup server, and is told to connect as root to server with an IP of 192.168.2.201 and retreive everything in the /home2/data/ folder, and copy it to the /home2/data/ folder of the backup server. (its VERY important you use the trailing slash after the directory) It is to use a ssh connection on port 1234. When this executes, the remote computer (server) will then ask for a password. Enter the password for the remote computer’s root password.
The next step is to configure the two servers so the backup server will login automatically. This is done with “keys”. Keys are basically files that (in this case..) allow automatic access of one computer into another computer. Just like a locked door has a key, key files “open” the other computer to you.
Here is how you do it:
The first thing you neeed to do on both servers is to set up the proper file permissions for the folders /root/.ssh folder. This needs to be set to 700 with the following command: chmod 700 /root/.ssh.
Next, log on to the backup server
Generate a public and private key pair in the /root/.ssh folder with the following command:
ssh-keygen -t dsa -f /root/.ssh/id_dsa
ssh-keygen will prompt you for a passphrase. This is basically the password for your key. This is the password ssh-agent will use to authenticate to all your machines that have your public key. After you enter your passphrase (make sure you remember it!), there should be two files located in the /root/.ssh directory called: id_dsa and id_dsa.pub.