System administrators need to regularly create system backup. There are two types of system backup – full backup and incremental backup. In full backup, you create a backup of all data, every time. In incremental backup, you create backup of only those data that have been modified or newly created since last backup. Incremental backups are useful as your data grows over time. They save a lot of space, resources & time. In this article, we will learn how to create incremental backup in Linux.
How to Create Incremental Backup in Linux
Here are the different steps to create incremental backup in Linux. We will use tar command for this purpose. You can also use other tools if you want.
1. Create data files
First we will create some dummy data files for backup. Open terminal and run the following command to create folder for data files.
$ mkdir -p /backup/data
Run the following command to create sample data files.
$ cd /backup/data $ cat /etc/sysctl.conf > test1.txt $ cat /etc/sysctl.conf > test2.txt $ cat /etc/sysctl.conf > test3.txt
2. Create Full Backup
Even when you create an incremental backup, you need to first take a full backup of your data. It is also known as Level 0 Incremental backup. We will compress the data files in .tgz format and save it as /backup/data.tgz and create a backup of this file at /backup/data.sngz.
$ cd /backup $ tar --verbose --verbose --create --gzip --listed-incremental=/backup/data.sngz --file=/backup/data.tgz data
Check the backup with the following command.
$ tar --list --incremental --verbose --verbose --file /backup/data.tgz
You will see the following output which lists files present in backup. In it, Y indicates that the file is present.
drwxr-xr-x root/root 67 2021-11-04 11:11 data/ Y test1.txt Y test2.txt Y test3.txt
3. Create Incremental Backup
Next, before creating incremental backup, we delete and add a file in our folder.
$ rm -rf /backup/data/test2.txt $ cat /etc/sysctl.conf > /backup/data/test4.txt
Next, we run the following commands to create incremental backup, also known as Level 1 incremental backup. In this case, we will store compressed incremental data at /backup/data1.tgz and incremental backup at /backup/data.sngz
$ cd /backup $ tar --verbose --verbose --create --gzip --listed-incremental=/backup/data.sngz --file=/backup/data1.tgz data
Run the following command to check the incremental backup file.
$ tar --list --incremental --verbose --verbose --file /backup/data1.tgz
You will see the following output which lists all the files present in incremental backup. In it, N indicates that the file is not present and Y indicates that the file is present.
drwxr-xr-x root/root 67 2021-12-04 11:30 data/ N test1.txt N test3.txt Y test4.txt
4. Restore Backup with Incremental Backup
Before we restore the data from our backup files, we will delete the original files.
$ rm -rf /backup/data
Next, we will restore data from our full backup first, since it is the base source of all backups.
$ cd /backup $ tar --extract --listed-incremental=/dev/null --file data.tgz
Now if you check the /backup/data folder you will see the following output.
$ ls -l data -rw-r--r-- 1 root root 2084 Nov 4 11:10 test1.txt -rw-r--r-- 1 root root 2084 Nov 4 11:10 test2.txt -rw-r--r-- 1 root root 2084 Nov 4 11:10 test3.txt
Next, we restore data from incremental backup file.
$ cd /backup $ tar --extract --listed-incremental=/dev/null --file data1.tgz
You will see the following information when check the /backup/data folder.
ls -l data -rw-r--r-- 1 root root 2084 Nov 4 11:10 test1.txt -rw-r--r-- 1 root root 2084 Nov 4 11:10 test3.txt -rw-r--r-- 1 root root 2084 Nov 4 11:10 test4.txt
Now you will see that file test2.txt has been automatically removed and test4.txt has been newly added, indicating that the incremental backup has been restored.
In this article, we have learnt how to create incremental backup in Linux. You can also use other utilities such as zip, rsync, etc. in order to perform backup, as per your requirement. It can be tedious to do a full backup every time. Incremental backups are useful in such cases where you need to regularly backup your data.
Also read:
How to Improve Ubuntu Speed & Performance
How to Embed PDF in HTML
How to Run Multiple Python Files One After the Other
How to Merge PDF Files Using Python
How to Do Incremental Backup in MySQL
Related posts:
![](https://fedingo.com/wp-content/uploads/2024/05/db908c05ad732bd197c1eaad1c6675d2.jpeg)
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.