< Day Day Up > |
tar (originally short for tape archive) is widely considered the most portable of the backup and restore programs. You will find that many applications are shipped on tar tapes and many Linux files downloaded from the Internet are in tar format or compressed tar format such as archivename.tgz. This is the most widely used format for exchanging data with other Linux and UNIX systems. You can append files to the end of a tar file or tape, which you can't do with many other programs. When sending files to another Linux user, I would recommend tar. tar is a slow backup program, so keep this in mind if you use it for your full or incremental backups if you have a lot of data to back up. One highly desirable aspect of tar is that when you load files onto a tape with tar and then restore them onto another system, the original users and groups are retained. We'll use several tar commands in the upcoming examples, including the following: # tar cf /dev/st0 /var ;use tar to create (c) an archive of the directory /var and put it on tape /dev/st0. # tar tvf /dev/st0 ;obtain table of contents (t) from tape /dev/st0 and produce produce verbose (v) output. # tar xvf /dev/st0 ;extract (x) the entire contents of the archive on tape /dev/st0 to default destination. # tar xvf /dev/st0 file1 ;extract (x) only file1 from the archive on tape /dev/st0 to default destination # cd /var/log; tar cf - .| (cd /tmp/log; tar xfBp -) ;send data to standard output with cf -, then use standard input with xfBp - to copy the contents of /var/log to /tmp/log. You'll notice when you view the man pages for tar that options are preceded by a hyphen. The command works without the hyphen so most tar examples omit the hyphen. Take a look at some examples using tar. Begin by performing a tar backup (usually called creating an archive) of the directory /var to tape device /dev/st0. We use the c option to create a backup, the v option for verbose output, and the f option to specify the file of the tape drive /dev/st0 because the default I/O is to stdin/stdout: [root@demtstd1 root]# tar cvf /dev/st0 /var tar: Removing leading `/' from member names var/ var/lib/ var/lib/rpm/ var/lib/rpm/Packages var/lib/rpm/Basenames var/lib/rpm/Name var/lib/rpm/Group var/lib/rpm/Requirename var/lib/rpm/Providename var/lib/rpm/Conflictname var/lib/rpm/Dirnames var/lib/rpm/Requireversion var/lib/rpm/Provideversion var/lib/rpm/Installtid var/lib/rpm/Sigmd5 var/lib/rpm/Sha1header var/lib/rpm/Filemd5s var/lib/rpm/Triggername var/lib/games/ var/lib/misc/ . . . var/www/icons/up.png var/www/icons/uu.gif var/www/icons/uu.png var/www/icons/uuencoded.gif var/www/icons/uuencoded.png var/www/icons/world1.gif var/www/icons/world1.png var/www/icons/world2.gif var/www/icons/world2.png var/crash/ var/crash/.ssh/ var/crash/.ssh/authorized_keys2 var/crash/magic/ var/crash/scripts/ [root@demtstd1 root]# more /var/log/sa/sa05 var/crash/magic/ var/crash/scripts/ [root@demtstd1 root]# The abbreviated output shows a list of all files that were included in the archive. If you wanted to see only messages related to problems with creating the archive, you would leave off the v option and you'd receive only problem-related messages. Next, take a look at only the file named sa05 on the tape. To produce a table of contents, we will use the t option. The following example also uses v for verbose output: [root@demtstd1 root]# tar tvf /dev/st0 | grep sa05 -rw-r--r-- root/root 146661 2003-03-05 10:30:00 var/log/sa/sa05 Note that this is a relative backup; that is, there is no / preceding the file name. You need to be aware of the relative path names used with tar and your present working directory when using tar. We'll delete sa05 from the computer and restore it from tape using the x option to extract the file from the tar archive. We'll then list the directory (using ll, which is an alias for ls -l that we've setup) on the system to confirm that the file we deleted has been restored to the directory from tape: [root@demtstd1 root]# rm /var/log/sa/sa05 rm: remove `/var/log/sa/sa05'? y [root@demtstd1 root]# tar xvf /dev/st0 /var/log/sa/sa05 var/log/sa/sa05 [root@demtstd1 root]# ll /var/log/sa total 5432 -rw-r--r-- 1 root root 329701 Mar 1 23:50 _[00msa01_[00m -rw-r--r-- 1 root root 329701 Mar 2 23:50 _[00msa02_[00m -rw-r--r-- 1 root root 329701 Mar 3 23:50 _[00msa03_[00m -rw-r--r-- 1 root root 329701 Mar 4 23:50 _[00msa04_[00m -rw-r--r-- 1 root root 329701 Feb 25 23:50 _[00msa25_[00m -rw-r--r-- 1 root root 329701 Feb 26 23:50 _[00msa26_[00m -rw-r--r-- 1 root root 329701 Feb 27 23:50 _[00msa27_[00m -rw-r--r-- 1 root root 329701 Feb 28 23:50 _[00msa28_[00m -rw-r--r-- 1 root root 311429 Mar 2 04:02 _[00msar01_[00m -rw-r--r-- 1 root root 311428 Mar 3 04:02 _[00msar02_[00m -rw-r--r-- 1 root root 311430 Mar 4 04:02 _[00msar03_[00m -rw-r--r-- 1 root root 311430 Mar 5 04:02 _[00msar04_[00m -rw-r--r-- 1 root root 311429 Feb 25 04:02 _[00msar24_[00m -rw-r--r-- 1 root root 311430 Feb 26 04:02 _[00msar25_[00m -rw-r--r-- 1 root root 311429 Feb 27 04:02 _[00msar26_[00m -rw-r--r-- 1 root root 311430 Feb 28 04:02 _[00msar27_[00m -rw-r--r-- 1 root root 311430 Mar 1 04:02 _[00msar28_[00m_ [root@demtstd1 root]# This backup and restore using tar is simple and gets the job done. The file sa05 has indeed been restored. A common use for tar is to back up files from one directory and restore them to another directory. We'll backup the contents of /var/log and restore them to the directory /tmp/log. In the following example, we create a tar backup archive to a file rather than to tape. The file is called tartest. We will then move this file to the destination directory and extract it there. We don't use a tape at all in this example: [root@demtstd1 root]# cd /var/log [root@demtstd1 log]# ll total 100124 -rw------- 1 root root 0 Mar 2 04:02 boot.log -rw------- 1 root root 0 Feb 23 04:02 boot.log.1 -rw------- 1 root root 0 Feb 16 04:02 boot.log.2 -rw------- 1 root root 3938 Feb 11 14:48 boot.log.3 -rw------- 1 root root 18637 Feb 11 14:32 boot.log.4 -rw------- 1 root root 122071 Mar 5 10:40 cron -rw------- 1 root root 260944 Mar 2 04:02 cron.1 -rw------- 1 root root 260768 Feb 23 04:02 cron.2 -rw------- 1 root root 170460 Feb 16 04:02 cron.3 -rw------- 1 root root 167831 Feb 11 14:37 cron.4. . . . drwxr-xr-x 2 root root 4096 May 21 2002 vbox -rw-rw-r-- 1 root utmp 19666000 Mar 5 10:40 wtmp -rw-rw-r-- 1 root utmp 80738000 Mar 1 04:01 wtmp.1 -rw-r--r-- 1 root root 86 Mar 5 10:40 xdm-errors [root@demtstd1 log]# tar cvf /tmp/tartest 'ls -a' boot.log boot.log.1 boot.log.2 boot.log.3 boot.log.4 cron cron.1 cron.2 cron.3 cron.4 samba/ . . . vbox/ wtmp wtmp.1 xdm-errors [root@demtstd1 log]# [root@demtstd1 log]# cd /tmp [root@demtstd1 tmp]# mkdir log [root@demtstd1 tmp]# cp tartest log [root@demtstd1 tmp]# cd log [root@demtstd1 log]# ls -l total 124260 -rw-r--r-- 1 root root 127109120 Mar 5 10:44 tartest [m[root@demtstd1 log]# tar xvf tartest boot.log boot.log.1 boot.log.2 boot.log.3 boot.log.4 cron cron.1 cron.2 cron.3 cron.4. . . . vbox/ wtmp wtmp.1 xdm-errors [root@demtstd1 log]# When creating the tar backup, I first changed to the /var/log directory and then used the ls command (a grav or accent, which is near the upper left of most keyboards on the same key as a tilde, appears before and after the ls). This produced relative path names that I could easily restore to the /tmp/ log directory. Alternatively, I could also have just changed directory to /var and issued the command tar cf /dev/st0 log to back up the entire contents of the /var/log directory. This entire process could have been done on a single command line. The following line shows the commonly used procedure for producing an archive in the fromdir and restoring it to the todir: # cd fromdir ; tar cf - . | (cd todir ; tar xf -i) For our example, we would perform the following: # mkdir /tmp/log # cd /var/log; tar cf - .| (cd /tmp/log; tar xfBp -) The "-" in the tar cf command tells tar to send its data to standard output. The "-" in the tar xfBp command tells tar to look to standard input for data, which is the data produced by tar cf - issued earlier on the command line. This does indeed result in all the files of /var/log being copied to the directory /tmp/log. You can also use tar combined with find to produce an incremental backup of your system. An incremental backup is one which includes only files that have changed over a specified time period. Running the following find command produces a file that contains only the names of files that have changed in the last 24 hours: # find / -mtime -1 -print > /tmp/daily.backup.list We could then take the list of files in /tmp/daily.backup.list and use this file as input to tar with the following command: # tar -cv -T /tmp/daily.backup.list -f /dev/st0 Note that in this example (unlike some of the earlier ones), we do need to include dashes for the options to be read. The -cv creates and archives with verbose output and the option -T indicates that the following file contains a list of files to be included in the archive. The -f indicates the file to which the archive will be written which, in this case, is our tape drive. You often download files that have been compressed with gzip from the Internet. Such files have an extension of .gz. You would first unzip such files with gunzip and then use tar -xf to extract the archive as shown here: # ls files.tar.gz # gunzip files.tar.gz # ls files.tar # tar -xf examples.tar # ls files.tar file1 file2 file3 # This is a commonly performed procedure after downloading files. |
< Day Day Up > |