Backing up and restoring your Linux system using rsync

.

Create a backup using rsync If you need to back up your system, there is no better way than to back up using rsync.

Rsync (Remote Synchronization) is a popular and powerful tool used to copy and synchronize files and directories between remote or local Linux/Unix systems. With rsync, we can easily copy/sync data between local and remote directories, on different drives and on different networks.

In this tutorial, I'll show you how to back up with rsync using the Linux terminal. If you prefer graphical programs, you can also use rsync with a graphical interface.

Create a backup with rsync

For this tutorial, I will be using Arch Linux in a virtual machine created with VirtualBox. To simulate an external hard drive, I'll plug in a USB stick where the backup will be stored and then restored. I recommend that you do the same to check your backup. This will give you confidence that your backup is working because an unverified backup is not a backup.

In this case, we'll use this whole command:

sudo rsync -aAXv --delete --dry-run --exclude=/dev/* --exclude=/proc/* --exclude=/sys/* --exclude=/tmp/* --exclude=/run/* --exclude=/mnt/* --exclude=/media/* --exclude="swapfile" --exclude="lost+found" --exclude=".cache" --exclude="Downloads" --exclude=".VirtualBoxVMs"--exclude=".ecryptfs" / /run/media/alu/ALU/

To make a backup using rsync, we usually use the command line. I know that not everyone is sure about the command line tools, but you will find that the process is not that complicated and you can also back up your system using the command line.

We now turn to explain what this command means:
sudo - execute the command as superuser. Mandatory use.
rsync is the program itself to be used.
-a - archive mode.
-A - save the access control list.
-X - keep extended attributes.
Basically, these three options mean preserving all the attributes of your files. Owner attributes or rights will not be changed during the backup process.
-v - show the progress of the backup.
--delete - This option allows you to make an incremental backup. This means that if this is not your first backup, it will only keep the difference between your source and destination. Thus, it will only back up new files and changed files, and it also deletes all files in the backup that have been deleted from your system. Be careful with this option.
--dry-run - This option simulates a backup. Useful for checking if it is being executed.
--exclude - Excludes folders and files from the backup. I entered exclude as a separate parameter for each directory. You can also use it this way --exclude={/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/home/*,/lost+found}. But before running rsync, make sure you change the working directory to root (cd /), otherwise the shared exclusion option may not work.

Excluded folders are directly dependent on each of us, but folders /dev/, /proc/, /proc/ /sys/ /tmp/ /run/ /mnt/ и /media are not important for backups because rsync will not copy their contents. /mnt/ it is vital to exclude them if we plug in a USB stick.
/ - What we want to keep.
/run/media/alu/ALU is what you need for backups. I recommend encrypting the destination to keep your data safe.
Press Enter, the command will be executed in simulation mode (due to the --dry-run option). This way we test it to make sure everything is in order. If you are sure everything is running the way you want, you remove --dry-run from the command and run it again.
Note. It is recommended that the backup disk have a Linux compatible filesystem like ext4.

.

Restore the backup using rsync

To restore the backup we created, we're going to boot from the live ISO. Since we are working with Arch Linux, the ISO must be from Arch Linux. Next, we need to mount our USB stick.
After logging in from the live image, we have to create two folders, one for the system on the hard drive, and the other where the created backup will be mounted:

mkdir /mnt/system/mnt/usb

Next, we need to check the names of our devices:

lsblk

Then we have to mount the filesystem and the backup on the USB flash drive:

mount /dev/sda1 /mnt/system mount /dev/sdb1 /mnt/usb

Finally, let's start restoring our backup. To do this, run:

rsync -aAXv --delete --exclude="lost+found" /mnt/usb/ /mnt/system/

By executing this command, we restore a backup of our system.

.

Conclusion

Rsync is a powerful tool that can be used from the command line, with several options to adapt to any need. Of course, you can use GUI backup programs. There are also some really good non-open source graphical backup software available.
To keep your backup safe, I also recommend encrypting your backup drive.
Have you already made a backup? Let me know your experience with rsync.
Share this article on your social networks.