Home Rsync How To Backup Your Entire Linux System Using Rsync

How To Backup Your Entire Linux System Using Rsync

By sk
Published: Last Updated on 156.7K views

In this brief tutorial, let me explain how to backup your entire Linux system using Rsync utility. This method will work on almost all Linux systems that have rsync installed.

Introduction

The other day I wanted to reinstall one of my virtual machine which was running with Ubuntu 16.04 LTS server. Before reinstalling it, I tried to backup the entire system.

While searching for an easiest way to do this, I found an easiest solution in Arch Linux wiki. I never thought that it was going to be that much simple.

I copied my entire root (/) tree with Rsync using a single line command. Yes, it is just a one-liner command. While there are so many tools to backup your systems, I find this method super easy and convenient, at least to me.

Also, this method is way better than disk cloning with dd command. Because It doesn't matter if your hard drive is different size, or use different filesystem. It will work regardless of hard disk's size and the underlying file system.

Please note your destination disk size should be bigger than the source disk's size. Both disk's need not to be same size.

Backup Your Entire Linux System Using Rsync

First, insert your backup medium (USB thumb drive or External hard disk). Then find the drive letter using 'fdisk -l' command. In my case, my Pen drive id is /dev/sdb1.

Mount your drive to any location of your choice. I am going to mount it under /mnt.

$ sudo mount /dev/sdb1 /mnt

To backup the entire system, all you have to do is open your Terminal and run the following command as root user:

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

This command will backup the entire root (/) directory, excluding /dev, /proc, /sys, /tmp, /run, /mnt, /media, /lost+found directories, and save the data in /mnt folder.

Let us break down the above command and see what each argument does.

  • rsync - A fast, versatile, local and remote file-copying utility
  • -aAXv - The files are transferred in "archive" mode, which ensures that symbolic links, devices, permissions, ownerships, modification times, ACLs, and extended attributes are preserved.
  • / - Source directory
  • --exclude - Excludes the given directories from backup.
  • /mnt - It is the backup destination folder.

Important note: Please be mindful that you must exclude the destination directory, if it exists in the local system. It will avoid the infinite loop.

If you want to preserve hard links, just include -H flag in the above command. Please note that it consumes more memory.

$ sudo rsync -aAXHv / --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} /mnt

Restore Backup

To restore the backup, just reverse the source and destination paths in the above command.

Please be mindful that this is suitable for local and stand-alone systems only. If your system is being actively accessed by some other systems on the network, it is not a better solution.

Because, the contents of the systems might be constantly updated every minute, and some files may change during the rsync process.

Say for example, when rsync will reach the file 2, the contents of the previous file (File 1) might be changed. This will leave you with a dependency error when you will need to use that backup.

In such cases, a snapshot-based backup is the better approach. Because the system will get "froze" before the backup process starts and get it "unfreeze" when the backup process finishes, so all the files are consistent.

Source:

You May Also Like

25 comments

Ocram September 26, 2019 - 12:00 pm

Wonderful linux! Just did a test with the backup I made with above commands. I can now restore a complete system in minutes.
Thank you for explanations of the command ‘rsync’ and taking the effort publishing it!

Reply
Gordon December 12, 2019 - 7:04 pm

To be on the safe side with open files or ones that may change, you could always switch to runlevel 1 before starting the backup.

Reply
Dominic January 8, 2020 - 10:59 pm

Hi, there is one important change you MUST make in your rsync command: Add the -H option. If you don’t do this then hardlinks will not be preserved and this will cause update hell later on (even though seemingly all is fine at first). Because some packages you hardlinks which are exactly the same file. When you update only one of them gets updated and the other one stays the old one. It even mentions it in the article you linked from: “If you use many hard links (e.g. if you are using Flatpak), consider adding the -H option, which is turned off by default due to its memory expense; however, it should be no problem on most modern machines. Many hard links reside under the /usr/ directory.”

When you do a full system backup the /usr/ dir is included and its literally riddled with hardlinks, so the -H option is a must. Otherwise looks good.

Reply
Steve Baker November 11, 2020 - 1:47 am

What if you wanted to do this over the network. What would be the process then??

Reply
sk November 11, 2020 - 11:12 am

Same as local copy. Just replace the destination with your remote location something like this:
rsync -aHAXxv --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} [source_dir] [destination_host:/destination_dir]

Reply
Jan March 11, 2022 - 4:58 pm

Thanks for sharing. If you add `–numeric-ids` to the rsync command the uid’s will be tranfered 1:1 and the local system will not guess the uid by the local user name.

Cheers,
Jan

Reply
sk March 11, 2022 - 6:58 pm

Thanks for the tip, Jan. Appreciated.

Reply
1 2

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More