避免誤刪文件:Linux回收站機制(升級版)

昨日凌晨精神恍惚,誤刪了在虛擬機中寫的程序文件,谷歌度娘數據恢復方法失敗,使昨天的工作功虧一簣,幸好程序改動不多。現準備在所有服務器用機制來解決誤刪問題。這樣總比花時間恢復付出的代價小得多把。

腳本說明:

隨意用法:rm -rf /data/test* /data/00000  /data/023-rf-r-f  /home/steven/dddd  /home/steven/  dirdir/ /home/steven/test* ddd

rm -rf *

禁止刪除 / 和根下面的重要目錄(rm.py中的deny變量內容)


開發平臺:CentOS6.2 X86_64

1、編寫回收站腳本程序 rm.py


#!/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 列表中包含的是禁止刪除的目錄或文件,可以根據自己的需要來增減。


2、其他步驟

chmod 755 /bin/rm.py

chmod 777 /data/Recycle  支持一般用戶rm

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

source /etc/bashrc 回收站生效


3、刪除文件測試

rm  /root/text.txt

此時,text.txt文件就會被mv 到 回收站目錄 /data/Recycle


遠程腳本調用時,須用全路徑:/bin/rm.py

4、刪除回收站文件

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

5、回收站機制是爲了解決不小心誤刪問題,如果確定某個文件永久刪除則直接刪除

/bin/rm -rf  filename

6、爲防止回收站目錄遺留文件過多而佔用太多的硬盤資源,使用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

7、失誤是無法避免的,我們猜不到失誤會在何時,何地,何種情況下發生。既然有這種因素存在,能用機制解決就用機制解決把。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章