hugo-teek is loading...

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

最后更新于:

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

没打大的用途。。。

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

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

测试脚本过程如下:

 1[root@localhost scripts]# alias rm=/data/scripts/RmToMv.sh
 2
 3[root@localhost scripts]# touch test.txt
 4[root@localhost scripts]# rm test.txt 
 5 mv test.txt to /tmp/2021-03-10_15-29-43 has be findshed! #输出成功。
 6[root@localhost scripts]# tree /tmp/
 7/tmp/
 8└── 2021-03-10_15-29-43
 9    └── test.txt
10
111 directory, 1 file
12
13[root@localhost scripts]# touch test1
14[root@localhost scripts]# rm -rf test1 #这里如果加上参数-rf的话,,就会出问题
15mv: invalid option -- 'r'
16Try 'mv --help' for more information.
17 mv -rf to /tmp/2021-03-10_15-30-35 has be findshed!
18 
19[root@localhost scripts]# ll test1 
20-rw-r--r--. 1 root root 0 Mar 10 15:30 test1
21[root@localhost scripts]# alias |grep rm #文里别名就是没带参数-rf的
22alias rm='/data/scripts/RmToMv.sh'
23
24[root@localhost scripts]# touch a b c d
25[root@localhost scripts]# rm a b c d #接多个参数会报错,只会移动第一个文件
26 mv a to /tmp/2021-03-10_15-33-01 has be findshed! 
27[root@localhost scripts]# tree /tmp/
28/tmp/
29├── 2021-03-10_15-29-43
30│   └── test.txt
31├── 2021-03-10_15-30-35
32└── 2021-03-10_15-33-01
33    └── a
34
353 directories, 2 files

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

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

测试脚本过程如下:

 1[root@localhost scripts]# touch a b c d
 2[root@localhost scripts]# rm a b  c d
 3 mv a b c d to /tmp/2021-03-10_15-35-18 has be findshed! #可成功删除多个文件。
 4[root@localhost scripts]# tree /tmp/
 5/tmp/
 6└── 2021-03-10_15-35-18
 7    ├── a
 8    ├── b
 9    ├── c
10    └── d
11
121 directory, 4 files
13[root@localhost scripts]#

v3(进一步优化)

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

 1#测试过程如下:
 2
 3#默认别名如下:
 4[root@localhost ~]# alias 
 5alias cp='cp -i'
 6alias egrep='egrep --color=auto'
 7alias fgrep='fgrep --color=auto'
 8alias grep='grep --color=auto'
 9alias l.='ls -d .* --color=auto'
10alias ll='ls -l --color=auto'
11alias ls='ls --color=auto'
12alias mv='mv -i'
13alias rm='rm -i' #
14alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
15[root@localhost ~]# 
16
17#通过alias临时设置的别名,只在当前shell进程有效,因此这里需要将这个别名写在配置文件中,使其永久生效。
18[root@localhost scripts]# alias rm=/data/scripts/RmToMv.sh
19[root@localhost scripts]# alias 
20alias cp='cp -i'
21alias egrep='egrep --color=auto'
22alias fgrep='fgrep --color=auto'
23alias grep='grep --color=auto'
24alias l.='ls -d .* --color=auto'
25alias ll='ls -l --color=auto'
26alias ls='ls --color=auto'
27alias mv='mv -i'
28alias rm='/data/scripts/RmToMv.sh'
29alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
30[root@localhost scripts]# 
31
32#注意
33[root@localhost scripts]# cat ~/.bashrc |grep alias
34# User specific aliases and functions
35alias rm='rm -i'
36alias cp='cp -i'
37alias mv='mv -i'
38[root@localhost scripts]# echo "alias rm='/data/scripts/RmToMv.sh'" >> ~/.bashrc
39[root@localhost scripts]# echo "alias rm -rf ='/data/scripts/RmToMv.sh'" >> ~/.bashrc
40[root@localhost scripts]# cat ~/.bashrc |grep alias
41# User specific aliases and functions
42alias rm='rm -i'
43alias cp='cp -i'
44alias mv='mv -i'
45alias rm='/data/scripts/RmToMv.sh'
46alias rm -rf ='/data/scripts/RmToMv.sh'
47[root@localhost scripts]# 
48
49#再次测试:
50Last login: Wed Mar 10 15:28:04 2021 from 172.20.0.1
51alias rm='/data/scripts/RmToMv.sh'
52-bash: alias: -rf: not found #这里登录报错,因此加-rf参数写在配置文件中还是有问题的
53-bash: alias: =/data/scripts/RmToMv.sh: not found
54
55#因此需要先替换掉原来的rm别名,然后加上自定义rm别名;且-rf参数无法实现次功能;
56[root@localhost ~]# cat .bashrc |grep alias
57# User specific aliases and functions
58alias rm='rm -i'
59alias cp='cp -i'
60alias mv='mv -i'
61[root@localhost ~]# sed -i s"/alias rm/#alias rm/g" ~/.bashrc 
62[root@localhost ~]# cat .bashrc |grep alias
63# User specific aliases and functions
64#alias rm='rm -i'
65alias cp='cp -i'
66alias mv='mv -i'
67[root@localhost ~]# echo "alias rm='/data/scripts/RmToMv.sh'" >> ~/.bashrc
68[root@localhost ~]# cat .bashrc |grep alias
69# User specific aliases and functions
70#alias rm='rm -i'
71alias cp='cp -i'
72alias mv='mv -i'
73alias rm='/data/scripts/RmToMv.sh'
74[root@localhost ~]# . ~/.bashrc
75
76#退出终端并再次进入,再次测试:测试成功!
77[root@localhost scripts]# touch a b c 
78[root@localhost scripts]# rm a b c
79 mv a b c to /tmp/2021-03-10_15-58-27 has be findshed! 
80[root@localhost scripts]# tree /tmp/
81/tmp/
82└── 2021-03-10_15-58-27
83    ├── a
84    ├── b
85    └── c
86
871 directory, 3 files
88[root@localhost scripts]#

最终输出脚本如下:

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

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

 1#源提示内容如下:
 2[root@localhost scripts]# cat RmToMv.sh 
 3#!/bin/bash
 4WARNNING_COLOR="echo -e \E[1;31m"
 5END="\E[0m"
 6DIR=/tmp/`date +%F_%H-%M-%S`
 7mkdir $DIR
 8mv $*  $DIR
 9$WARNNING_COLOR mv $* to $DIR has be findshed! $END 
10
11#修改后内容如下:
12[root@localhost scripts]# cat RmToMv.sh 
13#!/bin/bash
14WARNNING_COLOR="echo -e \E[1;31m"
15END="\E[0m"
16DIR=/tmp/RmToMv-garbage/`date +%F_%H-%M-%S`
17mkdir -p $DIR
18mv $*  $DIR
19$WARNNING_COLOR Mv $* to $DIR has be findshed! $END 

测试结果成功:

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

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

最终代码如下

 1mkdir -p /root/scripts
 2cat > /root/scripts/RmToMv.sh <<EOF
 3#!/bin/bash
 4WARNNING_COLOR="echo -e \E[1;31m"
 5END="\E[0m"
 6DIR=/root/RmToMv-garbage/\`date +%F_%H-%M-%S\`
 7mkdir -p \$DIR
 8mv \$*  \$DIR
 9\$WARNNING_COLOR Mv \$* to \$DIR has be findshed! \$END 
10EOF
11
12
13chmod +x /root/scripts/RmToMv.sh
14sed -i s"/alias rm/#alias rm/g" ~/.bashrc 
15echo "alias rm='/root/scripts/RmToMv.sh'" >> ~/.bashrc
16. ~/.bashrc

脚本位置:

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

RmToMv.sh

 1[root@docusaurus-wiki rm_to_mv]#cat RmToMv.sh 
 2#!/bin/bash
 3#
 4#***************************************************************
 5#Author:                hg
 6#QQ:                    xxx
 7#Date:                  2021-03-11
 8#FileName:              RmToMv.sh
 9#URL:                   http://www.wangxiaochun.com
10#Description:           The test script
11#Copyright (c) :       2021 All rights reserved
12#***************************************************************
13WARNNING_COLOR="echo -e \E[1;31m"
14END="\E[0m"
15DIR=/root/RmToMv-garbage/`date +%F_%H-%M-%S`
16mkdir -p $DIR
17mv $*  $DIR
18$WARNNING_COLOR Mv $* to $DIR has be findshed! $END 
19[root@docusaurus-wiki rm_to_mv]#
推荐使用微信支付
微信支付二维码
推荐使用支付宝
支付宝二维码
最新文章

文档导航