How to Make a Local Linux Backup Using the Rsync Tool – JumpCloud

It’s

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

Server backups provide 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 of handling this, and while some provide built-in utilities to achieve this, others need to be set from scratch. Like many other areas of administration, Linux operating systems have more options, but require more knowledge to configure correctly.

Just 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 adequate physical security controls for access if stored on-premises.

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

One of the

most practical 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 synchronization, is a file synchronization tool that intelligently backs up data locally or remotely. It compares the source and destination location and only transfers the parts of data that have changed. It is an incremental backup tool that saves you time when it comes to huge backups.

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

Basic syntax The rsync

syntax

is fairly straightforward and quite similar to cp or scp (secure copy).

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

$ touch

mydir1 $ touch mydir2

How to backup and restore your Linux system using the rsync utility

Next, let’s create some text files in the first directory.

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

To confirm that

the files exist, we will run the command: $ ls -l mydir1

<img src="https://jumpcloud.com//wp-content/uploads/2022/01/rsync-2.png" alt="How to backup and restore your Linux system using

the rsync utility” />

Next, we will synchronize all the files in the mydir1 directory with mydir2 as follows.

$ rsync -r mydir1/ mydir2

The -r option stands for recursive. This implies that it takes into account the entire contents of the directory.

How to backup and restore your Linux system using rsync utility

Also, you can use the -a option instead. Not only does this option synchronize recursively, but it also preserves attributes such as symbolic links, file ownership, 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 forward slash (/) after the first argument, which is mydir1. This specifies that only the contents of the directory should be copied, and not the directory itself as a whole.

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

$ rsync -a mydir1 mydir2 How to backup and restore your Linux system using the rsync utility

The -v option prints the synchronization process in detail.

$ rsync

-av mydir1/ mydir2

<img src="https://jumpcloud.com//wp-content/uploads/2022/01/rsync-5.png" alt="How to backup and restore your Linux system using the rsync utility"

/>

Now that you have a basic understanding of how rsync works, let’s switch gears and explore how you can backup 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 on the network, as some data will constantly change with each passing minute and interfere with the backup process

. The rsync command-line tool

is the preferred backup tool

on Linux systems for several reasons. It allows you to perform incremental backups that include the entire directory tree, both locally and on a remote server. Better yet, you can automate backups using shell scripts and cron jobs.

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

Read on to learn how to back up locally

.

Unmount, format and remount the external device

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

How to backup and restore your Linux system using Rsync utility

To use USB drive as backup destination, we need to format it in ext4 file system. To achieve this, we will first disassemble it.

$ sudo unmount /dev/sdb Once unmounted, format the external drive to ext4 file system using the mkfs

command as follows: $ sudo mkfs.ext4

/

dev/sdb

Next, we are going to mount it again so that the Linux system can detect it. To do this, we will create a mount point named /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

created mount point.

$

sudo mount /dev/sdb /backup

Verify that the

device is

mounted

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

$ df -th | grep sd

How to backup and restore your Linux system using rsync utility

Try Run Backup

At this juncture, we’re ready to start backup. It is always recommended to test the backup before running the actual backup. Remember, an untested backup is only as good as no backup.

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

$

cd

/ Then run the command: $ sudo rsync -aAXv / -dry-run -delete -exclude={/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/

cdrom/*,/lost+found} /backup

Let’s quickly go over the command options:

How to backup and restore your Linux system using the rsync utility

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

rsync – This is the backup program itself

. –

a – File mode

. -A – This

preserves the access control list

. –

X – This preserves all the extended file attributes of

the files.

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

-v – This is the detailed option. Print the backup process to the terminal.

-delete – This option allows you to make an incremental backup. In simple terms, with the exception of the first backup, it only backs up the difference between the source and destination backup drive. It only backs up new and changed files, as well as deletes all files in the backup location that 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 backup. The files and folders to exclude are defined between the double braces { }.

Run the actual backup and restore the backup to other PCs

After 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 backup and restore your Linux system using rsync utility

The backup process takes quite a while, depending on the data in the directories you are backing up

.

How

to restore the backup

Once the backup is complete, you can easily restore it to another PC. First, create directories for both USB

drive and file system: $ sudo mkdir /mnt/drive $ sudo

mkdir /mnt

/system

Next, mount your external device

. $ sudo mount /dev/sdb /mnt/drive

And the file system too.

$ sudo mount

/dev/sda5 /

mnt/system

In our case, /dev/

sda5

is currently mounted in root (/). For your case, it could be something like /dev/sda1 if all partitions fall under the root directory.

Then perform the backup as shown

.

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

Again, this will take a little time

.

Conclusion

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

: How to enable full disk encryption on an Ubuntu 20.04 desktop How to

  • Create and Manage SSH Keys
  • on Linux Machines How to Manage

  • Sudo Access in Ubuntu 20.04 How to manage
  • user passwords on Linux machines

Contact US