Книга: Fedora™ Unleashed, 2008 edition

tar: The Most Basic Backup Tool

tar: The Most Basic Backup Tool

The tar tool, the bewhiskered old man of archiving utilities, is installed by default. It is an excellent tool for saving entire directories full of files. For example, here is the command used to back up the /etc directory:

# tar cvf etc.tar /etc

Here, the options use tar to create an archive, are verbose in the message output, and use the filename etc.tar as the archive name for the contents of the directory /etc.

Alternatively, if the output of tar is sent to the standard output and redirected to a file, the command appears as follows:

# tar cv /etc > etc.tar

The result is the same.

All files in the /etc directory will be saved to a file named etc.tar. With an impressive array of options (see the man page), tar is quite flexible and powerful in combination with shell scripts. With the -z option, it can even create and restore gzip compressed archives, whereas the -j option works with bzipped files.

Creating Full and Incremental Backups with tar

If you want to create a full backup,

# tar cjvf fullbackup.tar.bz2 /

creates a bzip2-compressed tarball (the j option) of the entire system.

To perform an incremental backup, you must locate all the files that have been changed since the last backup. For simplicity, assume that you do incremental backups on a daily basis. To locate the files, use the find command:

# find / -newer name_of_last_backup_file ! -a -type f -print

When run alone, find generates a list of files systemwide and prints it to the screen. The ! -a -type eliminates everything but regular files from the list; otherwise, the entire directory would be sent to tar even if the contents were not all changed.

Pipe the output of the find command to tar as follows:

#  find / -newer name_of_last_backup_file ! -type d -print |
tar czT - backup_file_name_or_device_name

Here, the T - option gets the filenames from a buffer (where the - is the shorthand name for the buffer).

NOTE

The tar command can back up to a raw device (one with no file system) as well as a formatted partition. For example,

# tar cvzf /dev/hdd /boot /etc /home

backs up those directories to device /dev/hdd (not /dev/hda1, but to the unformatted device itself).

The tar command can also back up over multiple floppy disks:

# tar czvMf /dev/fd0 /home

backs up the contents of /home and spreads the file out over multiple floppies, prompting you with this message:

Prepare volume #2 for '/dev/fd0' and hit return:

Restoring Files from an Archive with tar

The xp option in tar restores the files from a backup and preserves the file attributes as well, and tar creates any subdirectories it needs. Be careful when using this option because the backups might have been created with either relative or absolute paths. You should use the tvf option with tar to list the files in the archive before extracting them so that you know where they will be placed.

For example, to restore a tar archive compressed with bzip2,

# tar xjvf fedoratest.tar.bz2

To list the contents of a tar archive compressed with bzip2,

# tar tjvf fedoratest.tar.bz2
drwxrwxr-x paul/paul    0 2003-09-04 18:15:05 fedoratest/
-rw-rw-r-- paul/paul  163 2003-09-03 22:30:49 fedoratest/fedora_screenshots.txt
-rw-rw-r-- paul/paul  840 2003-09-01 19:27:59 fedoratest/fedora_guideline.txt
-rw-rw-r-- paul/paul 1485 2003-09-01 18:14:23 fedoratest/style-sheet.txt
-rw-rw-r-- paul/paul  931 2003-09-01 19:02:00 fedoratest/fedora_TOC.txt

Note that because the pathnames do not start with a backslash, they are relative path names and install in your current working directory. If they were absolute pathnames, they would install exactly where the paths state.

Оглавление книги


Генерация: 1.509. Запросов К БД/Cache: 3 / 1
поделиться
Вверх Вниз