How To Backup And Restore Files Using Tar In Linux/Unix?


A system backup is a process in which the state, files, and data of a computer system are copied and stored somewhere. Later if primary data is corrupted, deleted, or lost then it can be used to recover the original data.

For useful recovery, a backup must be created when the system is in a consistent state. In this article, we will discuss backing up and restoring a system using tar. You can use this process in any Linux/Unix system.

What is Tar?

The name tar is derived from the tape archive. It is a file archiver that creates an archive file from one or more than one file or directories. A tar archive is known as the tarball. In Linux or Unix, tar command can be used to create a backup and restore the system whenever needed.

Create Backup

Unlike Windows, Linux doesn’t restrict root access to anything, so you can archive every single file on a partition in a tar file. To create a backup you need to become root. Open your terminal and then enter the following command –

sudo su

Enter your password if asked. Now navigate to the location where you want to save the backup of your system. The location includes remote drives, external drives like USB, etc. We will keep it in the root directory of the system. So change your current working directory accordingly –

cd /

To create full system backup use the following command in your terminal –

tar cvpzf backup.tar.gz --exclude=/proc --exclude=/lost+found --exclude=/backup.tgz --exclude=/mnt --exclude=/tmp --exclude=/sys /

where,

c– To create a new backup archive
v– tar will print what it doing on screen
p– preserve the permission of files
z– compress the backup file with ‘gzip’
f – To specify the filename backup.tgz in this example

The --exclude option instructs the tar what directories not to backup. Don’t forget to exclude the backup.tgz file otherwise, you will get weird results. Alternatively, you can use Bzip2 to compress your backup. This means higher compression but at a slower speed. Now If you want backup with Bzip2 compression use j instead of z command in the options. Use the command as it is given below –

tar cvpjf backup.tar.bz2 --exclude=/proc --exclude=/lost+found --exclude=/backup.tar.bz2 --exclude=/mnt --exclude=/tmp --exclude=/sys /

Note: At the end of the process you might get a message along the lines of ‘tar: Error exit delayed from previous errors’ or something, but in most cases, you can just ignore that.

When it gets done, the backup file will be saved in the root of the filesystem. now you can move it anywhere if you want.

Restoring from created backup

To restore, once again make sure you are root and the backup file is in the root of the filesystem. Be careful, this will overwrite every single file on your partition with the one in the archive. Now to restore execute the following command in your terminal-

tar xvpfz backup.tar.gz -C /

Or if Bzip2 is used for the compression then use –

 tar xvpfj backup.tar.bz2 -C /

Here -C tells tar to extract to a specific directory.

Now before you do anything first recreate the directories you excluded. By using the following command –

mkdir /proc /sys /mnt /tmp

Now once you reboot everything should be in a way when you made the backup. I hope the article is useful to you if you have any questions regarding the topic you can write to us in the comments below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.