Recoverable Security rm

Source: Internet
Author: User

Recoverable Security rm

We often use rm to delete some files. If you don't shake your hands, it will be a tragedy. You all know...

After such a tragedy, I decided to never stop it. I overwrote the shell function and executed the secure rm. This function backs up the files to be deleted to the specified directory by date. At the same time, there will be multiple versions based on the deletion time. At the same time, another function is provided to restore the previously deleted files.

# 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_aaRM_BACKUP_PATH=/Users/louzhenlin/.TrashHistoryfunction 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}
The above is the safe_rm function, which is displayed in the backup directory:
?  ~  ll ~/.TrashHistory/20141021total 32drwxr-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 removedfunction 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 will list multiple versions of files to be restored.
?  ~  revert_rm appendonly.aofrevert 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
You can create an alias command to direct rm to safe_rm. Hope everyone can have a good time.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.