在Linux上实现 Windows 回收站功能的脚本


由于公司里的备份软件是每天晚上执行一次,如果员工当天不小心删除了文件,那只能回复到昨天的状态,如果是员工今天花了一天才写完的CODE,那就没法恢复 了,这样就员工一天的工作就白干了,当然你可能会说,这个员工活该,谁让他不小心了,但是当事情发生了之后,人家就会来找你解决了,虽然 你平常躲在角落里听歌、看电影网上看美女没人管,但是当真正有了问题后人家第一个想到的还是你, 这个时候你总不能跟人家你不会吧,这可是体现你技术水平的时候,呵呵,你是这些stupid 的技术人员的最后救命稻草呀……,那就把文件给他恢复回来,把下面的脚本安装到你的 Linux系统上就好了,呵呵,Solaris 和AIX还有其它UNIX不知道好不好使,反正只要rm 命令用法是一样的,那就没问题!

########################################################################################
作者:李杰
发布时间: 2010-05-07
版本:1.0

脚本功能:
1.   在用户执行rm命令时,将文件移动/dump-file/ ,被 移动到dump-file 目录下的文件、文件夹以原文件名+删除除的日期格式存放,如 weather.txt 在删除后就会被自动移动到/dump-file/ 下并命名为weather.txt_2010-05-07_14:46:51.bak
2.   此命令在执行时会检测文件或目录的大小,如果文件或目录超过2GB,将不会再将文件移动到回收站,而会直接删除,此功能是为了避免回收站过大而对系统造成影响

安装需求:
1.创建删除文件存放目录mkdir /dump-file/ && chmod -R 777 /dump-file
2.创建日志输出文件 touch /tmp/rm.dump && chmod -R 777 /tmp/rm.dump
3.将系统rm命令移动成 rm.bak  : mv /bin/rm /bin/rm.bak
4.将此脚本命名为/bin/rm 并给予可执行权限 :chmod 644 /bin/rm 


#!/bin/bash
DumpFile=/dump-file/
File=`echo $line|awk '{print $2}'`
aa=$(echo $1|grep "^-")
###
function TestDir(){

Test_Dir=$(echo $line|grep "/") #test if is a directory.
Test_Dir2=$(echo $line |awk '{print $2}'|grep "^/") # Test if the prameter starts with "/".
Dir2=$(echo $line |awk -F/ '{print $2}') #Extract the prameter after the first "/".
Dir=$(echo $line |awk '{print $2}'|awk -F/ '{print $1}') ##Extract the prameter before the first "/".
D1=`date +%F_%H:%M:%S.bak`
Date=`echo $line | awk '{print $2 "_" D1}' D1=$D1`

        if [[ $Test_Dir != "" ]] ;then #If parameters include directory
                if [[ $Test_Dir2 != "" ]] ;then #If parameter starts with "/",as formate /a/b/c/
                        echo $line "start with /"
                        mkdir $DumpFile$Dir2"_"$D1 2>/tmp/rm.dump # Create directory as format /dump-file/(first directory after "/")
                      
                        mv `echo $line|awk '{print $2}'` $DumpFile$Dir2"_"$D1
                        echo $DumpFile$Dir2"_"$D1
                else #Means that parameter starts without "/" ,as format a/b/c
                        mkdir $DumpFile$Dir$D1 #Create directory as format /dump-file/(first directory before "/")
                        mv `echo $line|awk '{print $2}'` $DumpFile$Dir$D1
                        echo $line "starts before /"
                fi
        else
                Date=`echo $line | awk '{print $2 "_" D1}' D1=$D1`
                mv `echo $line|awk '{print $2}'` $DumpFile$Date
        fi

}
###
###
if [[ "$1" != "" ]] ;then #If the frist parameter is not empty.

        if [[ $aa != "$1" ]];then #If the first parameter not start with "-".
                du -s "$@" 2>/tmp/rm.dump |while read line
                do
                        #TestDir
                        size=`echo $line | awk '$1 >2000000{print $1}'`
                        if [ "$size" > "1900000" ];then
                                echo "Lager than 2Mb"
                        else
                                TestDir
                        fi
                done

        else
                if [[ "$2" != ""  ]]; then
                       until [ "$2" == ""  ]
                       do
                                 #echo "function is running"
                                 du -s $2 2> /tmp/rm.dump |while read line
                                 do
                                        size=`echo $line | awk '$1 >2000000{print $1}'`
                                        if [ "$size" > "1900000" ];then
                                                echo "Lager than 2Mb"
                                                rm.bak -rf $2
                                        else
                                                TestDir

                                        fi
                                        shift
                                 done
                                 shift
                        done
                else
                        echo -e "No file detected./nTry 'rm --help' for more information. "
                fi
        fi
else
        echo -e "rm: missing operand /nTry 'rm --help' for more information."
fi


--------------------------------------------------------------------------------

修订版本:
BUG现象:在安装了LINUX���的回收站后发现无法使用less命令,原因是less  会调用rm将less产生的在/tmp下临时文件删除,
解决方法:
加了一层判断,不对/tmp下的文件执行备份操作,而是直接删除
安装方法:
cp -rp /bin/rm /bin/rm.bak  #将系统原有rm 命令改成rm.bak,记住 一定要改成rm.bak,因为回收站脚本需要调用 rm.bak这个命令。
cp -rp rm.sh /bin/rm #将你的脚本文件rm.sh 覆盖成/bin/rm文件
echo "0 8  * * 6   /bin/sh  /fmnp/soft/clean-dump.sh " >>/var/spool/cron/tabs/root # 为了防止 /dump-file目录过大,采取每周进行一次删除操作,脚本如下:
    #!/bin/bash

    /bin/rm.bak -rf /dump-file/*
    /bin/rm.bak -rf /tmp/*.rm-dump
#########################################

mkdir /dump-file  &&chmod -R 777 /dump-file # 创建/dump-file 目录并给予所有用户可写的权限 。

更新后脚本:

#!/bin/bash
DumpFile=/dump-file/
File=`echo $line|awk '{print $2}'`
aa=$(echo $1|grep "^-")
DumpLog="`whoami`-`date +%m-%d-%H-%M`.rm-dump"

###

function TestDir(){

Test_Dir=$(echo $line|grep "/") #test if is a directory.
Test_Dir2=$(echo $line |awk '{print $2}'|grep "^/") # Test if the prameter starts with "/".
Test_Dir3=$(echo $line |awk '{print $2}'|grep "^/tmp") #
Dir2=$(echo $line |awk -F/ '{print $2}') #Extract the prameter after the first "/".
Dir=$(echo $line |awk '{print $2}'|awk -F/ '{print $1}') ##Extract the prameter before the first "/".
D1=`date +%F_%H:%M:%S.bak`
Date=`echo $line | awk '{print $2 "_" D1}' D1=$D1`
DumpLog="`whoami`-`date +%m-%d-%H-%M`.rm-dump"

        if [[ $Test_Dir != "" ]] ;then #If parameters include directory
                if [[ $Test_Dir2 != "" ]] ;then #If parameter starts with "/",as formate /a/b/c/
                   if [[ $Test_Dir3 != "" ]];then # this test is for to repaire less command's error.
                        rm.bak -rf `echo $line|awk '{print $2}'`
                   else
                        #echo $line "start with /"
                        mkdir $DumpFile$Dir2"_"$D1 2>/tmp/$DumpLog # Create directory as format /dump-file/(first directory after "/")
                       
                        mv `echo $line|awk '{print $2}'` $DumpFile$Dir2"_"$D1
                        echo $DumpFile$Dir2"_"$D1
                   fi
                else #Means that parameter starts without "/" ,as format a/b/c
                        mkdir $DumpFile$Dir$D1 #Create directory as format /dump-file/(first directory before "/")
                        mv `echo $line|awk '{print $2}'` $DumpFile$Dir$D1
                        echo $line "starts before /"
                fi
        else
                Date=`echo $line | awk '{print $2 "_" D1}' D1=$D1`
                mv `echo $line|awk '{print $2}'` $DumpFile$Date
        fi

}
###
###
if [[ "$1" != "" ]] ;then #If the frist parameter is not empty.

        if [[ $aa != "$1" ]];then #If the first parameter not start with "-".
                du -s "$@" 2>/tmp/$DumpLog |while read line
                do
                        size=`echo $line | awk '$1 >2000000{print $1}'`
                        if [ "$size" > "1900000" ];then
                                #echo "Lager than 2Mb"
                                rm.bak -rf `echo $line|awk '{print $2}'`
                        else
                                TestDir
                        fi
                done

        else
                if [[ "$2" != ""  ]]; then
                       until [ "$2" == ""  ]
                       do
                                 #echo "function is running"
                                 du -s $2 2> /tmp/$DumpLog |while read line
                                 do
                                        size=`echo $line | awk '$1 >2000000{print $1}'`
                                        if [ "$size" > "1900000" ];then
                                                #echo "Lager than 2Mb"
                                                rm.bak -rf $2
                                        else
                                                TestDir

                                        fi
                                        shift
                                 done
                                 shift
                        done
                else
                        echo -e "No file detected./nTry 'rm --help' for more information. "
                fi
        fi
else
        echo -e "rm: missing operand /nTry 'rm --help' for more information."
fi 

相关内容