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


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

1、编写回收站脚本程序

  1. [root@SlaveA data]# cat /bin/rm.sh  
  2. #!/bin/sh 
  3. # Author steven 
  4. Modify 20120709 
  5. dirpath=/data/Recycle # 选择回收站所在的分区目录
  6. now=`date +%Y%m%d_%H_%M_%S_` 
  7. filename=${now}$1  # 给已经删除的文件加一个时间前缀来标识删除时的精准时间
  8. if [ ! -d ${dirpath} ];then 
  9.         /bin/mkdir -p ${dirpath} 
  10. fi 
  11. /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@SlaveA ~]# cat clean_recycle.sh  
  2. #!/bin/sh 
  3. # AUthor steven 
  4. Modify 20120709 
  5. dirpath=/data/Recycle/ 
  6. /bin/find ${dirpath} -mtime +30  -exec /bin/rm -rf {} \;  # 30 天
  7. #注意:此处是 /bin/rm -rf
     

b、添加计划任务

crontab -e

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

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

相关内容