4.6. Archiving Files: cpio, tar, and dump/restoreThere are several occasions when you'll want to save some files to a secondary storage medium such as a disk or tape:
Three GNU archiving programs are available in Linux environments, each having its own strengths and weaknesses:
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: cpioThe 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.
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 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 $ _ 4.6.2. Tape Archiving: tarThe 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.
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 ./ 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 $ _ 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 RestoreThe 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:
This kind of backup strategy is supported nicely by the dump and restore utilities. Figure 4-15 describes how dump works.
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.
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 |