📄 229-231.html
字号:
<HTML>
<HEAD>
<TITLE>Special Edition Using Linux, Fourth Edition:Backing Up Data</TITLE>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!--ISBN=0789717468//-->
<!--TITLE=Special Edition Using Linux, Fourth Edition//-->
<!--AUTHOR=Jack Tackett//-->
<!--AUTHOR=Jr.//-->
<!--AUTHOR=Steve Burnett//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Que//-->
<!--CHAPTER=11//-->
<!--PAGES=229-231//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="227-229.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="231-234.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>It also has a few disadvantages:
</P>
<DL>
<DD><B>•</B> For some versions of <TT>tar</TT>, the archive must reside on one disk or tape, which means that if a portion of the medium fails—from a bad sector on a disk or bad block on a tape, for example—the entire backup may be lost.
<DD><B>•</B> <TT>tar</TT> can’t back up special files, such as device files.
<DD><B>•</B> On its own, <TT>tar</TT> can perform only complete backups. If you want to do incremental backups, you have to do a little shell programming.
</DL>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR>• <B>See</B> “Working with Shell Scripts,” <B>p. 365</B><HR></FONT>
</BLOCKQUOTE>
<P>Table 11.1 lists some options that are commonly used with <TT>tar</TT>. You can use many other command parameters with <TT>tar</TT>; refer to the man page for a complete list.</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 11.1</B> Common Options for the <I>tar</I> Command
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="20%" ALIGN="LEFT">Option
<TH WIDTH="80%" ALIGN="LEFT">Description
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TD><TT>c</TT>
<TD>Creates an archive.
<TR>
<TD VALIGN="TOP"><TT>x</TT>
<TD>Extracts or restores files from the archive that’s on the default device or on the device specified by the <TT>f</TT> option.
<TR>
<TD VALIGN="TOP"><TT>f <I>name</I></TT>
<TD>Creates the archive or reads the archive from <I>name</I>, where <I>name</I> is a filename or a device specified in /dev, such as /dev/rmt0.
<TR>
<TD><TT>Z</TT>
<TD>Compresses or decompresses the tar archive.
<TR>
<TD><TT>z</TT>
<TD>Compresses or decompresses the tar archive with <TT>gzip</TT>.
<TR>
<TD><TT>M</TT>
<TD>Creates a multivolume tar backup.
<TR>
<TD><TT>t</TT>
<TD>Creates an index of all files stored in an archive and lists on stdout.
<TR>
<TD><TT>v</TT>
<TD>Uses verbose mode.
<TR>
<TH COLSPAN="2"><HR>
</TABLE>
<P>Consider some examples of the use of <TT>tar</TT> in backing up and restoring files. The following command copies the directory /home to the floppy drive /dev/fd0:</P>
<!-- CODE SNIP //-->
<PRE>
tar -cf /dev/fd0 /home
</PRE>
<!-- END CODE SNIP //-->
<P>In this case, the <TT>f</TT> option specifies that the archive is created on the floppy drive device /dev/fd0.</P>
<P>The following command also archives the directory /home:</P>
<!-- CODE SNIP //-->
<PRE>
tar -cvfzM /dev/fd0 /home | tee homeindex
</PRE>
<!-- END CODE SNIP //-->
<P>The <TT>v</TT> option indicates verbose mode, the <TT>z</TT> option indicates that the archive should be compressed to save space, and the <TT>M</TT> option tells <TT>tar</TT> to create a multivolume backup. When one floppy disk is full, <TT>tar</TT> prompts you for another. A list of the copied files is directed to homeindex. It’s a good idea to look at that file to see what was copied.</P>
<P>The <TT>find</TT> command is useful for locating files that have been modified within a certain time period so that they can be scheduled for incremental backups. The following example uses the command <TT>find</TT> to create a list of all files that have been modified in the last day:</P>
<!-- CODE SNIP //-->
<PRE>
find /home -mtime -1 -type f -print > bkuplst tar cvfzM /dev/fd0
‘cat bkuplst’ | tee homeindex
</PRE>
<!-- END CODE SNIP //-->
<P>To use the list as input to the <TT>tar</TT> command, place the command <TT>cat</TT> <TT>bkuplst</TT> in back quotes (backward single quotation marks, also known as <TT>grave accents</TT>—<TT>‘cat bkuplst’</TT>). This tells the shell to execute the command as a subshell and place the output from the command on the command line in the location of the original back-quoted command.</P>
<P>The following command restores the /home/dave/notes.txt file from the device /dev/fd0 (note that you have to give the complete filename to restore it):</P>
<!-- CODE SNIP //-->
<PRE>
tar xv /usr2/dave/notes.txt
</PRE>
<!-- END CODE SNIP //-->
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>TIP: </B>You can automate any of these commands by putting them in root’s crontab file. For example, you could put the following entry in the root’s crontab file to perform a backup of /home every day at 1:30 a.m.:
<!-- CODE SNIP //-->
<PRE>
30 01 * * * tar cvfz /def/fd0 /home > homeindex
</PRE>
<!-- END CODE SNIP //-->
<P>If you need to do more complicated backups, you can create shell scripts to control your backups. These shell scripts can also be run via cron.
</P>
<P>• <B>See</B> “Scheduling Commands with <TT>cron</TT> and <TT>crontab</TT>,” <B>p. 388</B><HR></FONT>
</BLOCKQUOTE>
</P>
<P>You also can use the <TT>tar</TT> command to create archive files in the Linux file system rather than write to a backup device. This way, you can archive a group of files along with their directory structure in one file. To do this, simply give a file name as the argument to the <TT>f</TT> option instead of a device name. The following is an example of archiving a directory and its subdirectories with the <TT>tar</TT> command:</P>
<!-- CODE SNIP //-->
<PRE>
tar cvf /home/backup.tar /home/dave
</PRE>
<!-- END CODE SNIP //-->
<P>This creates the file /home/backup.tar, which contains a backup of the /home/dave directory and all files and subdirectories below /home/dave.
</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>NOTE: </B>The tar command by itself doesn’t perform any file compression. To compress the resulting tar file, either specify the z option with the tar command or use a compression program, such as gzip, on the final tar file.<HR></FONT>
</BLOCKQUOTE>
<P>When you use <TT>tar</TT> to make archive files, it’s usually a good idea to try to make the top-level entry in the tar file a directory. This way, when you extract the tar file, all the files in it are placed under a central directory in your current working directory. Otherwise, you could end up with hundreds of files in your directory if you extract a tar file in the wrong place.</P>
<P>Suppose that you have below your current directory a directory named data, which contains several hundred files. There are two basic ways to create a tar file of this directory. You can change directories to the data directory and create the tar file from there, as in this example:</P>
<!-- CODE SNIP //-->
<PRE>
$ <B>pwd</B>
/home/dave
$ <B>cd data</B>
$ <B>pwd</B>
/home/dave/data
$ <B>tar cvf ../data.tar *</B>
</PRE>
<!-- END CODE SNIP //-->
<P>This creates a tar file in /home/dave that contains just the contents of data without containing an entry for the directory. When you extract this tar file, you don’t create a directory to put the files in—you just get several hundred files in your current directory.
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="227-229.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="231-234.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -