Home Backup tools How To Backup Your Entire Linux System Using Rsync
Backup Your Entire Linux System Using Rsync

How To Backup Your Entire Linux System Using Rsync

By sk
2163 Views

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. This method will work on almost all Linux systems that have rsync installed. In this brief tutorial, let me explain how to backup your entire Linux system using Rsync utility.

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.

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


Recommended Read:


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 isn't a better solution. Because, the contents of these 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.

And, that's all. Hope this helps. If you find our guides useful, please share them on your social, professional networks, so that other users can also benefit from them. We will be keep posting more good stuffs everyday. Stay tuned with us to read awesome stuffs.

Cheers!

Source:

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

20 comments

dzs April 29, 2017 - 11:25 pm

Greetings Dear Writer!

This article is look so useful, however I don’t know one thing or maybe I could not solve.

If I have back up my entire system for a pendrive with rsync – in this case – , what shall I do with the system that the pendrive contains?. May I get an .iso or something else file or can I use for a ‘Live media’?

Sorry for my bad english and my silly question. 🙂

Best regards, Laci

Reply
JSawyer August 20, 2017 - 2:42 am

Hi, Laci. A bit late here!
You can copy the files to a fresh partition and boot it; there are various guides out there for this.

Reply
InvisibleInk April 29, 2017 - 11:27 pm

I like to backup only my home directory, minus the hidden dot files. That way, I can get a fresh, clean start on a new distro.

Here I’m backing up my home folder on an Ubuntu or Ubuntu-based distro to a folder named “synced-backup” on an external USB hard disk with the volume label “Seagate.”

rsync -r -t -v –progress –delete –exclude=”.*” –exclude “.*/” -s /home/$USER/ /media/$USER/Seagate/synced-backup

Reply
SK April 30, 2017 - 12:43 pm

Great tip. Thank you.

Reply
H0rnet May 1, 2017 - 11:26 am

Rsync is not a backup tool, instead it’s an efficient way transfer files and directories. It offers no point in time recovery options by it self. If you delete​ something on Monday you wouldn’t​ be able to restore it on Wednesday because Tuesdays backup will also remove the deleted file. You would be better off with a tools like rdiff-backup.

Reply
Jouni Järvinen August 5, 2017 - 8:25 pm

rsync is the way you create incremental backups for instance, so saying it’s not a backup tool is wrong.

Reply
Dan St.André May 2, 2017 - 10:42 pm

There is a package called LuckyBackyup [http://luckybackup.sourceforge.net/ — I am not affiliated. ] that is a desktop GUI deployment of ‘rsync’ based backup and restore. It runs as two editions: one as end-user; one as root-user. If you really like ‘rsync’ for backup-recovery, I encourage you to take a look at LuckyBackup.

Reply
SK May 3, 2017 - 12:28 pm

Thank you Dan. I will check it soon.

Reply
Cesar Jimenez May 7, 2017 - 8:40 pm

I modified for faster input instead of
“/media/*”,”/mnt/*”,”/proc/*”,”/run/*”,”/sys/*”,”/tmp/*”,
Just use
/media,/mnt,/proc,/run,/sys,/tmp
important no space after comma.
–exclude={/bak1,/cdrom,/dev,/home,/lost+found,/media,/mnt,/proc,/run,/sys,/tmp} / /bak1

Reply
Captain Murphy June 22, 2017 - 2:25 am

This was very helpful, thanks!

Reply
Søren Holm August 6, 2017 - 1:58 am

Remember –link-dest du allow using hardlinks for unchanged files from the previous backup.

Reply
marnixava November 6, 2017 - 9:26 am

“It doesn’t matter if your hard drive is different size, or use different filesystem.”

Au contraire. Rsync is great, but it *does* matter about the destination file system. For instance, using rsync to back up files in an ext4 file system to a FAT file system, would mean loss of file ownership and permission information. The destination file system needs to have at least the capabilities of the source file system.

Reply
Pepe June 7, 2018 - 10:05 pm

A good point!

Reply
HTGeek July 6, 2018 - 4:03 am

SK, I appreciate your efforts. However, IMHO you should make it more forcefully clear to your readers that RSYNC is not a suitable *complete* disk backup solution. It is an excellent *FILE* backup utility. However, it’s not a suitable *DISK* backup utility. The reason why is a disk must be un-mounted prior to backing up or there is a very high chance of file corruption on restore.

Reply
sk July 6, 2018 - 11:08 am

Agreed.

Reply
Shailesh Patle July 30, 2018 - 2:55 pm

Hello ,

Can we use that BACKUP on baremetal as fresh OS installation ?

Regards
Shailesh P

Reply
dirk August 2, 2018 - 11:55 pm

I was looking for a tool or a way to backup and “duplicate” a SD card of a Raspberry Pi. This worked for me.

Bought a small SSD, did connect it to my running Raspberry Pi with a USB/Sata cable and made 2 partitions on it. Then I copied the boot and root partitions, with Rsync. The files on the root partition as you described. Copied the files on the boot partion with rsync -av.

I wanted to use it on a second Raspberry Pi to boot from it. I changed the files ‘/boot/cmdline.txt’, ‘/boot/config.txt’ and ‘/etc/fstab’, and changed the file first 2 files on the SD card. After that the 2nd Raspberry Pi (model 3b+) booted from the SSD.

Awesome, thanks SK.

Other used links.

How to boot from a USB mass storage device on Raspberry Pi 3: https://www.raspberrypi.org/documentation/hardware/raspberrypi/bootmodes/msd.md

Make 2 partitions for the Raspberry Pi (choose 2nd tab):
https://archlinuxarm.org/platforms/armv6/raspberry-pi

Reply
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

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. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More