Create a tarball backup

29 Sep 2024

Creating a tarball backup is a common practice in Linux for archiving files and directories. A tarball, typically with a .tar or .tar.gz extension, is created using the tar command. In this tutorial, we will cover how to back up a directory, back up multiple files, and decompress a tarball.

Backing Up a Directory

To create a tarball backup of a directory, you can use the following command:

tar -cvzf backup-directory.tar.gz /path/to/directory

Explanation of the options:

Example:

tar -cvzf my_backup.tar.gz /home/user/my_directory

This command will create a compressed tarball named my_backup.tar.gz containing the contents of my_directory.

Backing Up Multiple Files

To back up multiple specific files into a single tarball, you can specify the file paths directly in the command:

tar -cvzf backup-files.tar.gz /path/to/file1 /path/to/file2 /path/to/file3

Example:

tar -cvzf my_files_backup.tar.gz /home/user/file1.txt /home/user/file2.txt /home/user/file3.txt

This command will create a tarball named my_files_backup.tar.gz that includes file1.txt, file2.txt, and file3.txt.

Decompressing a Tarball

To decompress a .tar.gz file, use the following command:

tar -xvzf backup-directory.tar.gz

Explanation of the options:

Example:

tar -xvzf my_backup.tar.gz

This command will extract the contents of my_backup.tar.gz into the current directory.

Summary

Creating a tarball backup of a directory:

tar -cvzf my_backup.tar.gz /path/to/directory

Creating a tarball backup of multiple files:

tar -cvzf my_files_backup.tar.gz /path/to/file1 /path/to/file2 /path/to/file3

Decompressing a tarball:

tar -xvzf my_backup.tar.gz

By using these commands, you can effectively manage your backups in Linux. Happy backing up!