首页 > 代码库 > 可恢复的安全rm
可恢复的安全rm
我们常常使用rm去删除一些文件。假设不小手一抖,那么就悲剧了。你们都懂的。。。
在经历过一次这种慘剧后。决定永远杜绝这种情况。重写写了shell函数。运行安全的rm。这个函数会把要删除的文件按日期备份到指定的文件夹,同一时候依据删除时间的不同会有多个版本号,同一时候提供了另外一个函数用于恢复之前删除的文件。
# safe rm # Don‘t remove the file, just move them to a temporary directory. # Files are grouped by remove time. # e.g. # # pwd => /home/work/ # > rm -r -f aa # ‘aa‘ will move to ~/.TrashHistory/20141018/aa@120111@_home_work_aa _RM_BACKUP_PATH=/Users/louzhenlin/.TrashHistory function safe_rm() { # skip cmd option, e.g. ‘-rf‘ in ‘rm -rf a b‘ or ‘-r/-f‘ in ‘rm -r -f a b‘ local first_char=${1:0:1} until [ ! "$first_char" = "-" ] do shift first_char=${1:0:1} done # check param if [ $# -lt 1 ]; then echo ‘usage: rm [-f | -i] [-dPRrvW] file ...‘ exit 1 fi local today=`date +"%Y%m%d"` local mvpath=${_RM_BACKUP_PATH}/$today # support for multi version local timestamp=`date +"%H%M%S"` # create dir if path non-exist if [ ! -d $mvpath ]; then mkdir $mvpath fi until [ $# -eq 0 ] do # fetch absolute path of the file local file_path=$1 local fchar=`echo "${file_path:0:1}"` if [ "$fchar" = "/" ]; then local dist_path="_${file_path}" else local abs_fpath=`pwd`/$file_path local dist_path="${file_path}@${timestamp}@${abs_fpath}" fi # substitue ‘/‘ to ‘_‘ local final_dist_path=${dist_path//\//^} # mv to temp trash mv $file_path $mvpath/$final_dist_path # next file shift done }
上面是safe_rm函数,在备份文件夹下显示:
? ~ ll ~/.TrashHistory/20141021 total 32 drwxr-xr-x 7 louzhenlin staff 238 10 21 18:01 . drwxr-xr-x 5 louzhenlin staff 170 10 21 15:51 .. -rw-r--r-- 1 louzhenlin staff 136 10 20 23:39 a@180117@^Users^louzhenlin^dev^workspace^c_cpp^leveldb^a -rw-r--r-- 1 louzhenlin staff 399 10 14 17:43 aof.log@164609@^Users^louzhenlin^dev^workspace^python^redis^aof^modifer^aof.log -rw-r--r-- 1 louzhenlin staff 0 10 14 11:19 appendonly-1.aof@155727@^Users^louzhenlin^dev^server^redis-2.8.17^appendonly-1.aof -rw-r--r-- 1 louzhenlin staff 399 10 14 17:42 appendonly.aof@155105@^Users^louzhenlin^dev^server^redis-2.8.17^appendonly.aof -rw-r--r-- 1 louzhenlin staff 565 10 21 15:56 appendonly.aof@161315@^Users^louzhenlin^dev^server^redis-2.8.17^appendonly.aof
能够看到,文件里包括时间以及全路径信息,以便用于恢复。
以下是用于恢复的函数:
# revert files that remove by safe_rm # you can choose the right one in multi files removed function revert_rm() { # process multi files until [ $# -eq 0 ] do echo "revert for $1:" for _f in `find $_RM_BACKUP_PATH -name "$1@*" -print` do local d=`echo $_f | awk -F\/ ‘{print $2}‘` local t=`echo $_f | awk -F@ ‘{print $2}‘` local file_path=`echo $_f | awk -F@ ‘{print $3}‘` file_path=${file_path//^/\/} echo -n " $file_path at ${d:0:4}-${d:4:2}-${d:6:2} ${t:0:2}:${t:2:2}:${t:4:2} [y/n]? " read _confirm if [ "${_confirm}" = ‘y‘ ]; then mv $_f $file_path break fi done shift done }
revert_rm会将多个版本号的文件列出来。用于选择想要恢复的那个。
? ~ revert_rm appendonly.aof revert for appendonly.aof: /Users/louzhenlin/dev/server/redis-2.8.17/appendonly.aof at 2014-10-21 15:51:05 [y/n]? n /Users/louzhenlin/dev/server/redis-2.8.17/appendonly.aof at 2014-10-21 16:13:15 [y/n]? y使用时。能够建立一个alias将rm指向safe_rm就可以。希望大家用的开心哈。
可恢复的安全rm
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。