Backup and Restore Your Linux System with rsync

It's only fair to share...Share on Facebook
Facebook
Share on Google+
Google+
Tweet about this on Twitter
Twitter
Share on Reddit
Reddit
Email this to someone
email
Command to create a backup with rsync

Create a backup with rsync

If you need to backup your system, there is no better way than making a backup with rsync.

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

In this tutorial, I will show you how to make a backup with rsync using the Linux terminal.

Video Tutorial

Making the backup with rsync

To make this tutorial, I will use Arch Linux in a virtual machine built with VirtualBox. To simulate an external hard drive, I will connect a USB flash drive where the backup will be stored and then restored. I recommend you do the same to test your backup. This will give you the confidence to know that your backup works because an untested backup is not a backup.

For this case we will use this entire 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 with rsync, we usually use the Command line. I know that not everyone is confident with the command line tools, but you will realize that the process is not that complicated and you can also back up your system using the command line.

We now proceed to explain what this command means:

sudo – to execute the command as a superuser. Mandatory use.

rsync – is the program itself to use.

-a – archive mode.

-A – preserve Access Control List.

-X – preserve extended attributes.

Basically, these three options mean to preserve all the attributes of your files. Owner attributes or permissions will not be modified during the backup process.

-v – It will show the progress of the backup.

--delete – this option allows you to make an incremental backup. That means, if it is not your first backup, it will backup only the difference between your source and the destination. So, it will backup only new files and modified files and it will also delete all the files in the backup which were deleted on your system. Be careful with this option.

--dry-run – This option simulates the backup. Useful to test its execution.

--exclude – Excludes folders and files from backup. I typed exclude as a separate option for every directory. You can also use it this way --exclude={/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/home/*,/lost+found}. But make sure you change your working directory to root (cd /) before you run rsync, otherwise the joint exclude option may not work.

The excluded folders depend directly on each of us, however, the /dev/, /proc/, /proc/ /sys/ /tmp/ /run/ /mnt/ and /media folders are not important to backup because rsyn will not copy their content. /mnt/ it is vital to exclude them if we connect a USB memory.

/ – What we want to back up.

/run/media/alu/ALU – This is where you what to backup.

We press enter, the command will execute in a simulation mode (because of the --dry-run option) This way we test it to make sure everything is okay. When you’re sure that everything is performed as you want, you remove --dry-run from the command and run it again.

Note: It is recommended that the backup drive has a Linux compatible file system as ext4.

Restore the backup with rsync

To restore the backup we have made, we are going to boot from a live ISO. Since we are working with Arch Linux, then the iso image must be from Arch Linux. Next, we must mount our USB flash drive.

Once logged in from the live image, we must create two folders, one for the system on the hard disk and the other where the backup created will be mounted:

mkdir /mnt/system /mnt/usb

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

lsblk

Then, we must mount the file system and the backup on the USB flash device:

mount /dev/sda1 /mnt/system
mount /dev/sdb1 /mnt/usb
Mounting directories to restore the backup with rsync

Mounting directories to restore the backup with rsync

Finally, we proceed to perform the restoration of our backup. For this we run:

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

By running this command, we restore the backup of our system.

 

As a conclusion, I would like to say that rsync is a powerful tool that can be used from the command line, with multiple options that adapt to any need.

Have you already done your backup? Let me know about your experience with rsync.

Please share this article through your social networks.

It's only fair to share...Share on Facebook
Facebook
Share on Google+
Google+
Tweet about this on Twitter
Twitter
Share on Reddit
Reddit
Email this to someone
email

Leave a Reply

Your email address will not be published. Required fields are marked *

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