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

📄 ch20_04.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 20] 20.4 Restoring Files from Tape with tar </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly &amp; Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1998-10-23T15:44:52Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-260-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch20_01.htm"TITLE="20. Backing Up Files"><LINKREL="prev"HREF="ch20_03.htm"TITLE="20.3 How to Make Backups with a Local Tape Drive "><LINKREL="next"HREF="ch20_05.htm"TITLE="20.5 Using tar to a Remote Tape Drive "></HEAD><BODYBGCOLOR="#FFFFFF"TEXT="#000000"><DIVCLASS="htmlnav"><H1><IMGSRC="gifs/smbanner.gif"ALT="UNIX Power Tools"USEMAP="#srchmap"BORDER="0"></H1><MAPNAME="srchmap"><AREASHAPE="RECT"COORDS="0,0,466,58"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="467,0,514,18"HREF="jobjects/fsearch.htm"ALT="Search this book"></MAP><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch20_03.htm"TITLE="20.3 How to Make Backups with a Local Tape Drive "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 20.3 How to Make Backups with a Local Tape Drive "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 20<BR>Backing Up Files</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch20_05.htm"TITLE="20.5 Using tar to a Remote Tape Drive "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 20.5 Using tar to a Remote Tape Drive "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-0425">20.4 Restoring Files from Tape with tar </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-21966"></A><ACLASS="indexterm"NAME="AUTOID-21969"></A>When you create an archive, there are several ways to specify the directory.If the directory is under the current directory,you could type:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>tar c project</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">A similar way to specify the same directory is:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>tar c ./project</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">&#13;If you are currently in the directory you want archived, youcan type:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>tar c .</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">Another way to archive the current directory isto type:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>tar c *</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">Here, the shell expands the<CODECLASS="literal">*</CODE> (asterisk) to the files in the current directory.However, it does not match files starting with a <CODECLASS="literal">.</CODE> (dot),which is why the previous technique is preferred.</P><PCLASS="para">This causes a problem when restoring a directory from a<EMCLASS="emphasis">tar</EM>archive.You may not know if an archive was created using <CODECLASS="literal">.</CODE> or the directory name.</P><PCLASS="para">I always check the names of the files before restoring an archive:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>tar t</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">If the archive loads the files into the current directory,I create a new directory, change to it, and extract the files.</P><PCLASS="para">If the archive restores the directory by name, thenI restore the files into the current directory.</P><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-425-SECT-1.1">20.4.1 Restoring a Few Files </A></H3><PCLASS="para">If you want to restore a single file, get the pathnameof the file as<EMCLASS="emphasis">tar</EM>knows it, using the<ACLASS="indexterm"NAME="AUTOID-22003"></A><EMCLASS="emphasis">t</EM> flag.<ACLASS="indexterm"NAME="AUTOID-22007"></A>You must specify the exact filename, because<CODECLASS="replaceable"><I>filename</I></CODE>and <CODECLASS="literal">./</CODE><CODECLASS="replaceable"><I>filename</I></CODE>are not the same.You can combine these two steps into one commandby using [this may run very slowly<EMCLASS="emphasis">-JP</EM>&nbsp;]:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>tar xvf /dev/rst0 `tar tf /dev/rst0 | grep <CODECLASS="replaceable"><I>filename</I></CODE>`</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">Whenever you use<EMCLASS="emphasis">tar</EM>to restore a directory, you must alwaysspecify <EMCLASS="emphasis">some</EM>filename.If none is specified, no files are restored.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-22022"></A><ACLASS="indexterm"NAME="AUTOID-22025"></A>There is still the problem of restoring a directory whose pathnamestarts with <EMCLASS="emphasis">/</EM>&nbsp;(slash).Because<EMCLASS="emphasis">tar</EM>restores a file to the pathname specified in the archive,you cannot change <EMCLASS="emphasis">where</EM>the file will be restored.The danger is that either you may overwrite some existing files oryou will not be able to restore the files because you don'thave permission.</P><PCLASS="para">You can ask the system administrator to rename a directory andtemporarily create a symbolic link pointing to a directory where you can restore the files.Other solutions exist, including editing the<EMCLASS="emphasis">tar</EM>archive and creating a new directory structure with aC program executing the<EMCLASS="emphasis">chroot</EM>(2)system call.Another solution is to use the version from the<SPANCLASS="link">Free Software Foundation (<ACLASS="linkend"HREF="glossary.htm#UPT-ART-1010"TITLE="Glossary">52.9</A>)</SPAN>that allows you to remap pathnames starting with <EMCLASS="emphasis">/</EM> (slash).It also allows you to create archives that are too large for a single tape,incremental archives, and a dozen other advantages.This freely available version of <EMCLASS="emphasis">tar</EM> is also called<SPANCLASS="link">GNU <EMCLASS="emphasis">tar</EM> (<ACLASS="linkend"HREF="ch19_06.htm"TITLE="GNU tar Sampler ">19.6</A>)</SPAN>.(It's on the disc.)</P><PCLASS="para">But the best solution is to never create an archiveof a directory that starts with <CODECLASS="literal">/</CODE>&nbsp;(slash)<EMCLASS="emphasis">or</EM><SPANCLASS="link"><CODECLASS="literal">~</CODE> (tilde) (<ACLASS="linkend"HREF="ch14_11.htm"TITLE="Finding (Anyone's) Home Directory, Quickly ">14.11</A>)</SPAN>.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-425-SECT-1.2">20.4.2 Remote Restoring </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-22047"></A>To restore a directory from a remote host, use the following command:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch01_33.htm"TITLE="1.33 UNIX Networking and Communications ">rsh</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">%<CODECLASS="userinput"><B> rsh -n host dd if=/dev/rst0 bs=20b | tar xvBfb - 20 </B></CODE><CODECLASS="replaceable"><I>files</I></CODE></PRE></TD></TR></TABLE></P><PCLASS="para">Because of its nature, it is difficult to readfixed-size blocks over a network.This is why<EMCLASS="emphasis">tar</EM>uses the<EMCLASS="emphasis">B</EM>flag to force it to read from the pipeuntil a block is completely filled.[Some versions of <EMCLASS="emphasis">tar</EM>, like the one from<SPANCLASS="link">GNU (<ACLASS="linkend"HREF="glossary.htm#UPT-ART-1010"TITLE="Glossary">52.9</A>)</SPAN>on the CD-ROM,handle remote drives automatically. <EMCLASS="emphasis">-JIK</EM>&nbsp;]</P></DIV><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">BB</SPAN></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch20_03.htm"TITLE="20.3 How to Make Backups with a Local Tape Drive "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 20.3 How to Make Backups with a Local Tape Drive "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="book"HREF="index.htm"TITLE="UNIX Power Tools"><IMGSRC="gifs/txthome.gif"SRC="gifs/txthome.gif"ALT="UNIX Power Tools"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch20_05.htm"TITLE="20.5 Using tar to a Remote Tape Drive "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 20.5 Using tar to a Remote Tape Drive "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">20.3 How to Make Backups with a Local Tape Drive </TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="index"HREF="index/idx_0.htm"TITLE="Book Index"><IMGSRC="gifs/index.gif"SRC="gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172">20.5 Using tar to a Remote Tape Drive </TD></TR></TABLE><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><IMGSRC="gifs/smnavbar.gif"SRC="gifs/smnavbar.gif"USEMAP="#map"BORDER="0"ALT="The UNIX CD Bookshelf Navigation"><MAPNAME="map"><AREASHAPE="RECT"COORDS="0,0,73,21"HREF="../index.htm"ALT="The UNIX CD Bookshelf"><AREASHAPE="RECT"COORDS="74,0,163,21"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="164,0,257,21"HREF="../unixnut/index.htm"ALT="UNIX in a Nutshell"><AREASHAPE="RECT"COORDS="258,0,321,21"HREF="../vi/index.htm"ALT="Learning the vi Editor"><AREASHAPE="RECT"COORDS="322,0,378,21"HREF="../sedawk/index.htm"ALT="sed &amp; awk"><AREASHAPE="RECT"COORDS="379,0,438,21"HREF="../ksh/index.htm"ALT="Learning the Korn Shell"><AREASHAPE="RECT"COORDS="439,0,514,21"HREF="../lrnunix/index.htm"ALT="Learning the UNIX Operating System"></MAP></DIV></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -