Feature image for the article about how to backup your home directory in Linux

How to backup your home directory in Linux

Got the itch for a little Linux distro-hopping? I know the feeling. We get spoiled with so many wonderful new Linux distribution releases throughout the year. It’s hard to resist the temptation. I typically first spin them up in VirtualBox. When it’s time to upgrade your daily driver PC, just make sure to first backup your personal data. This article explains step-by-step how to backup your home directory in Linux. We’ll use the rsync program in combination with an external USB drive.

Background

Throughout the year, several new Linux distributions release a new version. At the time of this writing Fedora 34 and Ubuntu 21.04 just got released. Within the next few months we can expect to see the release of openSUSE 15.3 and Debian 11.

Each time you read a Linux distribution’s installation or upgrade instructions, it emphasizes to always first backup your personal data. This makes sense, because if the installation or upgrade goes wrong, you might loose your personal data. However, most of these instructions unfortunately do not explain how to actually perform the backup of your personal data.

That’s where this article comes it. You’ll learn how to backup the home directory of your currently installed Linux system. Why the home (/home) directory? Assuming that you use Linux as a desktop PC, the home directory holds your personal data, so that’s the one to backup. In case you run Linux as a server, you might want to backup a different directory. For example /var/www for a web server.

What do you need

For backing up the home directory on your Linux system, we’ll use the rsync program. As the backup destination, we’ll use an external USB drive.

Think of rsync as the Swiss army knife for copying files, both locally and remotely. Because rsync is such a popular and versatile tool, the majority of Linux distributions install it by default. To verify the availability of rsync on your Linux system, run the command:

  • which rsync

This should output the location of the rsync, if installed. For example: /usr/bin/rsync. When the output does not show anything, then rsync is not installed on your Linux system. No problem, because we can quickly take care of this:

  • Ubuntu/Debian: sudo apt install rsync
  • openSUSE: sudo zypper install rsync
  • Fedora: sudo dnf install rsync

This article assumes that your USB drive is formatted using the EXT4 file system and mounted to directory /mnt/usbdrive. These previously published tutorials explain how you can do this:

Before you backup the home directory of your Linux system to the USB drive, make sure your personal data actually fits on the USB drive. Run the following command to determine the size of all data that resides in your home directory:

  • sudo du -sh /home

Backup the home directory of your Linux system

To backup the home directory of your Linux system with rsync, use the following rsync command syntax:

  • sudo rsync -a --info=progress2 --exclude="lost+found" --exclude=".cache" [SOURCE DIR]/ [DESTINATION DIR]/

This copies all files and directories from the [SOURCE DIR] recursively to the [DESTINATION DIR]. While copying the data, rsync preserves information such as:

  • Permissions
  • File and directory timestamps
  • Group and owner

During the copy operation, rsync outputs and updates a progress bar. Especially handy when copying a larger set of files and directories.

The lost+found directory is located at the root directory of a partition on EXT based file systems. In case you mounted your Linux home directory on a separate partition, you should exclude it from the backup. This directory is only used by the fsck program to store potentially corrupted files.

Similar for the .cache directory. Applications store non-essential data in this directory. Your web browser caches web page data here to load the page faster on a subsequent visit, to name just one example. Not really needed in your backup, so this directory can be excluded.

In our example, we want to backup our /home directory (source) to the external USB drive mounted at /mnt/usbdrive (destination). The following command handles this backup operation:

  • sudo rsync -a --info=progress2 --exclude="lost+found" --exclude=".cache" /home/ /mnt/usbdrive/

Make sure to add the forward slash (/) at the end of the source and destination directories.

Terminal screenshot that shows the rsync command for creating a backup of the home directory of your Linux system, to an external USB drive.

Restore the home directory of your Linux system

Once you completed the new installation of your preferred Linux distribution, you can easily restore the backup of your home directory with rsync. Just run the same command that you used for the backup and simply swap the [SOURCE DIR] and [DESTINATION DIR]:

  • sudo rsync -a --info=progress2 --exclude="lost+found" --exclude=".cache" /mnt/usbdrive/ /home/

In fact, we can shorten this a bit. The data on your external USB drive won’t have a .cache directory. So there is no need to exclude it. It will have a lost+found directory though, because the external USB is EXT4 formatted. The final command to restore the home directory of your Linux system with rsync:

  • sudo rsync -a --info=progress2 --exclude="lost+found" /mnt/usbdrive/ /home/

Again, make sure to add the forward slash (/) at the end of the source and destination directories.

Terminal screenshot that shows the rsync command to restore the backup of your Linux home directory.

Wrap up

After working your way through this article, you learned how to backup the home directory of your Linux system to an external USB drive. The perfect first step before upgrading or installing a new Linux distribution on your PC.

Assuming that you mounted your external USB drive to /mnt/usbdrive, the command to backup your Linux home directory is:

  • sudo rsync -a --info=progress2 --exclude="lost+found" --exclude=".cache" /home/ /mnt/usbdrive/

To restore the contents of the backed up home directory to your newly installed or upgrade Linux system, you can run a similar command. You just need to swap the directory names. Optionally, you can also skip the exclusion of the .cache directory:

  • sudo rsync -a --info=progress2 --exclude="lost+found" /mnt/usbdrive/ /home/

To emphasize one more time: don’t forget to add the forward slash (/) at the end of the source and destination directories.

PragmaticLinux

Long term Linux enthusiast, open source software developer and technical writer.

View all posts by PragmaticLinux →