Update: Rsync Automated incremental backups with database support

3 min read

RSYNC is an open source tool for synchronizing files both locally and remotely across servers. It’s not only fast, but also smart — the tool utilizes a famous algorithm to detect the differences between two files or folders. This reduces the amount of data sent over the network by only transferring the pieces of files that changed.

We at Servercheap have saved you the time and created a script to help you automate your backups. This script will backup your files and databases to a remote server. In this tutorial we will show you how to customize the script to fit your exact needs.

First we need a backup server, which will hold our backups. For this example I have quickly deployed a clean centos 7 $2.99/mo openvz ssd vps. On our backup server the only thing we need to install is rsync by running:

For CentOS/Fedora:

yum -y install rsync

For Debian/Ubuntu:

apt -y install rsync

Now let’s jump to our production server and customize our backup script.

Note: All steps below are performed as root user.

First things first, let’s setup SSH keys on our production server and copy them over to our backup server. If you already have ssh keys setup, skip the first step:

Setup ssh keys

  1. Generate SSH key:

    [root@production ~]# ssh-keygen

    Press ENTER on each line.

    Generating public/private rsa key pair.
    Enter file in which to save the key (/root/.ssh/id_rsa):
    Created directory '/root/.ssh'.
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    Your identification has been saved in /root/.ssh/id_rsa.
    Your public key has been saved in /root/.ssh/id_rsa.pub.
    The key fingerprint is:
    SHA256:I6BN/BwVXSOXe/LuCxEIlwVKm4qValVmQtcvND9+Iv4 root@production
    The key's randomart image is:
    +---[RSA 2048]----+
    | .o X++==. |
    | . X **+.. |
    | + = +..+.. |
    | + O o . *.. |
    | . = = S o.= |
    | . . .. o.o |
    | . ..+ |
    | . .. |
    | .E.o. |

  2. Copy ssh key to our backup server:

    [root@production ~]# ssh-copy-id root@IP-OF-OUR-BACKUP-SERVER

    /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
    The authenticity of host ' IP-of-our-backup-server ( IP-of-our-backup-server )' can't be established.
    ECDSA key fingerprint is SHA256:Ld0ma1DffczaDp/LbTEeLOZGDSo4BU2c/Hm5hdZpk7A.
    ECDSA key fingerprint is MD5:c3:a4:e8:94:3d:0e:c4:3d:3e:5c:b4:02:4e:23:e4:ff.
    Are you sure you want to continue connecting (yes/no)? yes
    /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
    /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
    root@ IP-of-our-backup-server 's password:
    Number of key(s) added: 1
    Now try logging into the machine, with: "ssh 'root@ IP-of-our-backup-server '"
    and check to make sure that only the key(s) you wanted were added.

As SSH Keys are now setup, let’s download the script. As root run:

[root@production ~]  wget https://servercheap.net/timages/rsync-backup2.sh -O /root/rsync-backup2.sh

As the script in downloaded in your /root folder, let’s open it with our favorite text editor:

[root@production ~]# nano -w /root/rsync-backup2.sh

There are just a few configuration options that we need to take care of, so let’s go over them one by one.

First set of option has to with the destination backup server. The options are self explanatory:


Let’s replace with the actual ip address of our backup server. Our ssh username can be unchanged if we are using root, same as the ssd port of our backup server. Next options:

 FoldersToBackup='/var/www/html /usr/share/app/data'

With FoldersToBackup we specify the the folder(s) we want backed up. For multiple folders we separate them with a space. DestinationBackupFolder is the folder on our backup server where we want the backups to be copied.

NOTE: It is recommended that you create all destination folders on your backup server before hand.

It is important not to put a trailing slash at the end of the folder(s).

With the next 2 options we specify whether we want to receive an email after each time our script runs. The email contains information and statistics about the backup that has just been performed.


The next 4 options specify the username and password of your local mysql/mariadb server, the database(s) you would like to backup and the remove folder where you wish the database backup to be placed.


The last two options can be left unchanged. logFile option specifies the location of the log file that is emailed to you if notifyEmail is set to yes .It is important to know that the log file is only temporary and is deleted after each run. rsyncOptions are the rsync options passed to rsync. Change them at your own risk.

That’s it. All we have to do now is test the script by running:

[root@production ~]# chmod +x /root/rsync-backup2.sh
[root@production ~]# /root/rsync-backup2.sh

If everything worked, you should now have a brand new backup on your backup server.

NOTE: this script does an Incremental backup. What this means is that the script will only back up the files that have been changed since the last backup. This on large production systems can save tons of time and bandwidth.

Last step is to automate this via CRON. Let’s set it up so our script does an automatic backup once a week every Sunday. Open cron for edit by running:

[root@production ~]# crontab -e

And put at the bottom:

0 0 * * 0 /root/rsync-backup2.sh >/dev/null 2>&1

And you are all set. From now on the script will run every Sunday at midnight and automatically backup all files that have changed since the last backup.


We want to hear your thoughts about this. Is there anything in particular that you love about it? What can we do to improve our services and experience? Leave a comment below or open a ticket on our helpdesk and we’ll personally review all suggestions and feedback. 

About Servercheap.NET

Since it was founded in 2015, Servercheap has always strived to provide its clients with enterprise-level performance at an unbeatable cost. Servercheap offers a wide range of customizable hybrid and virtual private server hosting services. All Servercheap clients enjoy a 99.9% uptime SLA and 24/7 rapid response support team.
At Servercheap, our core directive has always been to provide our clients with the best services and infrastructure possible, whether you’re hosting a game server, a high-intensity database, a development environment, or anything in-between.
For more information, visit https://www.servercheap.net

Leave a Reply

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