我们经常使用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即可。希望大家用的开心哈。