跳到主要内容

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

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

没打大的用途。。。

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

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

测试脚本过程如下:

[root@localhost scripts]# alias rm=/data/scripts/RmToMv.sh

[root@localhost scripts]# touch test.txt
[root@localhost scripts]# rm test.txt
mv test.txt to /tmp/2021-03-10_15-29-43 has be findshed! #输出成功。
[root@localhost scripts]# tree /tmp/
/tmp/
└── 2021-03-10_15-29-43
└── test.txt

1 directory, 1 file

[root@localhost scripts]# touch test1
[root@localhost scripts]# rm -rf test1 #这里如果加上参数-rf的话,,就会出问题
mv: invalid option -- 'r'
Try 'mv --help' for more information.
mv -rf to /tmp/2021-03-10_15-30-35 has be findshed!

[root@localhost scripts]# ll test1
-rw-r--r--. 1 root root 0 Mar 10 15:30 test1
[root@localhost scripts]# alias |grep rm #文里别名就是没带参数-rf的
alias rm='/data/scripts/RmToMv.sh'

[root@localhost scripts]# touch a b c d
[root@localhost scripts]# rm a b c d #接多个参数会报错,只会移动第一个文件
mv a to /tmp/2021-03-10_15-33-01 has be findshed!
[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
└── a

3 directories, 2 files

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

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

测试脚本过程如下:

[root@localhost scripts]# touch a b c d
[root@localhost scripts]# rm a b c d
mv a b c d to /tmp/2021-03-10_15-35-18 has be findshed! #可成功删除多个文件。
[root@localhost scripts]# tree /tmp/
/tmp/
└── 2021-03-10_15-35-18
├── a
├── b
├── c
└── d

1 directory, 4 files
[root@localhost scripts]#

v3(进一步优化)

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

#测试过程如下:

#默认别名如下:
[root@localhost ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i' #
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@localhost ~]#

#通过alias临时设置的别名,只在当前shell进程有效,因此这里需要将这个别名写在配置文件中,使其永久生效。
[root@localhost scripts]# alias rm=/data/scripts/RmToMv.sh
[root@localhost scripts]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='/data/scripts/RmToMv.sh'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@localhost scripts]#

#注意
[root@localhost scripts]# cat ~/.bashrc |grep alias
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='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 |grep alias
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias rm='/data/scripts/RmToMv.sh'
alias rm -rf ='/data/scripts/RmToMv.sh'
[root@localhost scripts]#

#再次测试:
Last login: Wed Mar 10 15:28:04 2021 from 172.20.0.1
alias rm='/data/scripts/RmToMv.sh'
-bash: alias: -rf: not found #这里登录报错,因此加-rf参数写在配置文件中还是有问题的
-bash: alias: =/data/scripts/RmToMv.sh: not found

#因此需要先替换掉原来的rm别名,然后加上自定义rm别名;且-rf参数无法实现次功能;
[root@localhost ~]# cat .bashrc |grep alias
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
[root@localhost ~]# sed -i s"/alias rm/#alias rm/g" ~/.bashrc
[root@localhost ~]# cat .bashrc |grep alias
# User specific aliases and functions
#alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
[root@localhost ~]# echo "alias rm='/data/scripts/RmToMv.sh'" >> ~/.bashrc
[root@localhost ~]# cat .bashrc |grep alias
# User specific aliases and functions
#alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias rm='/data/scripts/RmToMv.sh'
[root@localhost ~]# . ~/.bashrc

#退出终端并再次进入,再次测试:测试成功!
[root@localhost scripts]# touch a b c
[root@localhost scripts]# rm a b c
mv a b c to /tmp/2021-03-10_15-58-27 has be findshed!
[root@localhost scripts]# tree /tmp/
/tmp/
└── 2021-03-10_15-58-27
├── a
├── b
└── c

1 directory, 3 files
[root@localhost scripts]#

最终输出脚本如下:

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

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

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

#修改后内容如下:
[root@localhost scripts]# cat 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]# 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)

最终代码如下

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

[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]#