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

📄 759-762.html

📁 linux-unix130.linux.and.unix.ebooks130 linux and unix ebookslinuxLearning Linux - Collection of 12 E
💻 HTML
字号:
<HTML>

<HEAD>

<TITLE>Linux Unleashed, Third Edition:Backups</TITLE>

<SCRIPT>
<!--
function displayWindow(url, width, height) {
        var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>

 -->




<!--ISBN=0672313723//-->

<!--TITLE=Linux Unleashed, Third Edition//-->

<!--AUTHOR=Tim Parker//-->

<!--PUBLISHER=Macmillan Computer Publishing//-->

<!--IMPRINT=Sams//-->

<!--CHAPTER=45//-->

<!--PAGES=759-762//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="757-759.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="../ch46/763-766.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<H3><A NAME="Heading6"></A><FONT COLOR="#000077">Using tar for Backups</FONT></H3>

<P>The <TT>tar</TT> (tape archiver) program is usually the command used to save files and directories to an archive medium and recover them later. The <TT>tar</TT> command works by creating an archive file, which is a single large entity that holds many files within it (much like PKZIP does in DOS, for example). The <TT>tar</TT> command only works with archives it creates.</P>

<P>The format of the command is a little awkward and takes some getting used to, but fortunately, there are only a few more variations that users will need. The format of the <TT>tar</TT> command is:</P>

<!-- CODE SNIP //-->

<PRE>

tar switch modifiers files

</PRE>

<!-- END CODE SNIP //-->

<P>The files section of the command indicates which files or directories you want to archive or restore. Usually this is a full file system such as <TT>/usr</TT> or in the case of recovery, a single file such as <TT>/usr/tparker/big_file</TT>.</P>

<P>The switch controls how <TT>tar</TT> reads or writes to the backup media. Only one switch at a time can be used with <TT>tar</TT>. The valid switches are:</P>

<DL>

<DD><B>&#149;</B>&nbsp;&nbsp;<TT>c</TT> create a new archive media

<DD><B>&#149;</B>&nbsp;&nbsp;<TT>r</TT> write to end of existing archive

<DD><B>&#149;</B>&nbsp;&nbsp;<TT>t</TT> lists names of files in an archives

<DD><B>&#149;</B>&nbsp;&nbsp;<TT>u</TT> files added if not modified or archived already

<DD><B>&#149;</B>&nbsp;&nbsp;<TT>x</TT> extract from the archive

</DL>

<P>A number of modifiers can be added to the <TT>tar</TT> command to control the archive and how <TT>tar</TT> uses it. Valid modifiers include:</P>

<DL>

<DD><B>&#149;</B>&nbsp;&nbsp;<TT>A</TT> suppress absolute filenames

<DD><B>&#149;</B>&nbsp;&nbsp;<TT>b</TT> provide a blocking factor (1-20)

<DD><B>&#149;</B>&nbsp;&nbsp;<TT>e</TT> prevent splitting files across volumes

<DD><B>&#149;</B>&nbsp;&nbsp;<TT>f</TT> specifies the archive media device name

<DD><B>&#149;</B>&nbsp;&nbsp;<TT>F</TT> specifies the name of a file for <TT>tar</TT> arguments

<DD><B>&#149;</B>&nbsp;&nbsp;<TT>k</TT> gives size of archive volume (in kilobytes)

<DD><B>&#149;</B>&nbsp;&nbsp;<TT>l</TT> displays error messages if links unresolved

<DD><B>&#149;</B>&nbsp;&nbsp;<TT>m</TT> does not restore modification times

<DD><B>&#149;</B>&nbsp;&nbsp;<TT>n</TT> indicates the archive is not a tape

<DD><B>&#149;</B>&nbsp;&nbsp;<TT>p</TT> extracts files with their original permissions

<DD><B>&#149;</B>&nbsp;&nbsp;<TT>v</TT> verbose output (lists files on the console)

<DD><B>&#149;</B>&nbsp;&nbsp;<TT>w</TT> displays archive action and waits for user confirmation

</DL>

<P>The <TT>tar</TT> command uses absolute pathnames for most actions, unless the &#147;A&#148; switch is specified.</P>

<P>A few examples will help explain the <TT>tar</TT> command and the use of <TT>tar</TT> switches. If you are using a tape drive called <TT>/dev/tape</TT> and the entire file system to be archived totals to less than the tape&#146;s capacity, you can create the tape archive with the command:</P>

<!-- CODE SNIP //-->

<PRE>

tar cf /dev/tape /

</PRE>

<!-- END CODE SNIP //-->

<P>The &#147;f&#148; option is used to specify the device name, in this case <TT>/dev/tape</TT>. The entire root file system is archived in a new archive file (indicated by the &#147;c&#148;). Any existing contents on the tape are automatically overwritten when the new archive is created (it does not ask if you are sure you want to delete the existing contents of the tape, so make sure you are overwriting material you don&#146;t need). If the &#147;v&#148; option is included in the command, <TT>tar</TT> echoes the filenames and their sizes to the console as they are archived.</P>

<P>Now, if you need to restore the entire file system from the tape used in the above example, issue the command:</P>

<!-- CODE SNIP //-->

<PRE>

tar xf /dev/tape

</PRE>

<!-- END CODE SNIP //-->

<P>This restores all files on the tape because no specific directory is indicated for recovery. The default, when no file or directory is specified, is the entire tape archive. On the other hand, if you want to restore a single file from the tape, the command is:

</P>

<!-- CODE SNIP //-->

<PRE>

tar xf /dev/tape /usr/tparker/big_file

</PRE>

<!-- END CODE SNIP //-->

<P>which restores only the file <TT>/usr/tparker/big_file</TT>.</P>

<P>Sometimes you may want to obtain a list of all files on a tape archive. You can do this with the command:</P>

<!-- CODE SNIP //-->

<PRE>

tar tvf /dev/tape

</PRE>

<!-- END CODE SNIP //-->

<P>This uses the &#147;v&#148; option to display the results from <TT>tar</TT>. If the list is long, you may want to redirect the command to a file.</P>

<P>Now that you&#146;ve seen the basic use of <TT>tar</TT>, we should look at some practical examples. Most tapes require a blocking factor when creating an archive, but with <TT>tar</TT>, specifying a blocking factor is not necessary when reading a tape because <TT>tar</TT> can figure it out automatically. The blocking factor tells <TT>tar</TT> how much data to write in a chunk on the tape. When archiving to a tape, the blocking factor is specified with the &#147;b&#148; modifier. For example, the command:</P>

<!-- CODE SNIP //-->

<PRE>

tar cvfb /dev/tape 20 /usr

</PRE>

<!-- END CODE SNIP //-->

<P>creates a new archive on <TT>/dev/tape</TT> with a blocking factor of 20 and contains all the files in <TT>/usr</TT>. Most tapes can use a blocking factor of 20, and this can be assumed a default value unless your tape drive specifically won&#146;t work with this value. The only other time blocking factors are changed is for floppy disks and other hard disk volumes. Note that the arguments following the modifiers are in the same order as the modifier. The &#147;f&#148; precedes the &#147;b&#148; modifier so the arguments have the device before the blocking factor. The arguments must be in the same order as the modifiers, which can sometimes cause a little confusion.</P>

<P>Another common problem is that a tape may not be large enough to hold the entire archive, in which case more than one tape is needed. To tell <TT>tar</TT> the size of each tape, you need the &#147;k&#148; option. It uses an argument that specifies the capacity in kilobytes. For example, the command:</P>

<!-- CODE SNIP //-->

<PRE>

tar cvbfk 20 /dev/tape 122880 /usr

</PRE>

<!-- END CODE SNIP //-->

<P>tells <TT>tar</TT> to use a blocking factor of &#147;20&#148; for the device <TT>/dev/tape</TT>. The tape capacity is 122,880 kilobytes (approx. 120MB). Again, note the order of arguments and the modifiers that match.</P>

<P>Floppy disks create another problem with <TT>tar</TT> because the blocking factor is usually different. When floppy disks are used, archives usually require more than one disk and the &#147;k&#148; option is used to specify the archive volumes capacity. For example, to back up the <TT>/usr/tparker</TT> directory to 1.2MB disks, the command is:</P>

<!-- CODE SNIP //-->

<PRE>

tar cnfk /dev/fd0 1200 /usr/tparker

</PRE>

<!-- END CODE SNIP //-->

<P>where /dev/fd0 is the device name of the floppy drive and 1200 is the size of the disk in kilobytes. The &#147;n&#148; modifier tells <TT>tar</TT> that this is not a tape, and <TT>tar</TT> will run a little more efficiently than if the modifier had been left off.</P>

<H3><A NAME="Heading7"></A><FONT COLOR="#000077">Summary</FONT></H3>

<P>This chapter looks at the basics of backups. You should maintain a backup log and make regular backups to protect your work. Although <TT>tar</TT> is a little awkward to use, once you&#146;ve tried it a few times it becomes almost second nature.</P>

<P>A number of scripts are beginning to appear that automate the backup process or give you a menu-driven interface to the backup system. These are not in general distribution, but you may want to check FTP and BBS sites for a utility that will simplify backups for you. From here, you can go to these chapters for more information. To learn about:</P>

<DL>

<DD>Automating the backup routines on your system, see Chapter 46.

<DD>Setting up your Linux system to serve the Internet, see Chapter 47, &#147;Setting up an Internet Site.&#148;

<DD>Applications you can run under Linux, see Chapter 62, &#147;Adabas-D and other Databases&#148; through Chapter 64, &#147;Lone Star Software&#146;s Lone-Tar.&#148;

</DL>

<P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="757-759.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="../ch46/763-766.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 + -