Section 4.6. Archiving Files: cpio, tar, and dumprestore


[Page 121 (continued)]

4.6. Archiving Files: cpio, tar, and dump/restore

There are several occasions when you'll want to save some files to a secondary storage medium such as a disk or tape:

  • for daily, weekly, or monthly backups

  • for transport between non-networked Linux or UNIX machines

  • for posterity

Three GNU archiving programs are available in Linux environments, each having its own strengths and weaknesses:

  • cpio, which allows you to save directory structures onto a single backup volume. It's handy for saving small quantities of data, but the single-volume restriction makes it useless for large backups.

  • tar, which allows you to save directory structures onto a single backup volume. It's specially designed to save files onto tape, so it always archives files onto the end of the storage medium. As before, the single-volume restriction makes it unusable for large backups.

  • dump and restore, which allow you to save a file system to multiple backup volumes. dump is especially useful for doing total and incremental backups.

You can write archive information to a tape device or create regular files that can later be copied to an external drive (like a Zip drive), a spare internal disk drive, or a CD writer. You can store archive files anywhere you can store a regular file.

4.6.1. Copying Files: cpio

The cpio utility allows you to create and access special cpio-format files. These special format files are useful for backing up small subdirectories. Figure 4-13 describes how cpio works.


[Page 122]

Figure 4-13. Description of the cpio command.

Utility: cpio -ov

cpio -idtu patterns

cpio -pl directory

cpio allows you to create and access special cpio-format files.

The -o option takes a list of filenames from standard input and creates a cpio-format file that contains a backup of the files. The -v option causes the name of each file to be displayed as it's copied.

The -i option reads a cpio-format file from standard input and re-creates all of the files from the input channel whose name matches a specified pattern. By default, older files are not copied over younger files. The -u option causes unconditional copying. The -d option causes directories to be created if they are needed during the copy process. The -t option causes a table of contents to be displayed instead of performing the copy.

The -p option takes a list of filenames from standard input and copies their contents to a named directory. This option is useful for copying one subdirectory to another place, although most uses of this option can be performed more easily using the cp utility with the -r (recursive) option. The -l option creates links instead of performing physical copies whenever possible.


To demonstrate the -o and -i options, I created a backup version of all the C source files in my current directory, deleted the source files, and then restored them.

$ ls -lG *.c                     ...list the files to be saved. -rw-r--r--  1 glass   172 Jan  5 19:44 main1.c -rw-r--r--  1 glass   198 Jan  5 19:44 main2.c -rw-r--r--  1 glass   224 Jan  5 19:44 palindrome.c -rw-r--r--  1 glass   266 Jan  5 23:46 reverse.c $ ls *.c | cpio -ov > backup    ...save in "backup". main1.c main2.c palindrome.c reverse.c 3 blocks $ ls -lG backup            ...examine "backup". -rw-r--r--   1 glass      1536 Jan  9 18:34 backup $ rm *.c                  ...remove the original files. $ cpio -it < backup       ...restore the files. main1.c main2.c palindrome.c reverse.c 3 blocks 
[Page 123]
$ ls -lG *.c ..confirm their restoration. -rw-r--r-- 1 glass 172 Jan 5 19:44 main1.c -rw-r--r-- 1 glass 198 Jan 5 19:44 main2.c -rw-r--r-- 1 glass 224 Jan 5 19:44 palindrome.c -rw-r--r-- 1 glass 266 Jan 5 23:46 reverse.c $ _


To back up all of the files that match the pattern "*.c", including subdirectories, use the output from the find utility as the input to cpio. The -depth option of find recursively searches for matching patterns. In the following example, note that I escaped the * character in the argument to the -name option so that it was not expanded by the shell:

$ find . -name \*.c -depth -print | cpio -ov > backup2 main1.c main2.c palindrome.c reverse.c tmp/b.c tmp/a.c 3 blocks $ rm -r *.c            ...remove the original files. $ rm tmp/*.c           ...remove the lower-level files. $ cpio -it < backup2   ...restore the files. main1.c main2.c palindrome.c reverse.c tmp/b.c tmp/a.c 3 blocks $ _ 


To demonstrate the -p option, I obtained a list of all the files in my current directory that were modified in the last two days (using the find utility) and then copied them into the parent directory. Without the -l option, the files were physically copied, resulting in a total increase in disk usage of 153 blocks. With the -l option, however, the files were linked, resulting in no disk usage at all.

$ find . -mtime -2 -print | cpio -p ..            ...copy 153 blocks $ ls -lG ../reverse.c         ...look at the copied file. -rw-r--r--  1 glass      266 Jan  9 18:42 ../reverse.c $ find . -mtime -2 -print | cpio -pl ..           ...link 0 blocks $ ls -lG ../reverse.c         ...look at the linked file. -rw-r--r--    2 glass      266 Jan    7 15:26 ../reverse.c $ _ 



[Page 124]

4.6.2. Tape Archiving: tar

The tar utility was designed specifically for maintaining an archive of files on a magnetic tape. When you add a file to an archive file using tar, the file is always placed on the end, since you cannot modify the middle of a file that is stored on tape. Figure 4-14 shows how tar works.

Figure 4-14. Description of the tar command.

Utility: tar -cfrtuvxz [ tarFileName ] fileList

tar allows you to create and access special tar-format archive files. The -c option creates a tar-format file. Use the -f option followed by a filename to specify the destination for the tar-format file. The -v option causes verbose output. The -x option allows you to extract named files, and the -t option generates a table of contents. The -r option unconditionally appends the listed files to the archive file. The -u option appends only files that are more recent than those already archived. The -z option filters the archive through gzip to compress or uncompress it. If the file list contains directory names, the contents of the directories are appended/extracted recursively.


In the following example, I saved all of the files in the current directory to the archive file "tarfile":

$ ls                      ...look at the current directory. main1       main2        palindrome.c       reverse.h main1.c      main2.c      palindrome.h       tarfile main1.make   main2.make   reverse.c          tmp/ $ ls tmp                  ...look in the "tmp" directory. a.c          b.c $ tar -cvf  tarfile .     ...archive the current directory. ./main1.c /main2.c ... /main2 /tmp/b.c /tmp/a.c $ ls -lG tarfile    ...look at the archive file "tarfile". -rw-r--r--  1 glass    65536 Jan 10 12:44 tarfile $ _ 


To obtain a table of contents of a tar archive, use the -t option:

$ tar -tvf tarfile     ...look at the table of contents. drwxr-xr-x 496/62      0 2005-01-10 12:10:22 ./ 
[Page 125]
-rw-r--r-- 496/62 172 2005-01-10 12:10:24 ./main1.c -rw-r--r-- 496/62 198 2005-01-09 12:10:24 ./main2.c ... -rw-r--r-- 496/62 24576 2005-01-07 12:24:54 ./main2 drwxr-xr-x 496/62 0 2005-01-10 12:10:28 ./tmp/ -rw-r--r-- 496/62 9 2005-01-10 12:10:29 ./tmp/b.c -rw-r--r-- 496/62 9 2005-01-10 12:10:29 ./tmp/a.c $


To unconditionally append a file to the end of a tar archive, use the -r option followed by a list of files and/or directories to append. Notice in the following example that the tar archive ended up holding two copies of "reverse.c":

$ tar -rvf tarfile reverse.c         ...unconditionally append. reverse.c $ tar -tvf tarfile             ...look at the table of contents. drwxr-xr-x496/62       0 2005-01-10 12:10:24 ./ -rw-r--r--496/62     172 2005-01-10 12:32:33 ./main1.c ... -rw-r--r--496/62     266 2005-01-09 12:32:34 ./reverse.c ... -rw-r--r--496/62     266 2005-01-10 12:32:56 reverse.c $ _ 


To append a file only if it isn't in the archive or if it has been modified since it was last archived, use the -u option instead of -r. In the following example, note that "reverse.c" was not archived because it hadn't been modified:

$ tar -rvf tarfile reverse.c        ...unconditionally append. reverse.c $ tar -uvf tarfile reverse.c        ...conditionally append. $ _ 


To extract a file from an archive file, use the -x option followed by a list of files and/or directories. If a directory name is specified, it is recursively extracted:

$ rm tmp/*                   ...remove all files from "tmp". $ tar -vxf tarfile ./tmp     ...extract archived "tmp" files. ./tmp/b.c ./tmp/a.c $ ls tmp                     ...confirm restoration. a.c         b.c $ _ 



[Page 126]

Unfortunately, tar doesn't support pattern matching of the name list, so to extract files that match a particular pattern, be crafty and use grep as part of the command sequencelike this:

$ tar -xvf tarfile 'tar -tf tarfile | grep '.*\.c'' ./main1.c ./main2.c ./palindrome.c ./reverse.c ./tmp/b.c ./tmp/a.c $ _ 


If you change into another directory and then extract files that were stored using relative pathnames, the names are interpreted as being relative to the current directory. In the following example, I restored "reverse.c" from the previously created tar file to a new directory "tmp2". Note that each copy of "reverse.c" overwrote the previous one, so that the latest version was the one that was left intact:

$ mkdir tmp2                        ...create a new directory. $ cd tmp2                           ...move there. $ tar -vxf ../tarfile reverse.c     ...restore single file. reverse.c reverse.c $ ls -lG                            ...confirm restoration. total 1 -rw-r--r--  1 glass         266 Jan 10 12:48 reverse.c $ _ 


4.6.3. Incremental Backups: Dump and Restore

The dump and restore commands were originally part of BSD UNIX but became common in other versions of UNIX and are included in most Linux distributions for backing up ext2 and ext3 format file systems. Here's a system administrator's typical backup strategy:

  • Perform a monthly total file system backup.

  • Perform a daily incremental backup, storing only those files that were changed since the last incremental backup. Daily dumps may be made at different backup levels, so only files backed up since the last lower-level incremental backup will be backed up again.

This kind of backup strategy is supported nicely by the dump and restore utilities. Figure 4-15 describes how dump works.


[Page 127]

Figure 4-15. Description of the dump command.

Utility: dump [ level ] [ -f dumpFile ] [ -w ] fileSystem

dump [ level ] [ -f dumpFile ] [ -w ] { fileName }+

The dump utility has two forms. The first form of the dump utility copies files from the specified file system to dumpFile, which is "/dev/rmt0" by default (this will vary depending on your available tape or disk device). If the dump level is specified as n, then all of the files that have been modified since the last dump at a lower level than n are copied. For example, a level 0 dump will always dump all files, whereas a level 2 dump will dump all of the files modified since the last level 0 or level 1 dump. If no dump level is specified, it is set to 9. The -w option causes dump to display a list of all the file systems that need to be dumped instead of performing a backup.

The second form of dump allows you to specify the names of files to be dumped.

Both forms prompt the user to insert and/or remove dump media when necessary. For example, a large system dump to a tape drive often requires an operator to remove a full tape and replace it with an empty one.

When a dump is performed, information about the dump is recorded in the "/etc/dumpdates" file for use by future invocations of dump.


Here's an example of dump which performs a level 0 dump of the file system on /dev/hda0 to the tape drive /dev/rmt0:

$ dump 0 -f /dev/rmt0 /dev/hda0 


The restore utility allows you to restore files from a dump backup, and works as shown in Figure 4-16.

Figure 4-16. Description of the restore command.

Utility: restore -irtx [ -f dumpFile ] { fileName }*

The restore utility allows you to restore a set of files from a previous dump file found on dumpFile. The -r option causes every file on dumpFile to be restored into the current directory, so use this option with care. The -t option causes a table of contents of dumpFile to be displayed instead of restoring any files. The -x option causes restore to extract only the specified filenames from dumpFile. If a filename is the name of a directory, its contents are recursively restored.

The -i option causes restore to read the table of contents of dumpFile and then enter an interactive mode that allows you to choose the files that you wish to restore. For more information on this interactive mode, consult man restore.



[Page 128]

In the following example, I used restore to extract a couple of previously saved files from the dump device "/dev/rmt0":

$ restore -x -f /dev/rmt0 wine.c hacking.c 





Linux for Programmers and Users
Linux for Programmers and Users
ISBN: 0131857487
EAN: 2147483647
Year: 2007
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net