12、删库跑路之命令rm的安全实现
范例-删库跑路之命令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