Skip to content

删库跑路之命令RmToMv的安全实现

范例-删库跑路之命令rm的安全实现-20210310

没打大的用途。。。

v1(适用于删除单个文件)

bash
#创建脚本[root@localhost scripts]# vim RmToMv.sh#!/bin/bashWARNNING_COLOR="echo -e \E[1;31m"END="\E[0m"DIR=/tmp/`date +%F_%H-%M-%S`mkdir$DIRmv$1$DIR$WARNNING_COLOR mv $1to $DIR has be findshed!$END

测试脚本过程如下:

bash
[root@localhost scripts]# aliasrm=/data/scripts/RmToMv.sh[root@localhost scripts]# touch test.txt[root@localhost scripts]# rm test.txt mvtest.txtto/tmp/2021-03-10_15-29-43hasbefindshed!#输出成功。[root@localhost scripts]# tree /tmp//tmp/└──2021-03-10_15-29-43└──test.txt1directory,1file[root@localhost scripts]# touch test1[root@localhost scripts]# rm -rf test1 #这里如果加上参数-rf的话,,就会出问题mv:invalidoption--'r'Try'mv --help'formoreinformation.mv-rfto/tmp/2021-03-10_15-30-35hasbefindshed![root@localhost scripts]# ll test1 -rw-r--r--.1rootroot0Mar1015:30test1[root@localhost scripts]# alias |greprm#文里别名就是没带参数-rf的aliasrm='/data/scripts/RmToMv.sh'[root@localhost scripts]# touch a b c d[root@localhost scripts]# rm a b c d #接多个参数会报错,只会移动第一个文件mvato/tmp/2021-03-10_15-33-01hasbefindshed![root@localhost scripts]# tree /tmp//tmp/├──2021-03-10_15-29-43│  └──test.txt├──2021-03-10_15-30-35└──2021-03-10_15-33-01└──a3directories,2files

v2(适用于删除多个文件)

bash
#创建脚本[root@localhost scripts]# vim RmToMv.shWARNNING_COLOR="echo -e \E[1;31m"END="\E[0m"DIR=/tmp/`date +%F_%H-%M-%S`mkdir$DIRmv$*$DIR #匹配多个参数$WARNNING_COLOR mv $*to $DIR has be findshed!$END

测试脚本过程如下:

bash
[root@localhost scripts]# touch a b c d[root@localhost scripts]# rm a b c dmvabcdto/tmp/2021-03-10_15-35-18hasbefindshed!#可成功删除多个文件。[root@localhost scripts]# tree /tmp//tmp/└──2021-03-10_15-35-18├──a├──b├──c└──d1directory,4files[root@localhost scripts]#

v3(进一步优化)

🚩 解决问题:使次脚本可以同时支持-rf参数(这个经测试,无法实现。。);且永久生效(这个可以实现);

bash
#测试过程如下:#默认别名如下:[root@localhost ~]# alias aliascp='cp -i'aliasegrep='egrep --color=auto'aliasfgrep='fgrep --color=auto'aliasgrep='grep --color=auto'aliasl.='ls -d .*--color=auto'aliasll='ls -l --color=auto'aliasls='ls --color=auto'aliasmv='mv -i'aliasrm='rm -i'#aliaswhich='alias |/usr/bin/which --tty-only --read-alias --show-dot --show-tilde'[root@localhost ~]# #通过alias临时设置的别名,只在当前shell进程有效,因此这里需要将这个别名写在配置文件中,使其永久生效。[root@localhost scripts]# aliasrm=/data/scripts/RmToMv.sh[root@localhost scripts]# alias aliascp='cp -i'aliasegrep='egrep --color=auto'aliasfgrep='fgrep --color=auto'aliasgrep='grep --color=auto'aliasl.='ls -d .*--color=auto'aliasll='ls -l --color=auto'aliasls='ls --color=auto'aliasmv='mv -i'aliasrm='/data/scripts/RmToMv.sh'aliaswhich='alias |/usr/bin/which --tty-only --read-alias --show-dot --show-tilde'[root@localhost scripts]# #注意[root@localhost scripts]# cat ~/.bashrc |grepalias# User specific aliases and functionsaliasrm='rm -i'aliascp='cp -i'aliasmv='mv -i'[root@localhost scripts]# echo "alias rm='/data/scripts/RmToMv.sh'">>~/.bashrc[root@localhost scripts]# echo "alias rm -rf ='/data/scripts/RmToMv.sh'">>~/.bashrc[root@localhost scripts]# cat ~/.bashrc |grepalias# User specific aliases and functionsaliasrm='rm -i'aliascp='cp -i'aliasmv='mv -i'aliasrm='/data/scripts/RmToMv.sh'aliasrm-rf='/data/scripts/RmToMv.sh'[root@localhost scripts]# #再次测试:Lastlogin:WedMar1015:28:042021from172.20.0.1aliasrm='/data/scripts/RmToMv.sh'-bash:alias:-rf:notfound#这里登录报错,因此加-rf参数写在配置文件中还是有问题的-bash:alias:=/data/scripts/RmToMv.sh:notfound#因此需要先替换掉原来的rm别名,然后加上自定义rm别名;且-rf参数无法实现次功能;[root@localhost ~]# cat .bashrc |grepalias# User specific aliases and functionsaliasrm='rm -i'aliascp='cp -i'aliasmv='mv -i'[root@localhost ~]# sed -i s"/alias rm/#alias rm/g"~/.bashrc [root@localhost ~]# cat .bashrc |grepalias# User specific aliases and functions#alias rm='rm -i'aliascp='cp -i'aliasmv='mv -i'[root@localhost ~]# echo "alias rm='/data/scripts/RmToMv.sh'">>~/.bashrc[root@localhost ~]# cat .bashrc |grepalias# User specific aliases and functions#alias rm='rm -i'aliascp='cp -i'aliasmv='mv -i'aliasrm='/data/scripts/RmToMv.sh'[root@localhost ~]# .~/.bashrc#退出终端并再次进入,再次测试:测试成功![root@localhost scripts]# touch a b c [root@localhost scripts]# rm a b cmvabcto/tmp/2021-03-10_15-58-27hasbefindshed![root@localhost scripts]# tree /tmp//tmp/└──2021-03-10_15-58-27├──a├──b└──c1directory,3files[root@localhost scripts]#

最终输出脚本如下:

bash
sed-is"/alias rm/#alias rm/g"~/.bashrcecho"alias rm='/data/scripts/RmToMv.sh'">>~/.bashrc.~/.bashrc

🚩解决问题:优化提示语、存放垃圾目录、脚本位置。

bash
#源提示内容如下:[root@localhost scripts]# cat RmToMv.sh #!/bin/bashWARNNING_COLOR="echo -e \E[1;31m"END="\E[0m"DIR=/tmp/`date +%F_%H-%M-%S`mkdir$DIRmv$*$DIR$WARNNING_COLOR mv $*to $DIR has be findshed!$END #修改后内容如下:[root@localhost scripts]# cat RmToMv.sh #!/bin/bashWARNNING_COLOR="echo -e \E[1;31m"END="\E[0m"DIR=/tmp/RmToMv-garbage/`date+%F_%H-%M-%S`
mkdir -p $DIR
mv $*  $DIR
$WARNNING_COLOR Mv $* to $DIR has be findshed! $END

测试结果成功:

bash
[root@localhost scripts]# cat ~/.bashrc |grep alias
# User specific aliases and functions
#alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias rm='/root/scripts/RmToMv.sh'
[root@localhost scripts]# cat /root/scripts/RmToMv.sh
#!/bin/bash
WARNNING_COLOR="echo -e \E[1;31m"
END="\E[0m"
DIR=/tmp/RmToMv-garbage/`date+%F_%H-%M-%S`
mkdir -p $DIR
mv $*  $DIR
$WARNNING_COLOR Mv $* to $DIR has be findshed! $END 

[root@localhost scripts]#

v4 最终正式版脚本输出(success 2021.3.11)

最终代码如下

bash
mkdir -p /root/scripts
cat > /root/scripts/RmToMv.sh <<EOF
#!/bin/bash
WARNNING_COLOR="echo -e \E[1;31m"
END="\E[0m"
DIR=/root/RmToMv-garbage/\`date +%F_%H-%M-%S\`
mkdir -p \$DIR
mv \$*  \$DIR
\$WARNNING_COLOR Mv \$* to \$DIR has be findshed! \$END 
EOF


chmod +x /root/scripts/RmToMv.sh
sed -i s"/alias rm/#alias rm/g" ~/.bashrc 
echo "alias rm='/root/scripts/RmToMv.sh'" >> ~/.bashrc
. ~/.bashrc

脚本位置:

https://onedayxyy.cn/scripts/rm_to_mv/RmToMv.sh

RmToMv.sh

bash
[root@docusaurus-wiki rm_to_mv]#cat RmToMv.sh 
#!/bin/bash
#
#***************************************************************
#Author:                hg
#QQ:                    xxx
#Date:                  2021-03-11
#FileName:              RmToMv.sh
#URL:                   http://www.wangxiaochun.com
#Description:           The test script
#Copyright (c) :       2021 All rights reserved
#***************************************************************
WARNNING_COLOR="echo -e \E[1;31m"
END="\E[0m"
DIR=/root/RmToMv-garbage/`date+%F_%H-%M-%S`
mkdir -p $DIR
mv $*  $DIR
$WARNNING_COLOR Mv $* to $DIR has be findshed! $END 
[root@docusaurus-wiki rm_to_mv]#