How to Create Backup with tar Command in Linux

If you are looking for free command line backup tools on Linux systems then tar command is the solution for you. Tar command can create backup of your application, configurations files and folders of the system.

Tar stands for ‘tape archive’ and can archive multiple files and directories into a tar file. This tar can also be compressed using gzip and bzip2 compression utility.

In this post, we will demonstrate how to create backup with tar command in Linux.

How to create tar backup file?

To create a tar backup file, first identify the files and folders that would be part of your backup. Let’s assume we want to take backup of /home/rahul/etc and /opt folder. Run following tar command,

sudo tar -cv system-back.tar /home/rahul /etc /opt

This will create a tar ball in the present working directory. In above tar command, we have used following options

  • c – Create new archive
  • v – display verbose output while creating tar file
  • f – archive file name
  • p – preserve permissions

As you have seen that we have not used any compression options to compress tar file. So, to compress the tar backup file during the archive use -z ( gzip compression) or -j (bzip2 compression)

Creating tar backup along with gzip compression

$ sudo tar -zcvpf system-back.tgz /home/rahul /etc /opt

Creating tar backup along with bzip compression

$ sudo tar -jcvpf system-back.tbz2 /home/linuxtechi /etc /opt

How to append a file to tar backup?

To append a file to the existing tar backup file, use ‘-r’ option, complete command would like below:

$ sudo tar -rvf system-back.tar /root/app.yaml

How to exclude file while creating tar backup?

To exclude a file while creating tar backup, use ‘-X’ option followed by the exclude file. To use exclude feature we must create a exclude file which will have file name to be excluded.

$ cat exclude.txt

Run following command to exclude files mentioned in exclude.txt while creating tar backup of /etc

$ sudo tar -X exclude.txt -zcpvf etc-backup.tgz /etc

That’s all from this post, I hope you have found it informative. Please do share share your feedback and queries in below comments section below.

Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post

Gigabyte is the latest major PC maker to suffer a ransomware attack

Next Post

30 Best Free Places To Learn Coding in 2021

Related Posts