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.
저자 소개
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 than meets the eye: Behind the scenes of Red Hat Enterprise Linux 10 (Part 4)
Why should your organization standardize on Red Hat Enterprise Linux today?
What Is Product Security? | Compiler
Technically Speaking | Security for the AI supply chain
채널별 검색
오토메이션
기술, 팀, 인프라를 위한 IT 자동화 최신 동향
인공지능
고객이 어디서나 AI 워크로드를 실행할 수 있도록 지원하는 플랫폼 업데이트
오픈 하이브리드 클라우드
하이브리드 클라우드로 더욱 유연한 미래를 구축하는 방법을 알아보세요
보안
환경과 기술 전반에 걸쳐 리스크를 감소하는 방법에 대한 최신 정보
엣지 컴퓨팅
엣지에서의 운영을 단순화하는 플랫폼 업데이트
인프라
세계적으로 인정받은 기업용 Linux 플랫폼에 대한 최신 정보
애플리케이션
복잡한 애플리케이션에 대한 솔루션 더 보기
가상화
온프레미스와 클라우드 환경에서 워크로드를 유연하게 운영하기 위한 엔터프라이즈 가상화의 미래