织梦CMS - 轻松建站从此开始!

罗索

当前位置: 主页>杂项技术>PC常识>

避免误删文件:Linux回收站机制

落鹤生 发布于 2013-07-19 09:30 点击:次 
昨日凌晨精神恍惚,误删了在虚拟机中写的程序文件,谷歌度娘数据恢复方法失败,使昨天的工作功亏一篑,幸好程序改动不多。现准备在所有服务器用机制来解决误删问题。这样总比花时间恢复付出的代价小得多把。
TAG:

昨日凌晨精神恍惚,误删了在虚拟机中写的程序文件,谷歌度娘数据恢复方法失败,使昨天的工作功亏一篑,幸好程序改动不多。现准备在所有服务器用机制来解决误删问题。这样总比花时间恢复付出的代价小得多把。

1、编写回收站脚本程序

  1. [root@SlaveA data]# cat /bin/rm.sh    
  2. #!/bin/sh   
  3. # Author Steven   
  4. # Modify 20120716   
  5. dirpath=/data/Recycle # 选择回收站所在的分区目录  
  6. now=`date +%Y%m%d_%H_%M_%S_`    
  7. delfile=`basename $1` 
  8. filename=${now}${delfile}  # 给已经删除的文件加一个时间前缀来标识删除时的精准时间  
  9. if [ ! -d ${dirpath} ];then   
  10.         /bin/mkdir -p ${dirpath}   
  11. fi   
  12. /bin/mv $1 ${dirpath}/${filename}   

2、其他步骤

chmod 755 /bin/rm.sh

echo "alias rm='/bin/rm.sh'" >> /etc/bashrc 

3、删除文件测试

rm  /root/text.txt

此时,text.txt文件就会被mv 到 回收站目录 /data/Recycle

4、删除回收站文件

/bin/rm -rf /data/Recycle/text.txt

5、回收站机制是为了解决不小心误删问题,如果确定某个文件永久删除则直接删除

/bin/rm -rf  filename

6、为防止回收站目录遗留文件过多而占用太多的硬盘资源,使用crontab定时删除历史文件

a、编写定时删除回收站文件程序脚本

  1. [root@localhost ~]# cat clean_recycle.sh   
  2. #!/bin/sh  
  3. # Author Steven  
  4. # Modify 20130305  
  5.   
  6. dirpath=/data/Recycle/  
  7. ago=`date -d "-15 day" +%Y%m%d`  
  8.   
  9. if [ ! -d $dirpath ];then  
  10.         echo "This path [${dirpath}] not exist, please check."  
  11.         exit  
  12. fi  
  13.   
  14. for i in `ls $dirpath`  
  15. do  
  16.         # Get datestamp and check it. For example: 20130304_09_54_25_ld.lock   
  17.         datestamp=`echo $i | awk -F'_' '{print $1}'`  
  18.         check=`echo "$datestamp" | grep "^[0-9]\{8\}$"`  
  19.   
  20.         if [[ `echo $check` -ne "" ]];then  
  21.                 # Remove old files.  
  22.                 if [ "$datestamp" -lt "$ago" ];then  
  23.                         /bin/rm -rf $dirpath/$i  
  24.                 fi  
  25.         fi  
  26. done  

b、添加计划任务

crontab -e

1 5 * * 0 sh /root/clean_recycle.sh

7、失误是无法避免的,我们猜不到失误会在何时,何地,何种情况下发生。既然有这种因素存在,能用机制解决就用机制解决把。

(johnsteven)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201307/16700.html]
本文出处:51cto 作者:johnsteven 原文
顶一下
(1)
100%
踩一下
(0)
0%
------分隔线----------------------------
相关文章
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容