Skip to main content

VPS: Backing up Linux servers with Rsync

This guide was created with the following products:

(Details may vary with products from different providers but the main concepts remain the same)

Rsync:

Backups can be made with the tool Rsync. They can be copied on the local system itself to another directory/drive or to a remote system.

Copy data to another local directory or drive:

info

Attention: The first run can take much longer than the following runs, depending on the amount of data. This is because the first time Rsync syncs all data, from the second time on only changed data will be synchronized.

So then a incremental backup is created.

Step 1

Rsync can be installed with the following command:

apt install rsync

After it has been installed, it can be used directly.

Step 2

In this example, the Client folder under /home should be synchronized to the Backups folder under /home.

This can be done with the following command:

rsync -arz /home/Client /home/Backup

-a=Archiving, the attributes will be copied


-r=Recursive, subfolders are synchronized too


-z=Compression, depending on data quantity/data size is compressed

The folder was synchronized successfully

If a file etc. in the client folder is deleted now, it will remain in the backup folder. But since the files should always be 1:1 synchronous, the rsync command can easily be changed, this change will ensure that data etc. that are no longer present in the client folder are also removed from the backup folder.

The modified command is:

rsync -arz --delete /home/Client /home/Backup

-a=Archiving, the attributes will be copied


-r=Recursive, subfolders are synchronized too


-z=Compression, depending on data quantity/data size is compressed


--delete= Deletes data that no longer exists in the source but still exists in the target

Step 3

So that the command does not always have to be d manually, it can simply be placed in a cronjob. For example, a backup should be created daily at 3 am:

Open crontab -e:

With the number 1 "nano" can be used as an editor. With the number 2, "vim" can be used as an editor.

After the file has been opened with e.g. Nano, a crontab can be generated and entered. A crontab can be created with this Generator.

The entered crontab then looks as follows:

0 3 * * * rsync --progress -arz --delete -e  "ssh -i /home/sshkey/keybackup" /home/Client/ root@123.123.123.123:/home/Backup/Home-Server1/ >/dev/null 2>&1

Every day at 3 a.m. the command is executed and a backup is created.