How to Back Up and Restore Your Linux System Using the Rsync Utility

Written by David Worthington on January 21, 2022

Share This Article

Jump to Tutorial

It’s easy to take for granted the importance of having a server backup — until you experience a system failure, a natural disaster, or a malware attack. This can potentially paralyze your business through the loss of vital records such as financial and customer data. Your ability to recover will only be as good as your last backup. 

Server backups provide the much-needed confidence that data is not lost and can be recovered in case of any eventuality. However, each operating system has a different way to handle this, and while some provide built-in utilities to accomplish this, others must be established from the ground up. Like many other areas of management, Linux operating systems have more options, but require more knowledge to set them up successfully.

Like brushing your teeth, backing up your server regularly is an essential task that every Linux user should undertake. In addition to restoring from a major failure or attack, backups can save you time when you want to migrate data from one Linux system to another. Backups should always be encrypted and have the appropriate physical security controls in place for access if they are stored on premises. 

There are multiple backup solutions available to Linux users. You could use a backup software that automatically backs up your system at predefined times. Alternatively, you could manually back up your server to an external hard drive, RAID array, or another remote server.

One of the handiest and reliable backup tools you can use to back up your server is the rsync utility. 

What Is the Rsync Tool?

The rsync tool, short for remote sync, is a file synchronization tool that intelligently backs up data locally or remotely. It compares the source and destination location and only transfers the portions of data that have changed. It’s an incremental backup tool that saves your time when huge backups are involved.

In this guide, we explore how you can back up and restore your Linux system using the rsync utility. We will demonstrate how you can back up your Linux system on an external drive.

Basic Syntax

The rsync syntax is pretty straightforward and quite similar to the cp or the scp (secure copy).

To demonstrate how rsync works, we will start by creating two separate directories:

$ touch mydir1

$ touch mydir2

How to Back Up and Restore Your Linux System Using the Rsync Utility

Next, we are going to create a few text files in the first directory.

$ touch mydir1/file{1..5}.txt

To confirm the files exist, we will run the command:

$ ls -l mydir1

How to Back Up and Restore Your Linux System Using the Rsync Utility

Next, we will sync all the files in mydir1 directory to mydir2 as follows.

$ rsync -r  mydir1/ mydir2

The -r option stands for recursive. This implies that it takes into consideration all of the directory’s contents.

How to Back Up and Restore Your Linux System Using the Rsync Utility

Additionally, you could use the -a option instead. This option not only syncs recursively, but also preserves attributes such as symbolic links, file ownerships, permissions, and modification times. In most cases, this option is preferred to the -r option.

$ rsync -a  mydir1/ mydir2

Take note of the following:

In both commands, there is a trailing slash (/) after the first argument — which is mydir1.This specifies that only the contents of the directory, and not the directory itself in its entirety, is to be copied.

Without the trailing slash, the entire mydir1 directory would be placed into mydir2 directory.

$ rsync -a  mydir1 mydir2

How to Back Up and Restore Your Linux System Using the Rsync Utility

The -v option prints out the syncing process in verbose.

$ rsync -av  mydir1/ mydir2

How to Back Up and Restore Your Linux System Using the Rsync Utility

Now that you have a basic understanding of how rsync works, let’s now switch gears and explore how you can back up and restore a Linux system using the utility.

How to Make a Local Backup Using the Rsync Tool

Disclaimer: This method is recommended for standalone systems that are not accessed by other users or processes in the network since some data will constantly change with each passing minute and interfere with the backup process.

The rsync command-line tool is the most preferred backup tool in Linux systems for multiple reasons. It allows you to make incremental backups including the entire directory tree, both locally and on a remote server. Better yet, you can automate the backups using shell scripts and cron jobs.

In this section, we are going to demonstrate how you can back up your system on an external drive. You can also learn how to back up your system to another remote Linux system if you’d prefer in a different tutorial.

Keep reading to learn how to make a local backup.

Unmount, Format, and Remount the External Device

First, insert your external USB drive and take note of its SCSI ID. This can be done with different external devices as well. In the output, we can see it is labelled sdb.The complete SCSI ID for the device is /dev/sdb/ and it is currently mounted on the /media/jumpcloud/USB mount point.

How to Back Up and Restore Your Linux System Using the Rsync Utility

To use the USB drive as a backup destination, we need to format it to the ext4 filesystem. To achieve this, we will first unmount it.

$ sudo unmount /dev/sdb 

Once unmounted, format the external drive to ext4 filesystem using the mkfs command as follows:

$ sudo mkfs.ext4 /dev/sdb

Next, we are going to mount it back so the Linux system can detect it. To do so, we will create a mount point called /backup as follows. This is simply an arbitrary name and you are free to name it as you wish.

$ sudo mkdir /backup

Then mount the external drive to the mount point created.

$ sudo mount /dev/sdb /backup

Verify the Device is Mounted

You can verify the drive is mounted using the df command as follows:

$ df -Th | grep sd

How to Back Up and Restore Your Linux System Using the Rsync Utility

Test Run the Backup

At this juncture, we are ready to begin the backup. It’s always recommended to test the backup before running the real backup. Remember, an untested backup is as good as no backup at all.

To carry out a test run, navigate to the root (/) directory.

$ cd  /

Next, run the command:

$ sudo rsync -aAXv / –dry-run –-delete –exclude={/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/cdrom/*,/lost+found}  /backup

Let us quickly go over the command options:

How to Back Up and Restore Your Linux System Using the Rsync Utility

sudo – This allows you to execute the command as the root user or superuser.

rsync – This is the backup program itself.

-a – Archive mode.

-A – This preserves the Access Control List.

-X – This preserves all extended file attributes of the files.

The last three options allow you to preserve all of your files’ attributes. During the backup process, no permissions or ownership attributes will be defined.

-v – This is the verbose option. It prints the backup process on the terminal.

–delete – This option enables you to make an incremental backup. In simple terms, with exception of the first backup, it only backs up the difference existing between the source and the destination backup drive. It only backs up new and modified files as well as deletes all the files in the backup location which have been deleted.

–dry-run – This is the option that simulates the backup process.

–exclude – As the name suggests, it excludes specific folders and files from being backed up. The files and folders to be excluded are defined between the double curly braces { }. 

Run the Actual Backup and Restore Backup to Other PCs

Once the test run is complete, now run the actual backup command:

$ sudo rsync -aAXv / –-delete –exclude={/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/cdrom/*,/lost+found}  /backup

How to Back Up and Restore Your Linux System Using the Rsync Utility

The backup process takes quite some time, depending on the data on the directories you are backing up.

How to Restore the Backup

Once the backup is complete, you can easily restore it on another PC. First, create directories for both the USB drive and the file system:

$ sudo mkdir /mnt/drive

$ sudo mkdir /mnt/system

Next, mount your external device.

$ sudo mount /dev/sdb /mnt/drive

And the filesystem as well. 

$ sudo mount /dev/sda5 /mnt/system

In our case, /dev/sda5 is currently mounted on root (/). For your case, it might be something like /dev/sda1 if all the partitions fall under the root directory. 

Next, perform the backup as shown.

$ sudo rsync -aAXv –delete /mnt/drive  /mnt/system

Again, this will take a bit of time.

Conclusion

Rsync is a versatile and useful open source tool you can conveniently use to create a backup copy of your system and restore it. If you’d like to learn more about other ways to help manage the security of your Linux system, check out one of these Linux tutorials:

David Worthington

I'm the JumpCloud Champion for Product, Security. JumpCloud and Microsoft certified, security analyst, a one-time tech journalist, and former IT director.

Continue Learning with our Newsletter