How to Create and Restore Incremental Backups with Tar on Linux
Regularly backing up the system data is an essential part of any system administrator. There are two types of backup, incremental and full backup. Incremental backup is the advanced backup type that backup only the data that is created or modified since the last backup has been taken. This will save backup time and also save disk space.
There are several backup tools available in Linux. Among them, tar is one of the best and popular command-line tool used by System administrators to take a full and incremental backup.
In this post, we will show you how to create and restore the incremental backup with tar on Linux.
Create Data Files
For the purpose of this tutorial, we will create some files to perform the incremental backup.
First, create a data directory with the following command:
mkdir -p /backup/data
Next, create some files with the following command:
cd /backup/data cat /etc/sysctl.conf > test1.txt cat /etc/sysctl.conf > test2.txt cat /etc/sysctl.conf > test3.txt cat /etc/sysctl.conf > test4.txt cat /etc/sysctl.conf > test5.txt cat /etc/sysctl.conf > test6.txt
Create Level 0 Incremental Backup
A level 0 incremental backup is the base copy of all source files.
Let's take an example to create an incremental backup of the data directory.
In this example, we will use the following argument:
data: We will take a backup of this directory.
/backup/data.tgz: We will compress the data directory in tgz format and save it inside /backup directory.
/backup/data.sngz: We will create a level 0 incremental backup and save it at /backup/data.sngz
Run the following command to perform the incremental backup:
cd /backup tar --verbose --verbose --create --gzip --listed-incremental=/backup/data.sngz --file=/backup/data.tgz data
You should get the following output:
tar: data: Directory is new drwxr-xr-x root/root 0 2021-03-04 11:11 data/ -rw-r--r-- root/root 2084 2021-03-04 11:10 data/test1.txt -rw-r--r-- root/root 2084 2021-03-04 11:10 data/test2.txt -rw-r--r-- root/root 2084 2021-03-04 11:10 data/test3.txt -rw-r--r-- root/root 2084 2021-03-04 11:10 data/test4.txt -rw-r--r-- root/root 2084 2021-03-04 11:10 data/test5.txt -rw-r--r-- root/root 2084 2021-03-04 11:11 data/test6.txt
Now, list the content of the incremental backup data from data.tgz file using the following command:
tar --list --incremental --verbose --verbose --file /backup/data.tgz
You should get the following output:
drwxr-xr-x root/root 67 2021-03-04 11:11 data/ Y test1.txt Y test2.txt Y test3.txt Y test4.txt Y test5.txt Y test6.txt -rw-r--r-- root/root 2084 2021-03-04 11:10 data/test1.txt -rw-r--r-- root/root 2084 2021-03-04 11:10 data/test2.txt -rw-r--r-- root/root 2084 2021-03-04 11:10 data/test3.txt -rw-r--r-- root/root 2084 2021-03-04 11:10 data/test4.txt -rw-r--r-- root/root 2084 2021-03-04 11:10 data/test5.txt -rw-r--r-- root/root 2084 2021-03-04 11:11 data/test6.txt
At above output indicate a list of all files and directory included in the backup archive. The letter Y indicates if the file is present in the archive.
Create Level 1 Incremental Backup
In this section, we will create a level 1 incremental backup. We will use data.sngz snapshot file to perform this backup and create a new backup archive file named data1.tgz.
Before creating the incremental backup, delete one file and create a new file inside the data directory.
rm -rf /backup/data/test2.txt cat /etc/sysctl.conf > /backup/data/test7.txt
Run the following command to perfrom the incremental backup:
cd /backup tar --verbose --verbose --create --gzip --listed-incremental=/backup/data.sngz --file=/backup/data1.tgz data
You should get the following output:
drwxr-xr-x root/root 0 2021-03-04 11:30 data/ -rw-r--r-- root/root 2084 2021-03-04 11:30 data/test7.txt
As you can see, the above command will backup only the recent change that we have made after taking a level 0 backup.
You can check it with the following command:
tar --list --incremental --verbose --verbose --file /backup/data1.tgz
You should get the following output:
drwxr-xr-x root/root 67 2021-03-04 11:30 data/ N test1.txt N test3.txt N test4.txt N test5.txt N test6.txt Y test7.txt -rw-r--r-- root/root 2084 2021-03-04 11:30 data/test7.txt
In the above output, the letter N indicates if the file is not included in the archive.
Restore the Backup with Tar Incremental Backup
In order to perform restoration operations, you will need to remove the data directory. You can delete it with the following command:
rm -rf /backup/data
To restore the data directory, first you will need to extract the data directory from level 0 backup. Because level 0 backup is the base of the data directory.
You can do it with the following command:
cd /backup tar --extract --listed-incremental=/dev/null --file data.tgz
You can now check the restored files with the following command:
ls -l data
You should get the following output:
-rw-r--r-- 1 root root 2084 Mar 4 11:10 test1.txt -rw-r--r-- 1 root root 2084 Mar 4 11:10 test2.txt -rw-r--r-- 1 root root 2084 Mar 4 11:10 test3.txt -rw-r--r-- 1 root root 2084 Mar 4 11:10 test4.txt -rw-r--r-- 1 root root 2084 Mar 4 11:10 test5.txt -rw-r--r-- 1 root root 2084 Mar 4 11:11 test6.txt
Now, extract the data from level 1 incremental backup using the following command:
cd /backup tar --extract --listed-incremental=/dev/null --file data1.tgz
You can now verify the data directory using the following command:
ls -l data
You will see you all restored data in the following output:
-rw-r--r-- 1 root root 2084 Mar 4 11:10 test1.txt -rw-r--r-- 1 root root 2084 Mar 4 11:10 test3.txt -rw-r--r-- 1 root root 2084 Mar 4 11:10 test4.txt -rw-r--r-- 1 root root 2084 Mar 4 11:10 test5.txt -rw-r--r-- 1 root root 2084 Mar 4 11:11 test6.txt -rw-r--r-- 1 root root 2084 Mar 4 11:30 test7.txt
Conclusion
In the above guide, you learned how to create an incremental backup using the tar command. You also learned how to perform restore operation using the tar command. I hope this incremental backup guide will save your lot of time and disk space.
Thank you for helping us improve!