How to Backup A Directory in Linux

There are many ways to backup a directory in Linux, but how do you select one? The majority of people will back up their home directory to an external hard drive. Rsync will be our program, and we’ll use an external USB hard drive as the destination. Let’s get started!

Which directories you must back up

Unfortunately, it’s not at all clear on Linux systems what you need to include in a backup set. And how do you know which directories are worth including? Let’s discuss what needs to be backed up.

  • /home/: This contains all your personal files, documents, music, etc. You want this data included in any backup set. It is the most important directory to back up on a Linux system. If anything bad happens and you don’t have backups of it – well good luck! 
  • /etc/: The main configuration file for your operating system lives here (in Debian systems) or in subdirectories under /etc. Configuration information is key for getting all applications working properly after restoring from an older backup. 
  • /root/: This is the home directory for the root user. This should be included in your backup set if you are backing up your entire system. 
  • /var/: This contains files that change often, such as logs and mailboxes. If you’re just doing a quick ‘snapshot’ of your system, then you can probably omit this directory from the backup set.

Backup the home directory of the Linux system: Instructions

  1. Connect your external USB drive to your Linux system. 
  2. Open a terminal window and type the following command: sudo rsync -avz /home/username/ /media/usb-drive-namehere/backup-directory-namehere/ 
  3. Replace ‘username’ with your actual username, ‘usb-drive-namehere’ with the name of your USB drive, and ‘backup-directory-namehere’ with the desired backup directory on your USB drive. 
  4. Press enter. You will be prompted for your password. Type it in and press enter again.
  5. The rsync program will begin copying files from your home directory to the backup directory on your USB drive. This will take some time, depending on how much data is in your home directory. 
  6. When the process finishes you will receive a message stating how many files were copied and how long it took. 

Restore the home directory

To restore your backup copy later run this command: sudo rsync -avz /media/usb-drive-namehere/backup-directory-namehere/[email protected]:/home/username/. If you are not logged into Linux help then change ‘linuxhelp’ to the name of your server (if different). Replace username with actual username on Linux help. Press enter again. You will be prompted for your password, type it in and press enter again. The rsync program should begin restoring files from your USB drive’s backup directory to your home directory. This process may take some time, depending on how many files need to be restored and the size of your USB drive. 

When it is finished you will see a message stating “files have been successfully restored.” You can now log out of Linux help and reboot your computer. Your home directory will be exactly as it was when you backed it up!