linux 恢复回收站,避免误删文件: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、失误是无法避免的,我们猜不到失误会在何时,何地,何种情况下发生。既然有这种因素存在,能用机制解决就用机制解决把。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值