Backup

Use rsync and SSHFS to create incremental backups!

Good system administrator always keeps their critical data backed up, their are various ways you can backup your data, but some times it can be a hectic job, how ever we can use rsync and sshfs to create incremental backups (and automate the backup using cron jobs).

What is SSHFS?

SSHFS can mount a remote machine on your backup server as just another folder, lets consider a use case:

  • Machine A (Backup server)
  • Machine B (Server to be backed up)

We can use SSHFS to mount machine B on machine A, and machine A will be able to access machine B’s data as it is actually mounted on machine A.

How can we install SSHFS

#on cent os

yum install sshfs

#on debian/ubuntu

sudo apt-get install sshfs

This will install SSHFS on your backup server (You need to install sshfs on machine A since its your backup server). Follow the steps below to backup your machine B on machine A.

We will only backup machine B’s /home directory, since it contains most of the important user data, how ever you can use the same method to even backup complete machine B, or some other directories as well.

Step 1: Mount machine B

Login to machine A’s ssh, and create a directory where we will mount the remote machine.

mkdir /mnt/serverB

This will create an empty directory for us to mount machine B, now run the following command to mount machine B on machine A.

sshfs [email protected]:/ /mnt/serverB

Please replace the IP with the actual IP of machine B, now machine B is mounted at “/mnt/serverB”, you can use  cd /mnt/serverB to move to this directory and see its contents using  ls . It will display the contents of machine B.

Step 2: Create backup directory

Now you need to create a directory on your machine A, where you can place the backup, use the mkdir command to create the backup directory.

mkdir ~/serverBData

We can use rsync to backup the data, for the first run rsync will backup the whole data, how ever sub-sequent runs will only backup the changed bytes.

Step 3: Backup data

Since we will only backup the /home directory, you can run the command below to backup home directory

rsync -r /mnt/serverB/home ~/serverBData

It will take some time depending on the data and network speed, after its executed successfully backup will be on the “~/serverBData” directory, you can move to this directory and verify the files as well.

Step 4: Set up cron

You can use cron job to automate this process, I would recommend to run cron every 24 hours. Run the command below

crontab -e

It will ask you for the editor to edit the cron job file, you can use the one that suits you, and then append this line at the end of the file

0 5 * * * rsync -r /mnt/serverB/home ~/serverBData

Save and close the file, now each increment of your backup will be performed at 5AM every day (Choose the time when you have the least load on machine B).

Leave a Reply

Your email address will not be published. Required fields are marked *