📄 561-564.html
字号:
<HTML>
<HEAD>
<TITLE>Using Linux:System Maintenance</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=0789716232//-->
<!--TITLE=Using Linux//-->
<!--AUTHOR=William Ball//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Que//-->
<!--CHAPTER=31//-->
<!--PAGES=561-564//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="558-561.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="564-567.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>This command archives many of the files unique to most systems.
</P>
<TABLE BORDER="2" BORDERCOLOR="#0000" ALIGN="CENTER">
<TR><TD><FONT SIZE="+1"><B>The Linux Filesystem Standard describes the general layout of most Linux systems</B></FONT>
<BR>For more information about the standard configuration of most Linux filesystems, you can review the Linux Filesystem Standard (often abbreviated FSSTND) at <A HREF="http://www.pathname.com/fhs/">http://www.pathname.com/fhs/</A>.</TABLE>
<P>Again, if you cannot back up everything, make sure you are backing up everything important.
</P>
<P>Once you have a full backup, you can begin to take advantage of the <TT>tar</TT> and <TT>find</TT> command options to make backups of files that have changed since the last full backup (incremental backups).</P>
<P>To track the date of the last backup, sometimes it makes sense to store it in a file:</P>
<!-- CODE SNIP //-->
<PRE>
#ls -l lastarchive.tar | cut -f6-8 > lastarchive.date
</PRE>
<!-- END CODE SNIP //-->
<P>If you are backing up with <TT>tar</TT>, the <TT>-N</TT> option backs up only files with more recent access times than the date provided:</P>
<!-- CODE SNIP //-->
<PRE>
#tar -cz -g archlog.txt -N DATE -V 'latest backup’ /home
</PRE>
<!-- END CODE SNIP //-->
<P>The <TT>-g</TT> option creates a log file <TT>archlog.txt</TT>, which contains the archive time and a list of the directories archived. <TT>-V</TT> electronically labels the archive <TT>latest backup</TT>. The <TT>-N</TT> option specifies that <TT>tar</TT> is only to back up files newer than <TT>DATE</TT>, where the date is entered in the same format as the date command’s. So, if you know the date of the last archive, you could use this date to generate an incremental backup. (Actually, this command is a bit redundant. The <TT>-g</TT> archlog option looks for an existing file named <TT>archlog</TT> and, using the date recorded from the last backup in this file, instructs <TT>tar</TT> to archive only files newer than that date. So this command assumes you have not yet created a <TT>-g</TT> <TT>archlog</TT> file.)</P>
<P>If you need to archive files that could be anywhere on the system, the <TT>find</TT> command comes in handy:</P>
<!-- CODE SNIP //-->
<PRE>
#find / -atime -1 -print > /home/backuplists/todaysfiles
#tar -cvz -f /dev/rft0 -T /home/backuplists/todaysfiles
</PRE>
<!-- END CODE SNIP //-->
<P>These commands place an archive of files accessed within the last 24 hours on the tape mounted at <TT>/dev/rft0</TT>. (Make sure to change the tape each day if you need to store more than one day’s backup.)</P>
<P>You can use the <TT>crontab</TT> feature to avoid having to run this backup manually:</P>
<!-- CODE SNIP //-->
<PRE>
* 3 * * * find / -atime -1 -print > \
/home/backuplists/todaysfiles; mt -f /dev/rft0 rewind; \
tar -cvz -f /dev/rft0 -T /home/backuplists/todaysfiles
</PRE>
<!-- END CODE SNIP //-->
<P>You can also use shell scripts to automate backups. A simple example would be to take the <TT>cron</TT> bash commands in the preceding example and type them into a shell script called <TT>todaysbu</TT>. You could then just add the following to <TT>crontab</TT>:</P>
<!-- CODE SNIP //-->
<PRE>
* 3 * * * /home/backupscripts/todaysbu
</PRE>
<!-- END CODE SNIP //-->
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>SEE ALSO</B>
<DL>
<DD><B>•</B> For more information about using dates and <TT>crontab</TT> with Linux, see page 429.
<DD><B>•</B> For more information about using shell scripts to automate tasks, see page 365.
<DD><B>•</B> For more information about <TT>cron</TT>, see page 435.
</DL>
<HR></FONT>
</BLOCKQUOTE>
<H3><A NAME="Heading9"></A><FONT COLOR="#000077">Performing File System Maintenance</FONT></H3>
<P>It’s good to be in the habit of snooping around your system a little for unnecessary files. Besides keeping space free, it familiarizes you with the layout of your directories, and the way users (including you) are utilizing the system.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading10"></A><FONT COLOR="#000077">Deleting Unnecessary Files</FONT></H4>
<P>In addition to using <TT>find</TT> to locate files to back up, you can use it to search for files that are no longer needed. After you have found those files, you can delete them from the command line, or use an automated process (<TT>crontab</TT> entry or shell script) to remove them. (Warning: Make sure you back up any files you may <I>ever</I> need again before removing them.)</P>
<P>For instance, you can search for files belonging to users who no longer have access to the system, archive them to a tape, and delete the originals (make sure your backup is good):</P>
<!-- CODE SNIP //-->
<PRE>
#find / -user tb -print > tbfiles
#tar -cvz -f tbfile.tar.gz -T tbfiles
#find / -user tb -print0 | xargs -r0 rm
</PRE>
<!-- END CODE SNIP //-->
<P>The last command line makes use of the <TT>xargs</TT> utility. <TT>xargs</TT> processes each argument piped to it and runs a command on that argument. The <TT>find</TT> <TT>print0</TT> option separates filenames with a <TT>NULL</TT> (<TT>0</TT>) character instead of a <TT>newline</TT>, in case some files or directories have a <TT>newline</TT> in their names (unlikely, but better safe than sorry). The <TT>-r0</TT> options tell <TT>xargs</TT> to expect input separated by <TT>NULL</TT> characters (<TT>0</TT>), and make sure the command doesn’t run if the input is a blank line (<TT>r</TT>). To remove directories, use the following command:</P>
<!-- CODE SNIP //-->
<PRE>
#find / -user tb -print0 | xargs -r0 rmdir
</PRE>
<!-- END CODE SNIP //-->
<P>For more information about <TT>xargs</TT>, see its man page.</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>SEE ALSO</B>
<DL>
<DD><B>•</B> For more information about shell scripts, see page 365.
<DD><B>•</B> For more information about using <TT>crontab</TT>, see page 435.
<DD><B>•</B> For more information about the <TT>rm</TT> and <TT>rmdir</TT> commands, see page 42.
<DD><B>•</B> For more information about the <TT>tar</TT> command, see page 544.
</DL>
<HR></FONT>
</BLOCKQUOTE>
<H4 ALIGN="LEFT"><A NAME="Heading11"></A><FONT COLOR="#000077">Undeleting Files</FONT></H4>
<P>The only way to undelete files with Linux is to use a backup system like <TT>tar</TT> or <TT>taper</TT>. There is no Recycle Bin or Trash icon where deleted files are kept indefinitely. To examine archives for a lost file, use the <TT>tar</TT> command</P>
<!-- CODE SNIP //-->
<PRE>
#tar -tvz -f monthlybackup.tar.gz lostfile(s)
</PRE>
<!-- END CODE SNIP //-->
<P><TT>tar</TT> lists any files in the monthly backup matching <TT>lostfile(s)</TT>. To restore lost files, create a lost file directory and extract the designated files there (rather than trying to place them back in their original habitat immediately). Sometimes directory permissions or pathnames may have changed, and occasionally someone will have created a new file in the same place, with the same name, as the one you are restoring. Use the following commands:</P>
<!-- CODE SNIP //-->
<PRE>
#cd /home/me/restored_lostfiles
#tar -xvz -f monthlybackup.tar.gz lostfile(s)
</PRE>
<!-- END CODE SNIP //-->
<P>Then you can move the files back to the original places after you have verified that no conflicts or problems with file attributes exist.
</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>SEE ALSO</B>
<DL>
<DD><B>•</B> For more information about file permissions and attributes, see page 43.
<DD><B>•</B> For more information about the <TT>tar</TT> command, see page 544.
</DL>
<HR></FONT>
</BLOCKQUOTE>
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="558-561.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="564-567.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 + -