Ever try something, it didn’t work, and you didn’t make a backup first?
One of the key rules for working as a system administrator is always to make a backup. You never know when you might need it. In my personal experience, it has saved me more times than I can count. It’s a common practice to complete and sometimes makes a difference in your finished work.
The tar
utility has a ton of options and available usage. Tar stands for tape archive and allows you to create backups using: tar
, gzip
, and bzip
. It compresses files and directories into an archive file, known as a tarball. This command is one of the most widely-used commands for this purpose. Also, the tarball is easily movable from one server to the next.
How to create a tar backup
In this example, we create a backup called backup.tar of the directory /home/user
.
# tar -cvf backup.tar /home/user
Let’s break down these options:
-c
- Create the archive
-v
- Show the process verbosely
-f
- Name the archive
How to create a tar.gz
backup
In this example, we create a gzip
archive backup called backup.tar.gz of the directory /home/user
.
# tar -cvzf backup.tar.gz /home/user
Let’s break down these options:
-c
- Create the archive
-v
- Show the process verbosely
-f
- Name the archive
-z
- Compressed gzip archive file
How to exclude files when creating a tar backup
In this example, we create a gzip
backup called backup.tar.gz, but exclude the files named file.txt and file.sh by using the --exclude [filename]
option.
# tar --exclude file.txt --exclude file.sh -cvzf backup.tar.gz
How to extract content from a tar (.gz) backup
In this example, we extract content from a gzip
backup backup.tar.gz, specifically a file called file.txt from the directory /backup/directory
in the gzip
file.
# tar -xvzf backup.tar.gz /backup/directory/file.txt
Let’s break down these options:
-x
- Extract the content
-v
- Show the process verbosely
-f
- Name the archive
-z
- compressed gzip archive file
How to list contents of a tar(.gz) backup
In this example, we list the contents from a gzip
backup backup.tar.gz without extracting it.
# tar -ztvf backup.tar.gz
Let’s break down these options:
-t
- List the contents
-v
- Show the process verbosely
-f
- Name the archive
-z
- compressed gzip archive file
How to use the wildcard option
In this example, we use the wildcard option on a backup backup.tar. Wildcards allow you to select files without having a specific search for keywords. This is helpful in situations where you are trying to locate something but do not want to specify the name or want to add in all options matching that particular search.
# tar -cf backup.tar “*.xml”
Let’s break down these options:
-c
- Create the backup
-f
- Name the archive
How to append or add files to a backup
In this example, we add onto a backup backup.tar. This allows you to add additional files to the pre-existing backup backup.tar.
# tar -rvf backup.tar /path/to/file.xml
Let’s break down these options:
-r
- Append to archive
-v
- Verbose output
-f
- Name the file
How to split a backup into smaller backups
In this example, we split the existing backup into smaller archived files. You can pipe
the tar
command into the split
command.
# tar cvf - /dir | split --bytes=200MB - backup.tar
Let’s break down these options:
-c
- Create the archive
-v
- Verbose output
-f
- Name the file
In this example, the dir/
is the directory that you want to split the backup content from. We are making 200MB backups from the /dir
folder.
How to check the integrity of a tar.gz backup
In this example, we check the integrity of an existing tar
archive.
To test the gzip
file is not corrupt:
#gunzip -t backup.tar.gz
To test the tar
file content's integrity:
#gunzip -c backup.tar.gz | tar t > /dev/null
OR
#tar -tvWF backup.tar
Let’s break down these options:
-W
- Verify an archive file
-t
- List files of archived file
-v
- Verbose output
Use pipes and greps to locate content
In this example, we use pipes
and greps
to locate content. The best option is already made for you. Zgrep
can be utilized for gzip
archives.
#zgrep <keyword> backup.tar.gz
You can also use the zcat
command. This shows the content of the archive, then pipes
that output to a grep
.
#zcat backup.tar.gz | grep <keyword>
Egrep
is a great one to use just for regular file types.
Wrap up
Tar
has a lot of things you can do with it. It allows you to create the archive and manage it easily with the available tools in your terminal. If tar
is not installed, you can do so depending on your operating system. Tar
is useful in several different cases. As a system administrator, I created plenty of backups and recovered from some of them, too. It’s always safer to make a backup of a file or directory before making changes, in case you need to revert to the original setup. Having that security is something we all need.
[ Good backups are an important part of any security and disaster recovery plan. Want to learn more? Check out the IT security and compliance checklist. ]
Edited 3/24/23 to correct a code typo.
About the author
I currently work as a Cloud Support Supervisor for Acronis . I have worked with Linux and OpenSource tools for a decade, constantly wanting to make new resolutions for obstacles and always training others on improving systems as a systems administrator. I’ve helped manage and maintain elaborate servers and websites over the years. In my spare time, I enjoy spending time with my dogs, reading, and playing video games.
More like this
Browse by channel
Automation
The latest on IT automation for tech, teams, and environments
Artificial intelligence
Updates on the platforms that free customers to run AI workloads anywhere
Open hybrid cloud
Explore how we build a more flexible future with hybrid cloud
Security
The latest on how we reduce risks across environments and technologies
Edge computing
Updates on the platforms that simplify operations at the edge
Infrastructure
The latest on the world’s leading enterprise Linux platform
Applications
Inside our solutions to the toughest application challenges
Original shows
Entertaining stories from the makers and leaders in enterprise tech
Products
- Red Hat Enterprise Linux
- Red Hat OpenShift
- Red Hat Ansible Automation Platform
- Cloud services
- See all products
Tools
- Training and certification
- My account
- Customer support
- Developer resources
- Find a partner
- Red Hat Ecosystem Catalog
- Red Hat value calculator
- Documentation
Try, buy, & sell
Communicate
About Red Hat
We’re the world’s leading provider of enterprise open source solutions—including Linux, cloud, container, and Kubernetes. We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.
Select a language
Red Hat legal and privacy links
- About Red Hat
- Jobs
- Events
- Locations
- Contact Red Hat
- Red Hat Blog
- Diversity, equity, and inclusion
- Cool Stuff Store
- Red Hat Summit