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.
Über den Autor
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.
Ähnliche Einträge
Deploy Confidential Computing on AWS Nitro Enclaves with Red Hat Enterprise Linux
Red Hat OpenShift sandboxed containers 1.11 and Red Hat build of Trustee 1.0 accelerate confidential computing across the hybrid cloud
What Is Product Security? | Compiler
Technically Speaking | Security for the AI supply chain
Nach Thema durchsuchen
Automatisierung
Das Neueste zum Thema IT-Automatisierung für Technologien, Teams und Umgebungen
Künstliche Intelligenz
Erfahren Sie das Neueste von den Plattformen, die es Kunden ermöglichen, KI-Workloads beliebig auszuführen
Open Hybrid Cloud
Erfahren Sie, wie wir eine flexiblere Zukunft mit Hybrid Clouds schaffen.
Sicherheit
Erfahren Sie, wie wir Risiken in verschiedenen Umgebungen und Technologien reduzieren
Edge Computing
Erfahren Sie das Neueste von den Plattformen, die die Operations am Edge vereinfachen
Infrastruktur
Erfahren Sie das Neueste von der weltweit führenden Linux-Plattform für Unternehmen
Anwendungen
Entdecken Sie unsere Lösungen für komplexe Herausforderungen bei Anwendungen
Virtualisierung
Erfahren Sie das Neueste über die Virtualisierung von Workloads in Cloud- oder On-Premise-Umgebungen