📄 ch20_06.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 20] 20.6 Writing a Tape Drive on a Remote Machine </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly & Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1998-10-23T15:44:55Z"><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_05.htm"TITLE="20.5 Using tar to a Remote Tape Drive "><LINKREL="next"HREF="ch20_07.htm"TITLE="20.7 Creating a Timestamp File for Selective Backups "></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_05.htm"TITLE="20.5 Using tar to a Remote Tape Drive "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 20.5 Using tar to a Remote 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_07.htm"TITLE="20.7 Creating a Timestamp File for Selective Backups "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 20.7 Creating a Timestamp File for Selective Backups "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-6050">20.6 Writing a Tape Drive on a Remote Machine </A></H2><PCLASS="para">[Bruce Barnett introduces this topic in article<ACLASS="xref"HREF="ch20_05.htm"TITLE="Using tar to a Remote Tape Drive ">20.5</A>.-JP]</P><PCLASS="para">In news posting <EMCLASS="emphasis"><5932@tahoe.unr.edu> malc@equinox.unr.edu</EM> (Malcolm Carlock) asked how to make <EMCLASS="emphasis">tar</EM> write a remote tape drive via<SPANCLASS="link"><EMCLASS="emphasis">rsh</EM> (<ACLASS="linkend"HREF="ch01_33.htm"TITLE="UNIX Networking and Communications ">1.33</A>)</SPAN>and<SPANCLASS="link"><EMCLASS="emphasis">dd</EM> (<ACLASS="linkend"HREF="ch35_06.htm"TITLE="Low-Level File Butchery with dd ">35.6</A>)</SPAN>.Here's the answer:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>tar cf - . | rsh foo dd of=/dev/</B></CODE><CODECLASS="replaceable"><I>device</I></CODE> <CODECLASS="userinput"><B>obs=20b</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-22135"></A><ACLASS="indexterm"NAME="AUTOID-22138"></A><ACLASS="indexterm"NAME="AUTOID-22141"></A><ACLASS="indexterm"NAME="AUTOID-22143"></A><ACLASS="indexterm"NAME="AUTOID-22146"></A>Be forewarned that most incarnations of <EMCLASS="emphasis">dd</EM> are extremely slow at handling this.</P><PCLASS="para">What is going on? This answer requires some background:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-22154"></A><ACLASS="indexterm"NAME="AUTOID-22156"></A><ACLASS="indexterm"NAME="AUTOID-22158"></A><ACLASS="indexterm"NAME="AUTOID-22160"></A>Tapes have "block sizes." Not all tapes, mind you - most SCSItapes have a fixed block size that can, for the most part, beignored. Nine-track tapes, however, typically record data in"records" separated by "gaps," and only whole records can bereread later.</P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-22164"></A><ACLASS="indexterm"NAME="AUTOID-22166"></A>In order to accommodate this, UNIX tape drivers generally translateeach <EMCLASS="emphasis">read( )</EM> or <EMCLASS="emphasis">write( )</EM> system call into a single record transfer.The size of a written record is the number of bytes passed to<EMCLASS="emphasis">write( )</EM>. (There may be some additional constraints, such as"the size must be even" or "the size must be no more than 32768 bytes." Note that phase-encoded (1600-bpi) blocks should be nolonger than 10240 bytes, and GCR (6250-bpi) blocks should be nolonger than 32768 bytes, to reduce the chance of an unrecoverableerror.) Each <EMCLASS="emphasis">read( )</EM> call must ask for at least one whole record(many drivers get this wrong and silently drop trailing portionsof a record that was longer than the byte count given to <EMCLASS="emphasis">read( )</EM>);each <EMCLASS="emphasis">read( )</EM> returns the actual number of bytes in the record.</P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-22176"></A>Network connections are generally "byte streams": the two host"peers" (above, the machine running <EMCLASS="emphasis">tar</EM>, and the machine with thetape drive) will exchange data but will drop any "record boundary"notion at the protocol-interface level. If record boundaries areto be preserved, this must be done in a layer above the networkprotocol itself. (Not all network protocols are stream-oriented,not even flow-controlled, error-recovering protocols. Internet RDPand XNS SPP are two examples of reliable record-oriented protocols.Many of these, however, impose fairly small record sizes.)</P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-22182"></A><EMCLASS="emphasis">rsh</EM> simply opens a stream protocol, and does no work to preserve"packet boundaries."</P></LI><LICLASS="listitem"><PCLASS="para"><EMCLASS="emphasis">dd</EM> works in mysterious ways:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">dd if=x of=y</PRE></BLOCKQUOTE></P><PCLASS="para">is the same as:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">dd if=x of=y ibs=512 obs=512</PRE></BLOCKQUOTE></P><PCLASS="para">which means: open files <EMCLASS="emphasis">x</EM> and <EMCLASS="emphasis">y</EM>, then loop doing <EMCLASS="emphasis">read(fd_x)</EM> with a byte count of 512, take whatever you got, copy it into an outputbuffer for file <EMCLASS="emphasis">y</EM>, and each time that buffer reaches 512 bytes,do a single <EMCLASS="emphasis">write(fd_y)</EM> with 512 bytes.</P><PCLASS="para">On the other hand:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">dd if=x of=y bs=512</PRE></BLOCKQUOTE></P><PCLASS="para">means something completely different: open files <EMCLASS="emphasis">x</EM> and <EMCLASS="emphasis">y</EM>, thenloop doing <EMCLASS="emphasis">read(fd_x)</EM> with a byte count of 512, take whateveryou got, and do a single <EMCLASS="emphasis">write(fd_y)</EM> with that count.All of this means that:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>tar cf - . | rsh otherhost dd of=/dev/</B></CODE><CODECLASS="replaceable"><I>device</I></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">will write 512-byte blocks (not what you wanted), while:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>tar cf - . | rsh otherhost dd of=/dev/</B></CODE><CODECLASS="replaceable"><I>device</I></CODE> <CODECLASS="userinput"><B>bs=20b</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">will be even worse: it will take whatever it gets from <EMCLASS="emphasis">stdin</EM>-which,being a TCP connection, will be arbitrarily lumpy depending on theunderlying network parameters and the particular TCP implementation - and write essentially random-sized records.On purely "local" (Ethernet) connections, with typical implementations, you will wind up with 1024-byte blocks (a <EMCLASS="emphasis">tar</EM> "block factor" of 2).</P></LI></UL><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-22221"></A>If a blocking factor of 2 is acceptable, and if <EMCLASS="emphasis">cat</EM>forces 1024-byte blocks (both true in some cases), you can use:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>tar cf - . | rsh otherhost "cat >/dev/</B></CODE><CODECLASS="replaceable"><I>device</I></CODE><CODECLASS="userinput"><B>"</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">but this depends on undocumented features in <EMCLASS="emphasis">cat</EM>. In any case, on<ACLASS="indexterm"NAME="AUTOID-22231"></A><ACLASS="indexterm"NAME="AUTOID-22233"></A><ACLASS="indexterm"NAME="AUTOID-22235"></A>nine-track tapes, since each gap occupies approximately 0.7 inches ofotherwise useful tape space, a block size of 1024 has ten times as manygaps as a block size of 10240, wasting 9x1600x0.7 = 10 kbytes oftape at 1600 bpi, or 32 times as many as a size of 32768, wasting31x6250x0.7 = 136 kbytes of tape at 6250 bpi.</P><PCLASS="para">I say "approximately" becauseactual gap sizes vary. In particular, certain "streaming" drives(all too often called streaming because they do not - in some casesthe controller is too "smart" to be able to keep up with the requireddata rate, even when fed back-to-back DMA requests) have been knownto stretch the gaps to 0.9 inches.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-22239"></A>In general, because of tape gaps, you should use the largest record sizethat permits error recovery. Note, however, that some olid [2]hardware (suchas that found on certain AT&T 3B systems) puts a ridiculous upper limit(5K) on tape blocks.</P><BLOCKQUOTECLASS="footnote"><PCLASS="para">[2] Go ahead, look it up... it is a perfectly good crossword puzzle word. <CODECLASS="literal">:-)</CODE></P></BLOCKQUOTE><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">CT</SPAN> <SPANCLASS="bibliomisc">in <ACLASS="systemitem.newsgroup"HREF="news:comp.unix.questions">comp.unix.questions</A> on Usenet, 3 April 1991</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_05.htm"TITLE="20.5 Using tar to a Remote Tape Drive "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 20.5 Using tar to a Remote 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_07.htm"TITLE="20.7 Creating a Timestamp File for Selective Backups "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 20.7 Creating a Timestamp File for Selective Backups "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">20.5 Using tar to a Remote 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.7 Creating a Timestamp File for Selective Backups </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 & 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 + -