避免誤刪檔案:Linux資源回收筒機制 昨日淩晨精神恍惚,誤刪了在虛擬機器中寫的程式檔案,Google度娘資料恢複方法失敗,使昨天的工作功虧一簣,幸好程式改動不多。現準備在所有伺服器用機制來解決誤刪問題。這樣總比花時間恢複付出的代價小得多把。1、編寫資源回收筒指令碼程式[root@SlaveA data]# cat /bin/rm.sh #!/bin/sh # Author steven # Modify 20120709 dirpath=/data/Recycle # 選擇資源回收筒所在的分區目錄now=`date +%Y%m%d_%H_%M_%S_` filename=${now}$1 # 給已經刪除的檔案加一個時間首碼來標識刪除時的精準時間if [ ! -d ${dirpath} ];then /bin/mkdir -p ${dirpath} fi www.2cto.com /bin/mv $1 ${dirpath}/${filename}
2、其他步驟chmod 755 /bin/rm.shecho "alias rm='/bin/rm.sh'" >> /etc/bashrc 3、刪除檔案測試rm /root/text.txt此時,text.txt檔案就會被mv 到 資源回收筒目錄 /data/Recycle4、刪除資源回收筒檔案/bin/rm -rf /data/Recycle/text.txt5、資源回收筒機制是為瞭解決不小心誤刪問題,如果確定某個檔案永久刪除則直接刪除/bin/rm -rf filename
6、為防止資源回收筒目錄遺留檔案過多而佔用太多的硬碟資源,使用crontab定時刪除曆史檔案a、編寫定時刪除資源回收筒檔案程式指令碼[root@SlaveA ~]# cat clean_recycle.sh #!/bin/sh www.2cto.com # AUthor steven # Modify 20120709 dirpath=/data/Recycle/ /bin/find ${dirpath} -mtime +30 -exec /bin/rm -rf {} \; # 30 天#注意:此處是 /bin/rm -rfb、添加計劃任務crontab -e1 5 * * 0 sh /root/clean_recycle.sh7、失誤是無法避免的,我們猜不到失誤會在何時,何地,何種情況下發生。既然有這種因素存在,能用機制解決就用機制解決把。 作者 jie737421