# Introduction The aim of this guide is to show you a fairly simple way to perform a full backup and restoration of a local Linux installation. We cover only the basic use case of doing the backup and restoration on the **same machine** (but we will account for possible **disk changes**). ## Preparation While you can **theoretically** take a full snapshot from a running system, it's a bit risky since some files could change during the copy process leading to potentially unexpected behavior. I prefer doing this with the system **offline** so we'll only cover that case here. You need two things to start: 1. A **live Linux USB** to boot from. Doesn't really matter which distro. 2. The media on which you'll backup your system. This could be an **external SSD** or a **USB flash drive**, for example. We'll only cover backing up the **/root** partition. I personally don't use a separate **/home** partition. We won't cover backing up your **EFI** boot partition either. After booting into your live USB, you'll need to: 1. `lsblk` and take note of the output. 2. `mount` the root partition of the **existing** installation you want to backup. For example, if your **root** partition sits at **/dev/sda1**, the command will look like this: ```bash sudo mkdir /mnt/installation/ && sudo mount /dev/sda1/ /mnt/installation/ ``` 1. `mount` the external media you're copying your backup to. For example, if your external USB flash drive is at **/dev/sdb1**, the command will look like this: ```bash sudo mkdir /mnt/backup/ && sudo mount /dev/sdb1/ /mnt/backup/ ``` ## Perform the Backup We're going to use [rsync](https://wiki.archlinux.org/title/rsync). **Note that the filesystem of the external media you're backing up your system to needs to be ext4 in order to retain things like unix permissions.** If it's not, go back to the previous step, un-mount (`umount`) your device, format it to **ext4**, then remount it and continue from here. Assuming the mount points from the example above, the command will look like this: ```bash rsync /mnt/installation/ -aAXHv --exclude={"/mnt/installation/dev/*","/mnt/installation/proc/*","/mnt/installation/sys/*","/mnt/installation/tmp/*","/mnt/installation/run/*","/mnt/installation/mnt/*","/mnt/installation/media/*","/mnt/installation/lost+found"} /mnt/backup/ ``` We exclude **dynamic system directories** populated usually by the **kernel** on boot and only backup **actual persistent disk files**. As explained on the Arch wiki, by using the `-aAX` set of options, the files are transferred in archive mode which ensures that symbolic links, devices, permissions, ownerships, modification times, [ACLs](https://wiki.archlinux.org/title/ACL "ACL"), and extended attributes are preserved, assuming that the target file system supports the feature. The option `-H` preserves hard links, but uses more memory. This will take some time depending on how big the backup is and how fast you can write to the external media you're backing up to. ## Restoring the Backup Restoring a backup is as simple as **reversing** your **source** and **destination** parameters to **rsync**. Assuming similar mount point names, the command would look something like this: ```bash rsync /mnt/backup/ -aAXHv /mnt/installation/ ``` But you have to be careful here depending on your case. Here are a few things to remember: - If this is a **new empty disk**, you'll have to **create** and **configure** the **boot partition** and the **bootloader**, which is out of the scope of this article. - **Be careful with /etc/fstab and /etc/crypttab**. Depending on your use case, you'll probably need to fix the **UUIDs** in there manually. **blkid** is your friend. - If you're lazy or inexperienced, one option you have is to **reinstall** your same distro on the **new** disk, then, while still in the live installation media and before rebooting into the new system, restore the backup and **overwrite** the root partition. Just make sure to exclude **/etc/fstab** and **/etc/crypttab** as those have UUIDs corresponding to your new disk now. - If you're restoring the backup to a **new machine**, you have to account for hardware changes. The Linux kernel usually handles this well but it's still something to keep in mind and account for. ## Final Remarks **rsync** is a very powerful too and you can use it to do **incremental backups** where it only copies files that have changed since the last backup (**deltas**). You can also of course **automate** that process with **cron jobs**. It's probably a good idea to read [this](https://wiki.archlinux.org/title/rsync#Full_system_backup) well before you do anything.