Библиотека сайта rus-linux.net
9.1. Introduction
Although Linux is one of the safest operating systems in existence, and even if it is designed to keep on going, data can get lost. Data loss is most often the consequence of user errors, but occasionally a system fault, such as a power or disk failure, is the cause, so it's always a good idea to keep an extra copy of sensitive and/or important data.
9.1.1. Preparing your data
9.1.1.1. Archiving with tar
In most cases, we will first collect all the data to back up in a single archive file, which we will compress later on. The process of archiving involves concatenating all listed files and taking out unnecessary blanks. In Linux, this is commonly done with the tar command. tar was originally designed to archive data on tapes, but it can also make archives, known as tarballs.
tar has many options, the most important ones are cited below:
-v
: verbose-t
: test, shows content of a tarball-x
: extract archive-c
: create archive-f
archivedevice
: usearchivedevice
as source/destination for the tarball, the device defaults to the first tape device (usually/dev/st0
or something similar)-j
: filter through bzip2, see Section 9.1.1.2
It is common to leave out the dash-prefix with tar options, as you can see from the examples below.
Use GNU tar for compatibility | |
---|---|
The archives made with a proprietary tar version on one system, may be incompatible with tar on another proprietary system. This may cause much headaches, such as if the archive needs to be recovered on a system that doesn't exist anymore. Use the GNU tar version on all systems to prevent your system admin from bursting into tears. Linux always uses GNU tar. When working on other UNIX machines, enter tar |
In the example below, an archive is created and unpacked.
|
This example also illustrates the difference between a tarred directory and a bunch of tarred files. It is advisable to only compress directories, so files don't get spread all over when unpacking the tarball (which may be on another system, where you may not know which files were already there and which are the ones from the archive).
When a tape drive is connected to your machine and configured by your system administrator, the file names ending in .tar
are replaced with the tape device name, for example:
tar cvf
/dev/tape
mail/
The directory mail
and all the files it contains are compressed into a file that is written on the tape immediately. A content listing is displayed because we used the verbose option.
9.1.1.2. Incremental backups with tar
The tar tool supports the creation of incremental backups, using the -N
option. With this option, you can specify a date, and tar will check modification time of all specified files against this date. If files are changed more recent than date, they will be included in the backup. The example below uses the timestamp on a previous archive as the date value. First, the initial archive is created and the timestamp on the initial backup file is shown. Then a new file is created, upon which we take a new backup, containing only this new file:
|
Standard errors are redirected to /dev/null
. If you don't do this, tar will print a message for each unchanged file, telling you it won't be dumped.
This way of working has the disadvantage that it looks at timestamps on files. Say that you download an archive into the directory containing your backups, and the archive contains files that have been created two years ago. When checking the timestamps of those files against the timestamp on the initial archive, the new files will actually seem old to tar, and will not be included in an incremental backup made using the -N
option.
A better choice would be the -g
option, which will create a list of files to backup. When making incremental backups, files are checked against this list. This is how it works:
|
The next day, user jimmy works on file3
a bit more, and creates file4
. At the end of the day, he makes a new backup:
|
These are some very simple examples, but you could also use this kind of command in a cronjob (see Section 4.4.4), which specifies for instance a snapshot file for the weekly backup and one for the daily backup. Snapshot files should be replaced when taking full backups, in that case.
More information can be found in the tar documentation.
The real stuff | |
---|---|
As you could probably notice, tar is OK when we are talking about a simple directory, a set of files that belongs together. There are tools that are easier to manage, however, when you want to archive entire partitions or disks or larger projects. We just explain about tar here because it is a very popular tool for distributing archives. It will happen quite often that you need to install a software that comes in a so-called "compressed tarball". See Section 9.3 for an easier way to perform regular backups. |
9.1.1.3. Compressing and unpacking with gzip or bzip2
Data, including tarballs, can be compressed using zip tools. The gzip command will add the suffix .gz to the file name and remove the original file.
|
Uncompress gzipped files with the -d
option.
bzip2 works in a similar way, but uses an improved compression algorithm, thus creating smaller files. See the bzip2 info pages for more.
Linux software packages are often distributed in a gzipped tarball. The sensible thing to do after unpacking that kind of archives is find the README
and read it. It will generally contain guidelines to installing the package.
The GNU tar command is aware of gzipped files. Use the command
tar zxvf
file.tar.gz
for unzipping and untarring .tar.gz
or .tgz
files. Use
tar jxvf
file.tar.bz2
for unpacking tar archives that were compressed with bzip2.
9.1.1.4. Java archives
The GNU project provides us with the jar tool for creating Java archives. It is a Java application that combines multiple files into a single JAR archive file. While also being a general purpose archiving and compression tool, based on ZIP and the ZLIB compression format, jar was mainly designed to facilitate the packing of Java code, applets and/or applications in a single file. When combined in a single archive, the components of a Java application, can be downloaded much faster.
Unlike tar, jar compresses by default, independent from other tools - because it is basically the Java version of zip. In addition, it allows individual entries in an archive to be signed by the author, so that origins can be authenticated.
The syntax is almost identical as for the tar command, we refer to info jar
for specific differences.
tar, jar and symbolic links | |
---|---|
One noteworthy feature not really mentioned in the standard documentation is that jar will follow symbolic links. Data to which these links are pointing will be included in the archive. The default in tar is to only backup the symbolic link, but this behavior can be changed using the |
9.1.1.5. Transporting your data
Saving copies of your data on another host is a simple but accurate way of making backups. See Chapter 10 for more information on scp, ftp and more.
In the next section we'll discuss local backup devices.