Open In App

How to Use Rsync to Make a Remote Linux Backup

Last Updated : 13 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Imagine losing all your data by accidentally doing sudo rm -rf /someDirectory, sucks right? But it happens to the best of us. Now, if it was a Windows system, we could have used numerous backup options like OneDrive to secure our data. But what about Linux? Fortunately, we have some very robust tools to backup our Linux system too, and one of them is rsync.

Short for Remote SYNC, rsync is a CLI backup utility through which you can perform both local and remote backups in a secure and efficient way. It is secure because it uses SSH (secure shell) and efficient because it supports something called as incremental backup. Now what is incremental backup? In simple terms, rsync compares the file size and last modified time, and backs up only those files that have changed. rsync has become the industry standard to backup Linux systems for the last two decades because of a few additional reasons too, which are beyond the scope of this article.

Alright, enough theory. Now let’s see how to backup our Linux system to a remote computer using rsync.

Preparing for backup

You need to have rsync installed on both the source and destination machines. Generally, it comes preinstalled with most distros. You can check it by doing:

rsync --version
rsync---version

rsync –version

If it is not installed, you can get it by doing:

sudo apt install rsync

for Debian based systems, or an equivalent command for other package managers.

Backing up using rsync

After rsync has been setup on both the machines, we are now ready to send data back and forth. The rsync command is very simple, almost like the cp command that we all have been using for so long. A simple rsync command looks like this:

rsync  [OPTIONS]  SRC  DEST

Options Available in rsync in Linux

Option

Description

-r

recurse into directories

-z

compress file data during the transfer

-s

use the protocol to safely send the args

-v

increase verbosity

To see the full list of rsync options, just enter rsync –help into the terminal.

rsync---help

rsync –help

For the sake of this article, we will be backing up a directory from my PC which contains an image from the movie Coco to a remote server. I mean the movie is too cute to not back up, right?

The image miguel.jpg is inside a directory called coco in the home directory of my PC. So, we will simply write:

rsync -rsvz coco root@159.89.x.x:/backup

Here, -rsvz is all the options that I want, coco is the local directory that I want to backup, root is the username for my remote PC, 159.89.x.x is the IP address/hostname of the remote server, and finally, /backup is the destination directory that I want to put my backup into. Simple right?
It’s just -options source destination.

After I run the command, it will ask for the password for remote username (root in this case).

rsync-upload

rsync -rsvz coco root@159.89.x.x:/backup

After entering the password, rsync will backup the directory that you had specified. Like you can see, it has also printed a bunch of information to the console, that’s because we had used the -v option.

Now that the backup has succeeded, let’s verify it on the remote server too. I had set the destination to /backup, so I will do

ls /backup --recursive

on the remote machine to list all the directories and files recursively.

ls-backup---recursive

ls /backup –recursive

Like, you can see, our backup has been uploaded successfully to the remote machine, now you can rest assured that you will always have Coco with you even if you delete it by mistake. You can simply reverse the source and destination arguments to restore the backup from the remote server.

Conclusion

rsync is a very powerful tool. Do you want to learn one more cool trick? You can write a bash script for performing backup, and schedule a cron job to automatically run the file. And boom, you have your own automatic backup solution, just like OneDrive or Google Drive. I suggest you play with all the options of rsync, if you want to fully understand its potential. You should also check out rclone, which was inspired by rsync and offers similar functionalities for the cloud! For example, you can copy contents to and from Google Drive, or AWS S3. You can also use rclone to sync directories between clouds, like OneDrive and Google Drive!



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads