⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 229-231.html

📁 linux-unix130.linux.and.unix.ebooks130 linux and unix ebookslinuxLearning Linux - Collection of 12 E
💻 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>&#149;</B>&nbsp;&nbsp;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&#151;from a bad sector on a disk or bad block on a tape, for example&#151;the entire backup may be lost.

<DD><B>&#149;</B>&nbsp;&nbsp;<TT>tar</TT> can&#146;t back up special files, such as device files.

<DD><B>&#149;</B>&nbsp;&nbsp;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>&#149; <B>See</B> &#147;Working with Shell Scripts,&#148; <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&#146;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&#146;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 &gt; bkuplst tar cvfzM /dev/fd0

&#145;cat bkuplst&#146; | 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>&#151;<TT>&#145;cat bkuplst&#146;</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:&nbsp;&nbsp;</B>You can automate any of these commands by putting them in root&#146;s crontab file. For example, you could put the following entry in the root&#146;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 &gt; 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>&#149; <B>See</B> &#147;Scheduling Commands with <TT>cron</TT> and <TT>crontab</TT>,&#148; <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:&nbsp;&nbsp;</B>The tar command by itself doesn&#146;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&#146;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&#146;t create a directory to put the files in&#151;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 + -