在系统运维过程中“Linux系统上常见的误操作”之首就是删除rm操作(特别是rm -rf),这些被误删的文件将无法被取回,那么是否有办法解决这个问题呢?目前有两种解决方案:
1. 第一种是如果你在误删除文件以后,若没有对硬盘做大量的IO(也就是硬盘上存放该被删除文件的设备块没被新的数据所覆盖),那么可借助一些第三方软件支持从硬盘上直接还原文件,在Linux有这方面的第三方软件,类似windows平台下的FinalData。
2. 第二种则是在你每次删除文件时,都将被删除的文件复制一份到指定的目录下,在你发现误删文件后能够从该目录下取回数据,这就相当于windows下的回收站了。
这篇文章将介绍的则是第二种解决方案,其优缺点都是非常明显的,优点是:可以确保每次删除的文件都能够找回;缺点就是:需要耗费相当一部分硬盘空间来存放被删除的文件;同时需要在每次删除文件时都必须通过这个删除脚本来处理。
下面看看这个Linux Shell脚本,既然其优缺点都非常明显,但这妨碍我们学习这种解决问题的思路……
[code lang=”shell”]
#!/bin/bash
from1=1
from2=2
garbage=HOME/.garbage
mvlog=garbage/mv.log
if [ ! -e garbage ]
then
mkdir -pgarbage
chmod 777 garbage
fi
function rand
{
a=(0 1 2 3 4 5 6 7 8 9 a b c d e A B C D E F )
for ((i=0;i>mvlog
mv "from1" "garbage/from1:random"
fi
}
function more
{
for file in *
do
echo "`pwd`/:file:random:`date`" >> mvlog
mvfile "garbage/file:random"
done 2> /dev/null
}
function rmi
{
if [ ! -d "from2" ]
then
echo -n "rm:remove regular empty file ‘from2′?" ; read answer;
if [ "answer" = ‘y’ -o "answer" = ‘Y’ ]
then
echo "`pwd`/:from2:random:`date`" >>mvlog
mv "from2" "garbage/from2:random"
fi
else
echo "rm: cannot remove directory ‘from2′: Is a directory"
fi
}
function rmf
{
if [ ! -d "from2" ]
then
echo "`pwd`/:from2:random:`date`" >> mvlog
mv "from2" "garbage/from2:random"
else
echo "rm: cannot remove directory ‘from2′: Is a directory"
fi
}
function rmr
{
if [ -e "from2" ]
then
result=(echo from2 | sed ‘s/////g’)
echo "`pwd`/:result:random:`date`" >>mvlog
mv "result" "garbage/result:random"
fi
}
function rml
{
while :
do
clear
line=(cat -nmvlog | awk -F : ‘{print 1,"FileName:"2, "Time:"4}’)
linecount=(cat mvlog | wc -l)
echo -e "line/c"
echo
echo
echo "Please input number you want revent(line count:linecount)–exit(e)"
read answer
if [ "answer" = e -o "answer" = E ]
then
break
else
(
echo "please input y(sure:)"
read answer1
if [ "answer1" = y -o "answer" = Y ]
then
address=(sed -n "answer""p"mvlog | awk -F : ‘{print 1}’)
filename=(sed -n "answer""p"mvlog | awk -F : ‘{print 2}’)
filerand=(sed -n "answer""p"mvlog | awk -F : ‘{print 3}’)
fullname=addressfilename
if [ -e "fullname" ]
then
echo "The file exist!"
sleep 1
else
old="garbage/filename:filerand"
new="addressfilename"
mv "old" "new"
delline=( cat mvlog | sed "answer""d" | sort -o mvlog)
echo "update ok!!!"
sleep 1
fi
fi
)
fi
done
}
function help
{
echo "
-i) If you wants delete some file , this function is confirm you want,the same as old rm.
-f) If you wants delete some directory ,you can use this function ,the same as old rm.
-r) If you wants delete some directory of file ,this function can use , the same as old rm.
-l) This is new function,is you wants resume some file or directory you can use this function,
first this function can list some file in you garbage , these have some number ,if you
wants resume 1,you can input 1 and then input y to confirm.
If you want add some function or some new idear please contact me…
author:wds
email:7717060@sina.com
"
}
case "1"
in
[a-z]) : ;;
[0-9]) : ;;
[A-Z]) : ;;
?) more ;;
*) :;;
esac
if [ "#" -eq 0 ]
then
echo -n "rm: missing operand
Try ‘rm –help’ for more informaction.
"
fi
if [ "#" -eq 1 ]
then
case "from1"
in
-i) echo "Try ‘rm –help’ for more informaction."; break ;;
-f) echo "Try ‘rm –help’ for more informaction."; break ;;
-r) echo "Try ‘rm –help’ for more informaction."; break ;;
-l) rml ;;
–help) help;;
*) rm1;;
esac
fi
if [ "#" -eq 2 ]
then
case "from1"
in
-i) rmi ;;
-f) rmf ;;
-r) rmr ;;
-l) rml ;;
-rf) rmr ;;
–help) help ;;
esac
fi
if [ "#" -gt 2 ]
then
for file in *
do
mvfile "$home/"
done 2> /dev/null
fi
[/code]
本文链接:http://www.yunweipai.com/1579.html
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/53180.html