{"id":58482,"date":"2022-01-21T11:49:28","date_gmt":"2022-01-21T16:49:28","guid":{"rendered":"https:\/\/jumpcloud.com\/?p=58482"},"modified":"2023-01-26T14:20:26","modified_gmt":"2023-01-26T19:20:26","slug":"how-to-backup-linux-system-rsync","status":"publish","type":"post","link":"https:\/\/jumpcloud.com\/blog\/how-to-backup-linux-system-rsync","title":{"rendered":"How to Back Up and Restore Your Linux System Using the Rsync Utility"},"content":{"rendered":"\n

Jump to Tutorial<\/a><\/p>\n\n\n\n

It\u2019s easy to take for granted the importance of having a server backup \u2014 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. <\/p>\n\n\n\n

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.<\/p>\n\n\n\n

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. <\/p>\n\n\n\n

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.<\/p>\n\n\n\n

One of the handiest and reliable backup tools you can use to back up your server is the rsync<\/code> utility. <\/p>\n\n\n\n

What Is the Rsync Tool?<\/strong><\/h2>\n\n\n\n

The rsync<\/code> 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\u2019s an incremental backup tool that saves your time when huge backups are involved.<\/p>\n\n\n\n

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

Basic Syntax<\/strong><\/h2>\n\n\n\n

The rsync syntax is pretty straightforward and quite similar to the cp or the scp (secure copy).<\/p>\n\n\n\n

To demonstrate how rsync works, we will start by creating two separate directories:<\/p>\n\n\n\n

$ touch mydir1<\/code><\/p>\n\n\n\n

$ touch mydir2<\/code><\/p>\n\n\n\n

\"How<\/figure>\n\n\n\n

Next, we are going to create a few text files in the first directory.<\/p>\n\n\n\n

$ touch mydir1\/file{1..5}.txt<\/code><\/p>\n\n\n\n

To confirm the files exist, we will run the command:<\/p>\n\n\n\n

$ ls -l mydir1<\/code><\/p>\n\n\n\n

\"How<\/figure>\n\n\n\n

Next, we will sync all the files in mydir1<\/code> directory to mydir2 <\/code>as follows.<\/p>\n\n\n\n

$ rsync -r  mydir1\/ mydir2<\/code><\/p>\n\n\n\n

The -r <\/code>option stands for recursive. This implies that it takes into consideration all of the directory\u2019s contents.<\/p>\n\n\n\n

\"How<\/figure>\n\n\n\n

Additionally, you could use the -a<\/code> 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<\/code> option.<\/p>\n\n\n\n

$ rsync -a  mydir1\/ mydir2<\/code><\/p>\n\n\n\n

Take note of the following:<\/p>\n\n\n\n

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

Without the trailing slash, the entire mydir1<\/code> directory would be placed into mydir2<\/code> directory.<\/p>\n\n\n\n

$ rsync -a  mydir1 mydir2<\/code><\/p>\n\n\n\n

\"How<\/figure>\n\n\n\n

The -v<\/code> option prints out the syncing process in verbose.<\/p>\n\n\n\n

$ rsync -av  mydir1\/ mydir2<\/code><\/p>\n\n\n\n

\"How<\/figure>\n\n\n\n

Now that you have a basic understanding of how rsync works, let\u2019s now switch gears and explore how you can back up and restore a Linux system using the utility.<\/p>\n\n\n\n

How to Make a Local Backup Using the Rsync Tool<\/strong><\/h2>\n\n\n\n

Disclaimer:<\/em><\/strong> 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.<\/em><\/p>\n\n\n\n

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.<\/p>\n\n\n\n

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<\/a> if you’d prefer in a different tutorial. <\/p>\n\n\n\n

Keep reading to learn how to make a local backup.<\/p>\n\n\n\n

Unmount, Format, and Remount the External Device<\/h3>\n\n\n\n

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<\/code>.The complete SCSI ID for the device is \/dev\/sdb\/ <\/code>and it is currently mounted on the \/media\/jumpcloud\/USB<\/code> mount point.<\/p>\n\n\n\n

\"How<\/figure>\n\n\n\n

To use the USB drive as a backup destination, we need to format it to the ext4 <\/code>filesystem. To achieve this, we will first unmount it.<\/p>\n\n\n\n

$ sudo unmount \/dev\/sdb <\/code><\/p>\n\n\n\n

Once unmounted, format the external drive to ext4 <\/code>filesystem using the mkfs <\/code>command as follows:<\/p>\n\n\n\n

$ sudo mkfs.ext4 \/dev\/sdb<\/code><\/p>\n\n\n\n

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<\/code> as follows. This is simply an arbitrary name and you are free to name it as you wish.<\/p>\n\n\n\n

$ sudo mkdir \/backup<\/code><\/p>\n\n\n\n

Then mount the external drive to the mount point created.<\/p>\n\n\n\n

$ sudo mount \/dev\/sdb \/backup<\/code><\/p>\n\n\n\n

Verify the Device is Mounted<\/h3>\n\n\n\n

You can verify the drive is mounted using the df<\/code> command as follows:<\/p>\n\n\n\n

$ df -Th | grep sd<\/code><\/p>\n\n\n\n

\"How<\/figure>\n\n\n\n

Test Run the Backup<\/h3>\n\n\n\n

At this juncture, we are ready to begin the backup. It\u2019s always recommended to test the backup before running the real backup. Remember, an untested backup is as good as no backup at all.<\/strong><\/p>\n\n\n\n

To carry out a test run, navigate to the root (\/<\/code>) directory.<\/p>\n\n\n\n

$ cd  \/<\/code><\/p>\n\n\n\n

Next, run the command:<\/p>\n\n\n\n

$ sudo rsync -aAXv \/ –dry-run \u2013-delete –exclude={\/dev\/*,\/proc\/*,\/sys\/*,\/tmp\/*,\/run\/*,\/mnt\/*,\/media\/*,\/cdrom\/*,\/lost+found}  \/backup<\/code><\/p>\n\n\n\n

Let us quickly go over the command options:<\/p>\n\n\n\n

\"How<\/figure>\n\n\n\n

sudo <\/code>\u2013 This allows you to execute the command as the root user or superuser.<\/p>\n\n\n\n

rsync<\/code> \u2013 This is the backup program itself.<\/p>\n\n\n\n

-a <\/code>\u2013 Archive mode.<\/p>\n\n\n\n

-A<\/code> \u2013 This preserves the Access Control List.<\/p>\n\n\n\n

-X<\/code> \u2013 This preserves all extended file attributes of the files.<\/p>\n\n\n\n

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.<\/p>\n\n\n\n

-v<\/code> \u2013 This is the verbose option. It prints the backup process on the terminal.<\/p>\n\n\n\n

–delete <\/code>\u2013 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.<\/p>\n\n\n\n

–dry-run<\/code> \u2013 This is the option that simulates the backup process.<\/p>\n\n\n\n

–exclude<\/code> \u2013 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 { }.\u00a0<\/p>\n\n\n\n

Run the Actual Backup and Restore Backup to Other PCs<\/h3>\n\n\n\n

Once the test run is complete, now run the actual backup command:<\/p>\n\n\n\n

$ sudo rsync -aAXv \/ \u2013-delete –exclude={\/dev\/*,\/proc\/*,\/sys\/*,\/tmp\/*,\/run\/*,\/mnt\/*,\/media\/*,\/cdrom\/*,\/lost+found}  \/backup<\/code><\/p>\n\n\n\n

\"How<\/figure>\n\n\n\n

The backup process takes quite some time, depending on the data on the directories you are backing up.<\/p>\n\n\n\n

How to Restore the Backup<\/strong><\/h2>\n\n\n\n

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:<\/p>\n\n\n\n

$ sudo mkdir \/mnt\/drive<\/code><\/p>\n\n\n\n

$ sudo mkdir \/mnt\/system<\/code><\/p>\n\n\n\n

Next, mount your external device.<\/p>\n\n\n\n

$ sudo mount \/dev\/sdb \/mnt\/drive<\/code><\/p>\n\n\n\n

And the filesystem as well. <\/p>\n\n\n\n

$ sudo mount \/dev\/sda5 \/mnt\/system<\/code><\/p>\n\n\n\n

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

Next, perform the backup as shown.<\/p>\n\n\n\n

$ sudo rsync -aAXv –delete \/mnt\/drive  \/mnt\/system<\/code><\/p>\n\n\n\n

Again, this will take a bit of time.<\/p>\n\n\n\n

Conclusion<\/strong><\/h2>\n\n\n\n

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\u2019d like to learn more about other ways to help manage the security of your Linux system, check out one of these Linux tutorials:<\/p>\n\n\n\n