Skip to content

VPS: Back Up a Linux Server with Rsync

Learn how to create efficient incremental backups with Rsync for local or remote data synchronization → Find out more

10 November 20264 min read24 Views0 helpful

Rsync:

You can use the Rsync tool to create backups.
You can copy data locally to another directory or drive on the system, or to a remote system.

Copy data to another local directory or drive:

Info

Note: Depending on the amount of data, the first run may take significantly longer than subsequent runs. This is because Rsync synchronizes all data the first time; from the second run onward, only changed data is transferred. This creates an incremental backup.

Step 1

You can install Rsync with the following command:

Code
apt install rsync

Once installed, you can use it right away.

Step 2

In this example, the Client folder in /home is synchronized with the Backup folder in /home.

Use the following command:

Code
rsync -arz /home/Client /home/Backup

-a=Archive mode; copies all attributes
-r=Recursive; also synchronizes subfolders
-z=Compression; data is compressed depending on the amount and size of the data

The folder was successfully synchronized.

If a file, etc. is deleted from the Client folder, it remains in the Backup folder.
However, if the files should always be identical, you can modify the rsync command so that data no longer present in the Client folder is also removed from the Backup folder.

The modified command is:

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

-a=Archive mode; copies all attributes
-r=Recursive; also synchronizes subfolders
-z=Compression; data is compressed depending on the amount and size of the data
--delete= Deletes data that no longer exists in the source but is still present in the destination

Step 3

To avoid running the command manually every time, you can add it to a cron job.
For example, to create a backup every day at 3 a.m.:

Open crontab -e:

Choose option 1 to use "nano" as the editor. Choose option 2 to use "vim" as the editor.

After opening the file, for example with Nano, you can create and add a cron job.
You can create a cron job with this generator.

The cron job will look like this:

Code
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

The command runs every day at 3 a.m. and creates a backup.

Step 4

In this example, the Client folder in /home is synchronized with the Backup folder on a remote server. The connection uses an SSH key so that the backup can run automatically.

Important: Rsync must also be installed on the remote server.

Code
apt install rsync

For example, you can use the following command to back up the data to the remote host (adjust as needed):

Code
rsync --progress -arz -e  "ssh -i /home/sshkey/keybackup" /home/Client/ root@123.123.123.123:/home/Backup/Home-Server1/

-a=Archive mode; copies all attributes
-r=Recursive; also synchronizes subfolders
-z=Compression; data is compressed depending on the amount and size of the data
-e=Specifies the remote shell to use (default: ssh), for example, to specify an SSH key or a different port
Specify the SSH key (path)("ssh -i /home/sshkey/keybackup")= ssh -i /[path to key]
Specify the directory to back up (/home/client/)= /[directory]
Remote host (root@123.123.123.123:)= User login name on the remote host and address: name@IP/domain
Destination directory on the remote host (:/home/Backup/Home-Server1/)= :/[path to destination directory]

After running the command, the folders/files were successfully synchronized/backed up to the remote directory.

If a file, etc. is deleted from the Client folder, it remains in the Backup folder on the remote host.
However, if the files should always be identical, you can modify the rsync command so that data no longer present in the Client folder is also removed from the Backup folder on the remote host.

The modified command is:

Code
rsync --progress -arz --delete -e  "ssh -i /home/sshkey/keybackup" /home/Client/ root@123.123.123.123:/home/Backup/Home-Server1/

-a=Archive mode; copies all attributes
-r=Recursive; also synchronizes subfolders
-z=Compression; data is compressed depending on the amount and size of the data
--delete= Deletes data that no longer exists in the source but is still present in the destination
-e=Specifies the remote shell to use (default: ssh), for example, to specify an SSH key or a different port
Specify the SSH key (path)("ssh -i /home/sshkey/keybackup")= ssh -i /[path to key]
Specify the directory to back up (/home/client/)= /[directory]
Remote host (root@123.123.123.123:)= User login name on the remote host and address: name@IP/domain
Destination directory on the remote host (:/home/Backup/Home-Server1/)= :/[path to destination directory]

Step 5

To avoid running the command manually every time, you can add it to a cron job.
For example, to create a backup every day at 3 a.m.:

Open crontab -e:

Choose option 1 to use "nano" as the editor. Choose option 2 to use "vim" as the editor.

After opening the file, for example with Nano, you can create and add a cron job.
You can create a cron job with this generator.

The cron job will look like this:

Code
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

The command runs every day at 3 a.m. and creates a backup.

FAQ

Frequently asked questions

Run the command 'apt install rsync'.
Rsync synchronizes all data the first time. From the second run onward, it transfers only changed data, creating an incremental backup.
Run 'rsync -arz /home/Client /home/Backup'. The flags stand for archive mode (-a), recursive (-r), and compression (-z).

Feedback

Was this article helpful?

Your feedback helps us prioritize and improve content.

0 of 0 people found this article helpful.

Personal support

Need more help than the guide provides?

Our ZAP support team knows your product — briefly describe your issue and we'll help you personally.

Zappy knows this guide and answers you right in the chat.