Incremental backups with tar

When you have to backup a large amount of data, simple backups are a waste of time and storage space. In this situation, the most suitable option is to backup data incrementally. Although there are different methods, today I’ll explain how I use tar to keep my files safe.

With tar, an incremental backup consists of multiple files. The first one is a full dump of the data. This is a simple backup of the files as you would normally do. Simultaneously, the first time you perform an incremental backup it will be created a snapshot file. A snapshot file contains information related to the state of the data in a specific moment. With this information, later you can detect which files have changed and which ones have been added or deleted.

Once the first backup is done, you will have both a full dump and a snapshot file. The subsequent backups will only update the snapshot file and create additional tar files containing the information needed to recover the state of your data.

In order to backup files incrementally, you have to use the tar command with the following parameters:

tar -cvvf backup.tar -g snapshot_file backup_data

if snapshot_file does not exist, it will be created and backup.tar will be a full dump (or level 0 backup) of backup_data. When you decide to backup backup_data again, be sure you use another tar file name such as backup.1.tar. If you don’t do that and use the same filename, you will overwrite the full dump backup and it will be impossible to recover your data!

After many backups, you will have the following files: snapshot_file, backup.tar, backup.1.tar, backup.2.tar, backup.3.tar… being backup.tar a level 0 backup, backup.1.tar a level 1 backup, backup.2.tar a level 2 backup…

To recover your backup, you have to untar in that order backup.tar, backup.1.tar, backup.2.tar… In that case, there is no need to use the snapshot file, but you should indicate to tar that it is an incremental backup with the -g parameter:

tar -xvvf backup.tar -g /dev/null

tar -xvvf backup.1.tar -g /dev/null

tar -xvvf backup.2.tar -g /dev/null

If you want to keep multiple level 1 backups, you must keep the snapshot file created after the level 0 backup is created:

tar -cvvf backup.tar -g snapshot_file backup_data

cp snapshot_file snapshot_file.0

tar -cvvf backup.1.tar -g snapshot_file backup_data

tar -cvvf another_backup.1.tar -g snapshot_file.0 backup_data

Now, both backup.1.tar and another_backup.1.tar will be level 1 backups. Therefore, if you want to recover your files with another_backup.1.tar, there is no need to untar backup.1.tar. It’s useful if you want to keep different backups and later choose the one you think is more suitable.

As I mentioned before, there are other ways to keep backups of your data. Some of them are more efficient, but tar offers a great functionality if you keep your backups recorded on tapes, which nowadays are cheaper than other solutions.

This entry was posted in Code and tagged , , , . Bookmark the permalink.

Comments are closed.