昨日凌晨精神恍惚,误删了在虚拟机中写的程序文件,谷歌度娘数据恢复方法失败,使昨天的工作功亏一篑,幸好程序改动不多。现准备在所有服务器用机制来解决误删问题。这样总比花时间恢复付出的代价小得多把。
1、编写回收站脚本程序
- [root@SlaveA data]# cat /bin/rm.sh
- #!/bin/sh
- # Author Steven
- # Modify 20120716
- dirpath=/data/Recycle # 选择回收站所在的分区目录
- now=`date +%Y%m%d_%H_%M_%S_`
- delfile=`basename $1`
- filename=${now}${delfile} # 给已经删除的文件加一个时间前缀来标识删除时的精准时间
- if [ ! -d ${dirpath} ];then
- /bin/mkdir -p ${dirpath}
- fi
- /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、编写定时删除回收站文件程序脚本
- [root@localhost ~]# cat clean_recycle.sh
- #!/bin/sh
- # Author Steven
- # Modify 20130305
-
- dirpath=/data/Recycle/
- ago=`date -d "-15 day" +%Y%m%d`
-
- if [ ! -d $dirpath ];then
- echo "This path [${dirpath}] not exist, please check."
- exit
- fi
-
- for i in `ls $dirpath`
- do
- # Get datestamp and check it. For example: 20130304_09_54_25_ld.lock
- datestamp=`echo $i | awk -F'_' '{print $1}'`
- check=`echo "$datestamp" | grep "^[0-9]\{8\}$"`
-
- if [[ `echo $check` -ne "" ]];then
- # Remove old files.
- if [ "$datestamp" -lt "$ago" ];then
- /bin/rm -rf $dirpath/$i
- fi
- fi
- done
b、添加计划任务
crontab -e
1 5 * * 0 sh /root/clean_recycle.sh
7、失误是无法避免的,我们猜不到失误会在何时,何地,何种情况下发生。既然有这种因素存在,能用机制解决就用机制解决把。
(johnsteven) |