Create and Restore Incremental backups in Linux with tar

This tutorial explains how to take and restore the incremental backup in Linux with practical examples. Learn what the incremental backup is and how to create it in the Linux with the tar command.

What is the incremental backup?

Incremental backup is the advanced type of backup. In it, only the data that is created or modified since the last backup has taken, is backed up. It not only save disk space but also reduce the time taken in backup process. Let’s take an example.

A disk, that contains 60 GB data, is backed up daily. Suppose data writing speed in backup process is 1 GB per minute and 1 GB data is modified daily. If we take the full backup, total 375 GB data will be written in total 375 minutes. But if we take the incremental backup, only 65 GB data will be written in 65 minutes.

Day DataNew or Modified data Data written in full backup Time taken in full backup Data written in incremental backup Time taken in incremental backup
Monday 60 GB 60 GB 60 GB 60 Minutes 60 GB 60 Minutes
Tuesday 60 GB 1 GB 61 GB (60+1) 61 Minutes 1 GB 1 Minute
Wednesday 61 GB 1 GB 62 GB 62 Minutes 1 GB 1 Minute
Thursday 63 GB 1 GB 63 GB 63 Minutes 1 GB 1 Minute
Friday 64 GB 1 GB 64 GB 64 Minutes 1 GB 1 Minute
Saturday 65 GB 1 GB 65 GB 65 Minutes 1 GB 1 Minute
Total 375 GB 375 Minutes 65 GB 65 Minutes

incremental backup vs regular backup

How to take the incremental backup in Linux?

Advertisements

In Linux, several utilities exist to take the incremental backup. Among those, the command line utility tar stands at the top place. It functions as a standalone utility for creating and restoring the backups. In this tutorial, we will use this utility to create and restore the incremental backup.

For this tutorial, I assume that you know what the tar command is and how it works in Linux. To learn the tar command from scratch, you can use this tutorial.

Tar command and Syntax Explained with Examples

It explains how to use the tar command in the Linux with practical examples.

Lab setup for practice

Access a root shell and create a directory named practicelab. In this directory create two another directories named; backup and restore. In the directory backup, create few files and a sub-directory with a file.

Following figure illustrates this setup.

lab setup for the parctice of incremental backup

Creating incremental backups with the tar command

An incremental backup always starts with a full backup. Once the full backup is created, it is used in all incremental backups to compare and detect the changes.

Technically while creating the full backup; the tar creates a snapshot file which contains a list of all files and directories that have been written to the backup. Next time, it checks this file to find out what files and directories have been changed since the last backup, and writes only those changed files and directories.

To create the first full backup, use following command

#tar -czvg snapshot-file -f backup.tar.gz backup

In this command;

tar: - This is the main command.

-czvg: - These are the options. These options are used to create backup, compress backup with the gzip utility, display progress of backup process and use the snapshot file to identify the modified files respectively.

snapshot-file: - Name and location of the file that stores the list of files and directories which are added in the archive. You can use any descriptive name for this file. To store this file in other directory except the current directory, specify its full path. For example, to use the file name backup-list and store this file in /root directory, specify name and location as /root/backup-list.

-f: - This is also an option. It specifies the name and location of the backup file. Backup file is also known as archive or tarball. An archive or a tarball is the collection of multiple files and directories that are grouped together as a single file in backup process.

backup.tar.gz: - This is the name of archive or tarball file. Just like the snapshot file, you can use any name or location to store this file.

backup: - This is the name of directory which files and directories we want to add in archive. You can specify single file, multiple files, single directory or multiple directories here. If a directory is specified, all of its files and sub-directories will be added in archive.

Once full backup is created, you can verify the backup file with following command.

#tar -tvf backup.tar.gz

This command lists the files and directories stored in the archive file backup.tar.gz.

creating full backup

Creating incremental backup

To create incremental backup, same command and options are used. While creating the backup, the snapshot file is used to determine whether it’s a full backup or an incremental backup. If the snapshot file exists, it’s an incremental backup otherwise it’s a full backup.

While creating incremental backups, same snapshot file and different backup file are used each time.

  • Since a snapshot file is used to identify what is changed, same snapshot file must be used each time.
  • Since what is changed is stored in backup file, different backup file must be used each time.

To understand it practically, let’s create a new file in the directory backup and run following command.

#tar -czvg snapshot-file -f 1-backup.tar.gz backup

Notice, this is the same command which we used to create the full backup except the name of backup file. This command uses the snapshot-file to determine what have been already backed up from the directory backup. If it finds any new file or directory, it adds that file or directory in the backup file 1-backup.tar.gz.

Let’s modify a file from the directory backup and run above command again. Use new backup file to store the change.

#tar -czvg snapshot-file -f 2-backup.tar.gz backup

Same as above command, this command also uses the snapshot file to find the new or modified files. If it detects any new or modified file, write that file in the backup file 2-backup.tar.gz.

To view what have been stored in both backup files, you can use following commands.

#tar -tvf 1-backup.tar.gz
#tar -tvf 2-backup.tar.gz

Following figure illustrates above exercise.

creating and listing incremental backup

Restoring incremental backups

Incremental backups are restored in the same order in which they were created. The full backup file is restored at the first place. After it, all sequential files are restored till the last backup file.

To restore the backup file, use the options -xvf. For example, to restore the backup files created in preceding example, use following commands.

#tar -xvf /root/practicelab/backup.tar.gz
#tar -xvf /root/practicelab/1-backup.tar.gz
#tar -xvf /root/practicelab/2-backup.tar.gz

By default, tar command restores the backup in the directory from which it is executed. For example, if we execute above commands from the directory /home/test, backup will be restored in the directory /home/test. Or if we run the above commands from the directory /root/practicelab/restore, backup will be restored in the directory /root/practicelab/restore.

In order to restore the backup in the correct directory, we must have to be in the correct directory before executing the tar command. For example, if we want to restore the backup in directory /var, we have to change current directory to /var before executing the tar command.

Alternatively, we can specify the restore location with the option -C. For example, following commands restore backup in the directory /root/practicelab/restore in preceding example.

#tar -xvf /root/practicelab/backup.tar.gz -C /root/practicelab/restore
#tar -xvf /root/practicelab/1-backup.tar.gz -C /root/practicelab/restore
#tar -xvf /root/practicelab/2-backup.tar.gz -C /root/practicelab/restore

Since above commands use absolute path to specify the both source (location where the backup file is stored) and destination (location where you want to restore the backup), you can run these commands from any directory.

restoring incremental backup

That’s all for this tutorial. If you like this tutorial, please don’t forget to share it through your favorite social network.

Advertisements

ComputerNetworkingNotes Linux Tutorials Create and Restore Incremental backups in Linux with tar