首页 > 代码库 > 可恢复的安全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‘ 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 today=`date +"%Y%m%d"` mvpath=${RM_BACKUP_PATH}/$today # support for multi version 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 fpath=$1 fchar=`echo "${fpath:0:1}"` if [ "$fchar" = "/" ]; then dist_path="_${fpath}" else abs_fpath=`pwd`/$fpath dist_path="${fpath}@${timestamp}@${abs_fpath}" fi # substitue ‘/‘ to ‘_‘ final_dist_path=${dist_path//\//_} # mv to temp trash mv $fpath $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() { cd $RM_BACKUP_PATH # process multi files until [ $# -eq 0 ] do echo "revert for $1:" for f in `find . -name "$1@*" -print` do d=`echo $f | awk -F\/ ‘{print $2}‘` t=`echo $f | awk -F@ ‘{print $2}‘` fpath=`echo $f | awk -F@ ‘{print $3}‘` fpath=${fpath//_/\/} echo -n " $fpath 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 $fpath 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个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。