昨日凌晨精神恍惚,誤刪了在虛擬機中寫的程序文件,谷歌度娘數據恢復方法失敗,使昨天的工做功虧一簣,幸虧程序改動很少。現準備在全部服務器用機制來解決誤刪問題。這樣總比花時間恢復付出的代價小得多把。python
腳本說明:bash
隨意用法:rm -rf /data/test* /data/00000 /data/023-rf-r-f /home/steven/dddd /home/steven/ dirdir/ /home/steven/test* ddd服務器
rm -rf * ide
禁止刪除 / 和根下面的重要目錄(rm.py中的deny變量內容)測試
開發平臺:CentOS6.2 X86_64this
一、編寫回收站腳本程序 rm.pycrontab
#!/usr/bin/env python # Linux Recycle # Author Steven # Modify 2013-12-18 import time,sys,os,shutil recy_path = "/data/Recycle/" now = time.strftime('%Y%m%d_%H_%M_%S',time.localtime(time.time())) if not os.path.isdir(recy_path): os.makedirs(recy_path) para = sys.argv[1:] if '-f' in para: para.remove('-f') if '-r' in para: para.remove('-r') if '-rf' in para: para.remove('-rf') deny = ['/usr','/usr/','/var','/var/','/root','/root/','/etc','/etc/','/home','/home/','/proc','/proc/','/boot','/boot/','/bin','/bin/','/sbin','/sbin/','/lib','/lib/','/ lib64','/lib64/','/'] for p in tuple(para): if p in deny: para.remove(p) print "Dangerous command: %s" % p if not para: sys.exit('Nothing to run.') for f in para: try: if '/' not in f: recy_file = "%s/%s_%s" % (recy_path,now,f) shutil.move(f,recy_file) else: path = os.path.dirname(f) file = os.path.basename(f) if file == "": file = path.split('/')[-1] recy_file = "%s/%s_%s" % (recy_path,now,file) shutil.move(f,recy_file) except Exception,e: print e
提示:deny 列表中包含的是禁止刪除的目錄或文件,能夠根據本身的須要來增減。ip
二、其餘步驟資源
chmod 755 /bin/rm.py開發
chmod 777 /data/Recycle 支持通常用戶rm
echo "alias rm='/bin/rm.py'" >> /etc/bashrc
source /etc/bashrc 回收站生效
三、刪除文件測試
rm /root/text.txt
此時,text.txt文件就會被mv 到 回收站目錄 /data/Recycle
遠程腳本調用時,須用全路徑:/bin/rm.py
四、刪除回收站文件
/bin/rm -rf /data/Recycle/text.txt
五、回收站機制是爲了解決不當心誤刪問題,若是肯定某個文件永久刪除則直接刪除
/bin/rm -rf filename
六、爲防止回收站目錄遺留文件過多而佔用太多的硬盤資源,使用crontab定時刪除歷史文件
a、編寫定時刪除回收站文件程序腳本
[root@master bin]# cat /root/clean_recycle.sh #!/bin/sh # Author Steven # Modify 20131218 if [ `whoami` != 'root' ];then echo "Must be root run this scripts!!" >> /var/log/messages exit fi dirpath=/data/Recycle/ ago=`date -d "-15 day" +%Y%m%d` if [ ! -d $dirpath ];then echo "This path [${dirpath}] not exist, please check." >> /var/log/messages 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
c、給文件加鎖,避免被修改。
chattr +i /bin/rm.py
chattr +i /root/clean_recycle.sh
d、回收站內容
[root@master ~]# ls /data/Recycle/ 20131217_19_01_26_ 20131217_19_05_48_test33.txt 20131217_19_19_30_test0df-.211.txt 20131217_19_20_50_test1.txt 20131217_19_05_48_ 20131217_19_05_48_test3436.txt 20131217_19_19_30_test1.txt 20131217_19_20_50_test322.txt 20131217_19_05_48_000 20131217_19_05_48_testdd.txt 20131217_19_19_30_test322.txt 20131217_19_20_50_test33.txt 20131217_19_05_48_0234-f12-rf 20131217_19_05_48_testdgg.txt 20131217_19_19_30_test33.txt 20131217_19_20_50_test3436.txt 20131217_19_05_48_dddd 20131217_19_05_48_testg.txt 20131217_19_19_30_test3436.txt
七、失誤是沒法避免的,咱們猜不到失誤會在什麼時候,何地,何種狀況下發生。既然有這種因素存在,能用機制解決就用機制解決把。