Pages

Wednesday, July 3, 2019

How to Use Rsync for automatic backup plan in Linux servers ?

The Rsync command  :

 

The rsync command is a ideal tool for copying and synchronizing files and directories to a remote location, while the crontab command is used to schedule jobs to be executed periodically. Combining these two commands, we can setup a light-weight and effective backup solution.

$ rsync -av Documents/* /tmp/documents

In the command above, the option:

-a – means archive mode

-v – means verbose, showing details of ongoing operations

By default, rsync only copies new or changed files from a source to destination, when I add a new file into my Documents directory, this is what happens after running the same command second time:

$ rsync -av Documents/* /tmp/documents

------------------------------------------------------------------------------------

Syncing Files From Local to Remote Linux

In the example below, I am copying files from my local machine to a remote sever with the IP address – 192.168.1.8. So as to only sync new files on the local machine, that do not exist on the remote machine, we can include the --ignore-existing option:

 

$ rsync -av --ignore-existing /backup_dhcp/ root@192.168.1.8:/home/backup/

-----------------------------------------------------------------------------------

Subsequently, to sync only updated or modified files on the remote machine that have changed on the local machine, we can perform a dry run before copying files as below:

$ rsync -av --dry-run --update  /backup_dhcp/ root@192.168.1.8:/home/backup/

$ rsync -av --update /backup_dhcp/ root@192.168.1.8:/home/backup/

 

$ rsync -av --update /backup_dhcp/ root@192.168.1.8:/home/backup/

 

 

 

 

 

 

The Process :

 


  • 1.)  Make sure rsync is installed on both Host A and Host B.  If it is not installed, install it using command:
yum install rsync.

  • 2.) Login to host B using root account (Crontab requires a root user permission, though theoretically it can be done using a non-root account, but not easy) ,  and create a directory to hold the backup data:

mkdir -p  /home/backup

  • 3.) Generate the SSH key pair :

ssh-keygen

Two files are generated in the /root/.ssh directory: id_rsa is a private key file, while id_rsa.pub is a public key file, which must to be copied to Host B:

scp /root/.ssh/id_rsa.pub root@192.168.1.8:/root/id_rsa.pub

  • 4.) Login to host B using root account, and attach the content in id_rsa.pub file to the authorized_keys file.  If /root/.ssh/authorized_keys file doesn’t exist in host A, execute the following commands to create it first:

 mkdir -p /root/.ssh
 chmod 700 /root/.ssh
 touch /root/.ssh/authorized_keys
 chmod 600 /root/.ssh/authorized_keys

To attach the public key generated in host B to authorized_keys file:
cat /root/id_rsa.pub >> /root/.ssh/authorized_keys

Now we can use scp or rsync to transfer data from host A to host B without password required.

  
  • 5.)  Modify /etc/crontab file to schedule the execution of backup script.  Add this line to the end of the crontab file:
$ crontab -e

0 2 * * * root bash /home/usr/backup.sh     # the script file backup.sh is scheduled to be executed every day at 2:

The content of the backup.sh script is something like this:

#!/bin/sh

/usr/bin/rsync -av --ignore-existing /backup_dhcp/ root@192.168.1.8:/home/backup/

This script is just a sample and you can modify it based on your need. 



No comments:

Post a Comment