📄 231-234.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=231-234//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="229-231.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="../ch12/235-237.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>Another way to create the tar file is to start from data’s parent directory and specify the directory name as the thing to archive. Here’s the command sequence:
</P>
<!-- CODE SNIP //-->
<PRE>
$ <B>pwd</B>
/home/dave
$ <B>tar cvf data.tar data</B>
</PRE>
<!-- END CODE SNIP //-->
<P>This also creates an archive of the data directory, but it puts the directory entry as the first thing in the archive. This way, when the tar file is extracted, the first thing that’s created is the directory data, and all the files in data are placed in the data subdirectory.
</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>NOTE: </B>If you want to create a tar file of all the files in the directory, it’s a good idea to specify a different location for the tar file (other than the current directory). That way, if you try to archive all the files in the current directory, <TT>tar</TT> won’t get confused and try to add its tar file recursively to the tar that it’s creating.<HR></FONT>
</BLOCKQUOTE>
<H4 ALIGN="LEFT"><A NAME="Heading7"></A><FONT COLOR="#000077">Using <I>cpio</I>
</FONT></H4>
<P><TT>cpio</TT> is a general-purpose command for copying file archives. You can use it to create backups by using the <TT>-o</TT> option, or to restore files by using the <TT>-i</TT> option. It takes its input from standard input and sends its output to standard output.</P>
<P>The advantages of <TT>cpio</TT> include the following:</P>
<DL>
<DD><B>•</B> It can back up any set of files.
<DD><B>•</B> It can back up special files.
<DD><B>•</B> It stores information more efficiently than <TT>tar</TT>.
<DD><B>•</B> It skips bad sectors or bad blocks when restoring data.
<DD><B>•</B> Its backups can be restored on almost any Linux or UNIX system.
</DL>
<P>Some people find <TT>cpio</TT>’s syntax to be a bit more confusing than <TT>tar</TT>’s syntax. Also, to perform incremental backups, you have to do some shell programming.</P>
<P>Table 11.2 lists the commonly used options for <TT>cpio</TT>. See <TT>cpio</TT>’s man page for a complete description of the options you can use with this command.</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 11.2</B> Commonly Used Options for <I>cpio</I>
<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>-o</TT>
<TD>Copy out. Creates an archive on standard out.
<TR>
<TD VALIGN="TOP"><TT>-B</TT>
<TD>Blocks input or output at 5,120 bytes per record; useful for efficient storage on magnetic tape.
<TR>
<TD VALIGN="TOP"><TT>-i</TT>
<TD>Copy in. Extracts files from standard input. This is typically used when the standard input is the result of a copy out action of another <TT>cpio</TT> command.
<TR>
<TD><TT>-t</TT>
<TD>Creates a table of contents of the input.
<TR>
<TH COLSPAN="2"><HR>
</TABLE>
<P>The following list provides some examples of using <TT>cpio</TT> to back up and restore files:</P>
<DL>
<DD><B>•</B> The following command copies the files in the directory /home to the device /dev/fd0:
<!-- CODE SNIP //-->
<PRE>
ls /home | cpio -o > /dev/fd0
</PRE>
<!-- END CODE SNIP //-->
<DD><B>•</B> The following command extracts the files on the device /dev/fd0 and creates an index in the bkup.indx file:
<!-- CODE SNIP //-->
<PRE>
cpio -it < /dev/fd0 > bkup.indx
</PRE>
<!-- END CODE SNIP //-->
<DD><B>•</B> The following example uses the <TT>find</TT> command to create a list of all files in /home that have been modified in the last day:
<!-- CODE SNIP //-->
<PRE>
find /home -mtime 1 -type f -print | cpio -oB > /dev/fd0
</PRE>
<!-- END CODE SNIP //-->
<BR>The output of that command is piped to <TT>cpio</TT>, which creates an archive on /dev/fd0, where the data is stored at 5,120 bytes per record.
<DD><B>•</B> The following command restores the file /home/dave/notes.txt from the device /dev/fd0:
<!-- CODE SNIP //-->
<PRE>
echo "/home/dave/notes.txt" | cpio -i < /dev/fd0
</PRE>
<!-- END CODE SNIP //-->
</DL>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>NOTE: </B>You must give the complete filename to restore a file with <TT>cpio</TT>.<HR></FONT>
</BLOCKQUOTE>
<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 cron file to perform a daily backup of /home at 1:30 a.m.:
<!-- CODE SNIP //-->
<PRE>
30 01 * * * ls /home | cpio -o > /dev/fd0
</PRE>
<!-- END CODE SNIP //-->
<P>If you need to do more complicated backups, you can create shell scripts to control your backups. You also can run these shell scripts via <TT>cron</TT>.<HR></FONT>
</BLOCKQUOTE>
</P>
<H3><A NAME="Heading8"></A><FONT COLOR="#000077">From Here…</FONT></H3>
<P>You can find more information about system administration in the following chapters:
</P>
<DL>
<DD><B>•</B> Chapter 7, “Understanding System Administration,” gives an overview of the duties of a systems administrator.
<DD><B>•</B> Chapter 10, “Managing User Accounts,” shows how to create and manage user access to your Linux system.
<DD><B>•</B> Chapter 14, “Managing File Systems,” discusses the ins and outs of file systems and the various issues that you need to consider.
</DL>
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="229-231.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="../ch12/235-237.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 + -