Preface
Use the script to delete the files, and the script backs up the deleted files at the same time. If we delete it by mistake, we can go to the designated backup directory and choose to restore.
1. Script content
[root@hya shell]# vim rm.backup.sh
#!/bin/bash
NAME=$1
now=`date +%Y%m%d%H%M`
read -p "你确定要删除当前的文件或者文件夹吗 yes|no:" inpt
read -p "请在确定一次 是否删除文件或者文件夹 yes|no:" input
if [ $input == "yes" ] ||[ $input == "y" ];then
mkdir -p /data/.$now
#### 判断rsync是否已经安装
RSYNC=`rpm -qa rsync |wc -l`
if [ $RSYNC -ne 0 ];then
echo "现在开始备份数据"
else
#### 如果没有rsync就先安装rsync并启动
yum install xinetd rsync -y
useradd -s /sbin/nologin -M rsync
mkdir -p /data
chown -R rsync.rsync /data/
echo "rsync_backup:123456" >/etc/rsync.password
chmod 600 /etc/rsync.password
fi
rsync --daemon
#### 判断要删除的文件是否存在
if [ -f $1 ] || [ -d $1 ];then
### 如果要删除的文件存在则开始备份
rsync -aR $1 /data/.$now/$1/
echo "数据备份成功"
else
echo "文件不存在,请重新确认文件名字"
exit 1
fi
echo "开始删除数据"
/bin/rm -rf $1
echo "文件删除成功,若需要恢复请到 /data/.$now/路径下恢复"
elif [ $input == "no" ] ||[ $input == "n" ]
then
exit 0
else
echo "请输入yes|no"
exit
fi
2. Practical Camp
[root@hya shell]# chmod +x rm.backup.sh #授权 (给脚本执行的权限)
[root@hya ~]# cd /etc/
[root@hya etc]# sh /usr/local/shell/rm.backup.sh /etc/
你确定要删除当前的文件或者文件夹吗 yes|no:yes
请在确定一次 是否删除文件或者文件夹 yes|no:yes
现在开始备份数据
数据备份成功
开始删除数据
文件删除成功,若需要恢复请到 /data/.202006291515/路径下恢复
##查看是否删除
[root@hya etc]# ls -l
总用量 0
恢复
[root@hya etc]# mv /data/.202006291515/etc/* /etc/
[root@hya etc]# ls
etc
to sum up
This script can be used for daily file deletion by mistake, in general it is fine.