Lesson 4 | The tar tape backup command |
Objective | Use the tar command to back up and restore files and directories. |
The tar
command creates a single archive file (commonly with .tar
, .tar.gz
, or .tar.bz2
extensions) from files and directories.
Basic Backup Command
tar -cvf backup.tar /path/to/directory
-c
: Create a new archive.-v
: Verbose mode, shows progress.-f
: Specifies the name of the archive file./path/to/directory
: The files or directories to back up.Compress the Backup
You can compress the archive to save space:
gzip
:
tar -czvf backup.tar.gz /path/to/directory
Use the -z
option for gzip compression.
bzip2
:
tar -cjvf backup.tar.bz2 /path/to/directory
Use the -j
option for bzip2 compression.
Example
Backup /home/user/docs
to a compressed file named docs_backup.tar.gz
:
tar -czvf docs_backup.tar.gz /home/user/docs
To extract files from a tar archive:
Extract from a Non-Compressed Archive
tar -xvf backup.tar
-x
: Extract files.-v
: Verbose mode.-f
: Specifies the archive file to extract.Extract from a Gzip-Compressed Archive
tar -xzvf backup.tar.gz
Use the -z
option for gzip-compressed files.
Extract from a Bzip2-Compressed Archive
tar -xjvf backup.tar.bz2
Use the -j
option for bzip2-compressed files.
Extract to a Specific Location
You can specify a directory where the files should be restored:
tar -xvf backup.tar -C /path/to/restore
-C
: Change to the specified directory before extracting.
Example
Restore the contents of docs_backup.tar.gz
to /home/user/restored_docs
:
mkdir -p /home/user/restored_docs tar -xzvf docs_backup.tar.gz -C /home/user/restored_docs
To view the contents of an archive without extracting it:
tar -tvf backup.tar
-t
: Lists files in the archive.
For large backups, you can use incremental backup options by combining tar
with the --listed-incremental
option:
tar --listed-incremental=snapshot.file -cvf incremental_backup.tar /path/to/directory
To verify the integrity of an archive:
tar -tvf backup.tar
crontab -e
0 2 * * * tar -czvf /backup/backup_$(date +\%F).tar.gz /path/to/directory
tar
command is used to create archives and extract files and directories from tar archives. You can use tar
as an expedient and simple method for storing an entire file system.
You can then use the tar
file as a backup, or you can transfer and extract the file onto another machine.
tar
is very useful for generating daily backups of critical files (such as a current project) or for archiving data, such as mail for an entire organization.
tar
"flattens" files and directories into sequential archives that can go to tape or disk. Linux supports
standard commands for tape backup.
These include
c
: Create an archivex
: Extract files and file systems from a tar
archivez
: Use gzip compressionf
: Specify the file system on which to store the archivetar
(Tape Archive)
The `tar` command is widely used for creating, extracting, and managing archive files.
-v
(Verbose): Provides detailed output of the files being archived or extracted. This is the most commonly used verbose option.
tar -cvf archive.tar /path/to/files
Output: Displays the list of files being archived.
--verbose
: Equivalent to `-v`, but uses the long-form option syntax.
tar --create --verbose --file=archive.tar /path/to/files
gzip
and bzip2
These are compression utilities commonly used alongside `tar`.
-v
(Verbose): Shows the compression process and details about the file being compressed or decompressed.
gzip
:
gzip -v file.txt
Output: Displays the compression ratio and the resulting file name.
bzip2
:
bzip2 -v file.txt
zip
The `zip` command creates compressed ZIP archives.
-v
(Verbose): Lists detailed information about the files being added to the archive.
zip -v archive.zip /path/to/files
--verbose
: Provides detailed output in a long-form syntax.
zip --verbose archive.zip /path/to/files
cpio
The `cpio` command is another archive tool often used with verbose options.
-v
(Verbose): Lists the files being archived.
find . | cpio -ov > archive.cpio
-tvf
or similar options to inspect the contents of an archive without extracting it.tar -czvf archive.tar.gz /path/to/files
tar -xjvf archive.tar.bz2
tar -tvf archive.tar
# tar -clpMzvf /dev/st0 /home
# tar -dlpMzvf /dev/st0 /home
# tar -xlpMzvf /dev/st0 /home
/project.tar.gzthat contains the files and subdirectories in the /project directory?
tar czf /project.tar.gz /projector
tar zcf /project.tar.gz /project
dump
command.