📄 ch23_09.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 23] 23.9 delete: Protecting Files from Accidental Deletion </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-08-04T21:41:05Z"><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="ch23_01.htm"TITLE="23. Removing Files"><LINKREL="prev"HREF="ch23_08.htm"TITLE="23.8 Safe Delete: Pros and Cons "><LINKREL="next"HREF="ch23_10.htm"TITLE="23.10 Deletion with Prejudice: rm -f "></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="ch23_08.htm"TITLE="23.8 Safe Delete: Pros and Cons "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 23.8 Safe Delete: Pros and Cons "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 23<BR>Removing Files</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch23_10.htm"TITLE="23.10 Deletion with Prejudice: rm -f "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 23.10 Deletion with Prejudice: rm -f "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-5520">23.9 delete: Protecting Files from Accidental Deletion </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="UPT-ART-5520-IX-DELETE-SCRIPT"></A><ACLASS="indexterm"NAME="UPT-ART-5520-IX-DELETING-FILES-SAFEGUARDS-AGAINST"></A>The problem of protecting users from accidental file deletion is onethat many people have encountered, and therefore there are manysolutions of different types already implemented and available.Which solution you choose depends onthe features you want it to have and on how you want it to do its job.Many people do not use theshell-script solutions described<SPANCLASS="link">above (<ACLASS="linkend"HREF="ch23_08.htm"TITLE="Safe Delete: Pros and Cons ">23.8</A>)</SPAN>,because they are too slow or too unreliable orbecause they don't allow deleted files to be recovered for longenough.</P><PCLASS="para">For example, Purdue University runs a large network of many differentmachines that utilize some local file space and some<SPANCLASS="link">NFS (<ACLASS="linkend"HREF="ch01_33.htm"TITLE="UNIX Networking and Communications ">1.33</A>)</SPAN>file space.<ACLASS="indexterm"NAME="UPT-ART-5520-IX-ENTOMB-SYSTEM"></A>Their file recovery system, <EMCLASS="emphasis">entomb</EM>, replaces certain system calls(for example, <EMCLASS="emphasis">open</EM>(2), <EMCLASS="emphasis">unlink</EM>(2)) with <EMCLASS="emphasis">entomb</EM> functions that check tosee if a file would be destroyed by the requested system call; if so,the file is backed up (by asking a local or remote <EMCLASS="emphasis">entomb</EM> daemonto do so) before the actual system call is performed.</P><PCLASS="para">The advantages of this system are that you don't have to create anynew applications to do safe file removal - the standard <EMCLASS="emphasis">rm</EM> programwill automatically do the right thing, as will <EMCLASS="emphasis">mv</EM> and any otherprograms that have the potential of erasing files.Even <EMCLASS="emphasis">cat a b > a</EM> is recoverable.</P><PCLASS="para">A disadvantage of this system is that you have to have the sourcecode for your UNIX system and be able to recompile its utilities inorder to link them against the <EMCLASS="emphasis">entomb</EM> libraries. Furthermore, ifyou wish to install this system on your machines, you have to be ableto install it on <EMCLASS="emphasis">all</EM> of them. If someone learns <EMCLASS="emphasis">entomb</EM> on amachine you manage and then wants to use it on a workstation in aprivate lab for which you do not have source code, it can't be done.Also, there is a danger of people getting used to <EMCLASS="emphasis">entomb</EM> beingthere to save them if they make mistakes, and then losing a file whenthey use <EMCLASS="emphasis">rm</EM> or <EMCLASS="emphasis">mv</EM> on a system that doesn't have <EMCLASS="emphasis">entomb</EM>.</P><PCLASS="para">If you don't have strict control over all the machines on which you want tohave file-deletion protection, or if you don't have source code andtherefore can't use something like <EMCLASS="emphasis">entomb</EM>, there are several otheroptions available. One of them is the <EMCLASS="emphasis">delete</EM> package,written at MIT.</P><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="programreference"HREF="examples/index.htm"TITLE="delete">delete</A><BR></TH><TDVALIGN="TOP"> <EMCLASS="emphasis">delete</EM> overcomes several of the disadvantages of <EMCLASS="emphasis">entomb</EM>. Itis very simple, compiles on virtually any machine, and doesn't requireany sort of superuser access to install. This means that if you learnto use <EMCLASS="emphasis">delete</EM> on one system and then move somewhere else, you cantake it with you by getting the source code and simply recompiling iton the new system. Furthermore, <EMCLASS="emphasis">delete</EM> intentionally isn't named<EMCLASS="emphasis">rm</EM>, so that people who use it know they are using it and thereforedon't end up believing that files removed with <EMCLASS="emphasis">rm</EM> can berecovered. However, this means that users have to be educated to use<EMCLASS="emphasis">delete</EM> instead of <EMCLASS="emphasis">rm</EM> when removing files.</TD></TR></TABLE><PCLASS="para"><EMCLASS="emphasis">delete</EM> works by renaming files with a prefix that marks them asdeleted. For example, <EMCLASS="emphasis">delete foo</EM> would simply rename thefile <EMCLASS="emphasis">foo</EM> to <EMCLASS="emphasis">.#foo</EM>. Here's an example of the <EMCLASS="emphasis">delete</EM>,<EMCLASS="emphasis">undelete</EM>, <EMCLASS="emphasis">lsdel</EM>, and <EMCLASS="emphasis">expunge</EM> commands in action:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"> <ACLASS="co"HREF="ch16_11.htm"TITLE="16.11 Showing Hidden Files with ls -A and -a ">-A</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen"><EMCLASS="emphasis">The directory starts with three files:</EM>% <CODECLASS="userinput"><B>ls</B></CODE>a b c <EMCLASS="emphasis">One of the files is deleted:</EM>% <CODECLASS="userinput"><B>delete a</B></CODE><EMCLASS="emphasis">The deleted file doesn't show up with normal ls because the namenow starts with a dot (.). However, it shows up when files starting with .are listed or when the lsdel command is used:</EM>% <CODECLASS="userinput"><B>ls</B></CODE>b c% <CODECLASS="userinput"><B>ls -A</B></CODE>.#a b c% <CODECLASS="userinput"><B>lsdel</B></CODE>a<EMCLASS="emphasis">Bringing the file back with undelete leaves us back where we started:</EM>% <CODECLASS="userinput"><B>undelete a</B></CODE>% <CODECLASS="userinput"><B>ls</B></CODE>a b c<EMCLASS="emphasis">We can delete everything:</EM>% <CODECLASS="userinput"><B>delete *</B></CODE>% <CODECLASS="userinput"><B>lsdel</B></CODE>a b c<EMCLASS="emphasis">We can expunge individual files or the current working directory:</EM>% <CODECLASS="userinput"><B>expunge a</B></CODE>% <CODECLASS="userinput"><B>lsdel</B></CODE>b c% <CODECLASS="userinput"><B>expunge</B></CODE><EMCLASS="emphasis">After the last expunge, there are no files left at all:</EM>% <CODECLASS="userinput"><B>lsdel</B></CODE>% <CODECLASS="userinput"><B>ls -A</B></CODE>%</PRE></TD></TR></TABLE></P><PCLASS="para">The technique used by <EMCLASS="emphasis">delete</EM> has some advantages and somedisadvantages. The advantages include:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para">It works on any filesystem type - local, NFS, AFS, RFS, whatever.You don't have to have special daemons running on your file serversin order for it to work, and there are no daemons to go down andprevent deleted file archiving from taking place.</P></LI><LICLASS="listitem"><PCLASS="para">It maintains the directory locations in which deleted files arestored so that they can be undeleted in the same locations. </P></LI><LICLASS="listitem"><PCLASS="para">It maintains file permissions and ownership so that undeleted filescan be restored with them. Furthermore, deleted files can beundeleted by anyone who had permission to delete them in the firstplace, not just by the one individual who deleted them.</P></LI></UL><PCLASS="para">Disadvantages include:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para">Deleted files are counted against a user's<SPANCLASS="link">disk quota (<ACLASS="linkend"HREF="ch24_17.htm"TITLE="Disk Quotas ">24.17</A>)</SPAN>until they areactually permanently removed (either by the system, a few days afterthey are deleted, or by the user with the <EMCLASS="emphasis">expunge</EM> command thatis part of the <EMCLASS="emphasis">delete</EM> package). Some people would actually callthis an advantage, because it prevents people from using deletedfile space to store large files (something which is possible with<EMCLASS="emphasis">entomb</EM>).</P></LI><LICLASS="listitem"><PCLASS="para">Deleted files show up when a user does <EMCLASS="emphasis">ls -a</EM>. This isconsidered a relatively minor disadvantage by most people,especially sincefiles starting with a dot (<CODECLASS="literal">.</CODE>) are supposed to be<SPANCLASS="link">hidden (<ACLASS="linkend"HREF="ch16_11.htm"TITLE="Showing Hidden Files with ls -A and -a ">16.11</A>)</SPAN>most of the time.</P></LI><LICLASS="listitem"><PCLASS="para">Deleted files have to be searched for in filesystem trees in orderto expunge them, rather than all residing in one location as they dowith <EMCLASS="emphasis">entomb</EM>. This, too, is usually considered a minordisadvantage, since most systems already<SPANCLASS="link">search the entire filesystem (<ACLASS="linkend"HREF="ch23_22.htm"TITLE="Using find to Clear Out Unneeded Files ">23.22</A>)</SPAN>each night automatically in order to delete certaintemporary files.</P></LI><LICLASS="listitem"><PCLASS="para">Only the <EMCLASS="emphasis">entomb</EM> program protects files. A user can still blowaway a file with <EMCLASS="emphasis">mv</EM>,<EMCLASS="emphasis">cat a b > a</EM>, etc.If your main concernis eliminating accidental file deletions with <EMCLASS="emphasis">rm</EM>, thisisn'tmuch of a problem; furthermore, it is not clear that the extraoverhead required to run something like <EMCLASS="emphasis">entomb</EM> is worth theadvantage gained (even if it is possible to do what <EMCLASS="emphasis">entomb</EM> needsat your site).</P></LI></UL><PCLASS="para"><EMCLASS="emphasis">entomb</EM> and <EMCLASS="emphasis">delete</EM> represent the two main approaches to theproblem of protection from accidental file erasure. Other packages ofthis sort choose one or the other of these basic techniques in orderto accomplish their purposes. <ACLASS="indexterm"NAME="AUTOID-25562"></A><ACLASS="indexterm"NAME="AUTOID-25563"></A><ACLASS="indexterm"NAME="AUTOID-25564"></A></P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">JIK</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="ch23_08.htm"TITLE="23.8 Safe Delete: Pros and Cons "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 23.8 Safe Delete: Pros and Cons "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="ch23_10.htm"TITLE="23.10 Deletion with Prejudice: rm -f "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 23.10 Deletion with Prejudice: rm -f "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">23.8 Safe Delete: Pros and Cons </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">23.10 Deletion with Prejudice: rm -f </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 + -