Basics, Intermediate

Making Backups With Rsync

There is a famous saying among ‘nix users, “There are two types of people using linux – one kind of people who backup their system regularly and the other kind of people who never came across any issue in linux.” It is safe to say that the latter kind is virtually nonexistent.
Backing-up your system regularly is one of the most important and underrated tasks you will have to keep doing, once you have installed any distro. The truth of the matter is that at some point in time, things will get screwed up and there may not be an easy way to fix it. This is true for any system – Linux or Windows or Mac OS. It is always good to have a backup.

With that being said, in Linux you will come across problems, and the “solvability” of problems you might encounter depends mainly on the extent of your knowledge of Linux and also on how much support your distro’s forums, subreddits, etc. provide. However, you can always manage to get to the point of screwed-up from which there is no return, except for a clean install. And that is where backing up your system regularly comes into play.

What is rsync ?

As the official website puts it, “rsync is an open source utility that provides fast incremental file transfer.” So let us say that you have previously backed up your home directory to your external drive. After a while, you might have modified the directory and you would have made some changes. However, the majority of files and folders would remain the same. It would not make sense to copy the same unmodified files again to the external drive when you make your next backup. This is what you do when you choose the option to replace existing files while dragging and dropping folders in Nautilus, or when you are copying the entire directory through the command line. This is where rsync’s delta-transfer algorithm plays a significant role.

From the name “delta-transfer,” it should be apparent that rsync only transfers what is different or what is changed from your previous backup and the current state of your home directory. As the README says, “[rsync] does this by sending just the differences in the files across the link, without requiring that both sets of files are present at one of the ends of the link beforehand.

Rsync is such a versatile tool that it can be used to “sync” directories across network connections too! So you could set up your own backup server system with a Raspberry-pi and some hard drives. But that is a topic for another post 🙂

One more awesome & interesting feature of rsync is that it can, “optionally preserve symbolic links, hard links, file ownership, permissions, devices and times,” to quote the official website.

Which directories should I backup?

Backing up the entire root filesystem is the crude and naïve option – and it does work, but it comes with wastage of memory. Generally, from a vanilla installation of any distribution, you should be able to reinstall the packages you need, and you won’t need to take a backup of those files. It makes sense to make a backup of only those files and directories that you have created/modified. Also, you definitely do not need to make a backup of directories such as /dev, /mnt, /tmp, /sys and so on.

By no means an exhaustive list, here are the directories that I, personally consider necessary to backup –

  • /home – For obvious reasons.
  • /opt – This is where I store some of my hard-installed applications, i.e., those not installed by my package manager (apt, pacman, yum, etc.).
  • /usr/local – This is where I store the rest of my hard-installed applications.
  • /root – In case I have some configuration files or folders stored in the root user’s directory.
  • /var – This directory generally consists of a mixture of files and folders you might want to backup and some of which you won’t need to backup. Being lazy, I just take a backup of the entire directory.
  • /etc – Because this directory consists of system-wide configuration files.

For a more detailed take on what directories and files you might want to backup and why, check out this link.

Backing-up!

For the rest of the discussion, I will be using /home directory as an example to show how to use rsync to make backups. Also, note that the ‘#’ at the beginning of each command is to indicate the root’s prompt (and not a bash comment!), as some of the commands require super-user access.

First, find your external drive with the help of the following commands –

 # fdisk -l

or

# blkid

Say your device is /dev/sdb1. The next step is to mount the device. Once the device is mounted, navigate to the folder in which the previous backup of the /home directory exists, or to the folder in which you would like to make the first backup.

# mount /dev/sdb1 /mnt/ && cd /mnt/

Do an ls to quickly find out what the contents are.

# ls
home     usr     etc     var     opt     root
# ls home
joker    seewishnew

Here we see that there are two users – joker and seewishnew, and we want to backup the data of both users (as we are root 😉).
This can be done in various ways –

# rsync --archive --verbose --compress --progress /home ./ 

or

# rsync -avz --progress /home ./ 

or

# rsync -avz --progress /home ./home 

or

# rsync -avz --progress /home home

But do NOT do this –

# rsync -av --progress /home ./home/

The trailing slash at the end of the destination, ./home/ will cause rsync to create another backup of the /home directory inside the existing ./home in the external drive.

The option --archive, -a means that the files are transferred in “archive” mode, which ensures that symbolic links, devices, attributes, permissions, ownerships, etc. are preserved in the transfer.

The option --progress is used to show the improvement, i.e., the files and time remaining to complete the transfer.

--compress, z option means that compression will be used to reduce the size of data portions of the transfer.

Other useful rsync options

  • -h = human-readable. The numbers displayed, such as bytes, etc. will be in human readable format. For example, 1024 bytes will be represented as 1K.
  • -u = update. Skip files that are newer on the destination side.
  • -c = checksum. This ensures that existing files on the destination side are checked by checksum instead of modified times and sizes.
  • -n = dry-run. This will perform a trial run of rsync with the specified options, without making any changes.
  • --delete = Deletes all files in destination/ directory that are not present in source/ directory.

With this knowledge of rsync, backup your system efficiently and regularly! 😎

Leave a comment