In this chapter you will learn how to back up and restore your data with Linux.
Objectives: In this chapter, future Linux administrators will learn how to:
use the tar and cpio command to make a backup; check their backups and restore data; compress or decompress their backups.
backup, restore, compression
Knowledge: Complexity:
Reading time: 40 minutes
Note
Throughout this chapter the command structures use "device" to specify both a target location for backup, and the source location when restoring. The device can be either external media or a local file. You should get a feel for this as the chapter unfolds, but you can always refer back to this note for clarification if you need to.
The backup will answer a need to conserve and restore data in a sure and effective way.
The backup allows you to protect yourself from the following:
Destruction: voluntary or involuntary. Human or technical. Virus, ...
Deletion: voluntary or involuntary. Human or technical. Virus, ...
Integrity: data becomes unusable.
No system is infallible, no human is infallible, so to avoid losing data, it must be backed up to be able to restore after a problem.
The backup media should be kept in another room (or building) than the server so that a disaster does not destroy the server and the backups.
In addition, the administrator must regularly check that the media are still readable.
Depending on the utilities available, it will be possible to perform several types of restorations.
Complete restoration: trees, ...
Selective restoration: part of tree, files, ...
It is possible to restore a whole backup, but it is also possible to restore only a part of it. However, when restoring a directory, the files created after the backup are not deleted.
Tip
To recover a directory as it was at the time of the backup, it is necessary to completely delete its contents before launching the restoration.
The use of a naming convention makes it possible to quickly target the contents of a backup file and thus avoid hazardous restorations.
name of the directory;
utility used;
options used;
date.
Tip
The name of the backup must be an explicit name.
Note
The notion of extension under Linux does not exist. In other words, our use of extensions here is for the human operator. If the systems administrator sees a .tar.gz or .tgz file extension, for instance, then he knows how to deal with the file.
The tar command allows saving on several successive media (multi-volume options).
It is possible to extract all or part of a backup.
tar implicitly backs up in relative mode even if the path of the information to be backed up is mentioned in absolute mode. However, backups and restores in absolute mode are possible.
Before a restoration, it is important to take time to think about and determine the most appropriate method to avoid mistakes.
Restorations are usually performed after a problem has occurred that needs to be resolved quickly. A poor restoration can, in some cases, make the situation worse.
The default utility for creating backups on UNIX systems is the tar command. These backups can be compressed by bzip2, xz, lzip, lzma, lzop, gzip, compress or zstd.
tar allows you to extract a single file or a directory from a backup, view its contents or validate its integrity.
Creating a non-compressed backup explicitly in absolute mode is done with the cvfP keys:
$ tar c[vf]P [device] [file(s)]
Example:
[root]# tar cvfP /backups/home.133.P.tar /home/
Key
Description
P
Creates a backup in absolute mode.
Warning
With the P key, the path of the files to be backed up must be entered as absolute. If the two conditions (key P and path absolute) are not indicated, the backup is in relative mode.
Creating a compressed backup with gzip is done with the cvfz keys:
$ tar cvzf backup.tar.gz dirname/
Key
Description
z
Compresses the backup in gzip.
Note
The .tgz extension is an equivalent extension to .tar.gz.
Note
Keeping the cvf (tvf or xvf) keys unchanged for all backup operations and simply adding the compression key to the end of the keys makes the command easier to understand (e.g., cvfz or cvfj, etc.).
Viewing the contents of a backup without extracting it is possible.
tar t[key(s)] [device]
Key
Description
t
Displays the content of a backup (compressed or not).
Examples:
$ tar tvf backup.tar
$ tar tvfz backup.tar.gz
$ tar tvfj backup.tar.bz2
When the number of files in a backup becomes large, it is possible to pipe the result of the tar command to a pager (more, less, most, etc.):
$ tar tvf backup.tar | less
Tip
To list or retrieve the contents of a backup, it is not necessary to mention the compression algorithm used when the backup was created. That is, a tar tvf is equivalent to tar tvfj, to read the contents, and a tar xvf is equivalent to tar xvfj, to extract.
The integrity of a backup can be tested with the W key at the time of its creation:
$ tar cvfW file_name.tar dir/
The integrity of a backup can be tested with the key d after its creation:
$ tar vfd file_name.tar dir/
Tip
By adding a second v to the previous key, you will get the list of archived files as well as the differences between the archived files and those present in the file system.
Extract (untar) a *.tar backup is done with the xvf keys:
Extract the etc/exports file from the /savings/etc.133.tar backup into the etc directory of the active directory:
$ tar xvf /backups/etc.133.tar etc/exports
Extract all files from the compressed backup /backups/home.133.tar.bz2 into the active directory:
[root]# tar xvfj /backups/home.133.tar.bz2
Extract all files from the backup /backups/etc.133.P.tar to their original directory:
$ tar xvfP /backups/etc.133.P.tar
Warning
Go to the right place.
Check the contents of the backup.
Key
Description
x
Extracts files from the backup, compressed or not.
Extracting a tar-gzipped (*.tar.gz) backup is done with the xvfz keys:
$ tar xvfz backup.tar.gz
Extracting a tar-bzipped (*.tar.bz2) backup is done with the xvfj keys:
$ tar xvfj backup.tar.bz2
Tip
To extract or list the contents of a backup, it is not necessary to mention the compression algorithm used to create the backup. That is, a tar xvf is equivalent to tar xvfj, to extract the contents, and a tar tvf is equivalent to tar tvfj, to list.
Warning
To restore the files in their original directory (key P of a tar xvf), you must have generated the backup with the absolute path. That is, with the P key of a tar cvf.
To extract a specific file from a tar backup, specify the name of that file at the end of the tar xvf command.
$ tar xvf backup.tar /path/to/file
The previous command extracts only the /path/to/file file from the backup.tar backup. This file will be restored to the /path/to/ directory created, or already present, in the active directory.
$ tar xvfz backup.tar.gz /path/to/file
$ tar xvfj backup.tar.bz2 /path/to/file
The result of the find command is sent as input to the cpio command via a pipe (character |, AltGr + 6).
Here, the find /etc command returns a list of files corresponding to the contents of the /etc directory (recursively) to the cpio command, which performs the backup.
Do not forget the > sign when saving or the F save_name_cpio.
There is no option, unlike the tar command, to save and compress at the same time.
So it is done in two steps: saving and compressing.
The syntax of the first method is easier to understand and remember, because it is done in two steps.
For the first method, the backup file is automatically renamed by the gzip utility which adds .gz to the end of the file name. Similarly, the bzip2 utility automatically adds .bz2.
Restores only the files whose name is contained in file.
--make-directories or -d
Rebuilds the missing tree structure.
-u
Replaces all files even if they exist.
--no-absolute-filenames
Allows to restore a backup made in absolute mode in a relative way.
Warning
By default, at the time of restoration, files on the disk whose last modification date is more recent or equal to the date of the backup are not restored (in order to avoid overwriting recent information with older information).
The u option, on the other hand, allows you to restore older versions of the files.
Examples:
Absolute restoration of an absolute backup
$ cpio –ivF home.A.cpio
Absolute restoration on an existing tree structure
The u option allows you to overwrite existing files at the location where the restore takes place.
$ cpio –iuvF home.A.cpio
Restore an absolute backup in relative mode
The long option no-absolute-filenames allows a restoration in relative mode. Indeed the / at the beginning of the path will be removed.
$ cpio --no-absolute-filenames -divuF home.A.cpio
Tip
The creation of directories is perhaps necessary, hence the use of the d option
Restore a relative backup
$ cpio –iv <etc.cpio
Absolute restoration of a file or directory
The restoration of a particular file or directory requires the creation of a list file that must then be deleted.