惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
雷峰网
雷峰网
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Spread Privacy
Spread Privacy
H
Hacker News: Front Page
PCI Perspectives
PCI Perspectives
Webroot Blog
Webroot Blog
罗磊的独立博客
H
Heimdal Security Blog
TaoSecurity Blog
TaoSecurity Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
大猫的无限游戏
大猫的无限游戏
月光博客
月光博客
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Google Online Security Blog
Google Online Security Blog
Last Week in AI
Last Week in AI
美团技术团队
Help Net Security
Help Net Security
The Hacker News
The Hacker News
C
Cisco Blogs
T
The Blog of Author Tim Ferriss
J
Java Code Geeks
The Register - Security
The Register - Security
IT之家
IT之家
WordPress大学
WordPress大学
Jina AI
Jina AI
Recent Commits to openclaw:main
Recent Commits to openclaw:main
H
Help Net Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Threat Research - Cisco Blogs
P
Proofpoint News Feed
NISL@THU
NISL@THU
爱范儿
爱范儿
The GitHub Blog
The GitHub Blog
Scott Helme
Scott Helme
V
Vulnerabilities – Threatpost
B
Blog
T
Tenable Blog
博客园 - 三生石上(FineUI控件)
T
The Exploit Database - CXSecurity.com
S
Security Affairs
小众软件
小众软件
Hacker News: Ask HN
Hacker News: Ask HN
Security Latest
Security Latest
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
W
WeLiveSecurity
A
Arctic Wolf
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
M
MIT News - Artificial intelligence

博客园 - centosboy

Thinkpad T490卡顿排查 xinference初探 diff的安装与使用 ubuntu docker运行大模型 vllm部署 zabbix监控mongodb /root/.dbshell无权限解决办法 开机启动后应用limit没有生效 关于运维工作的一点感悟 将CDLINUX装入U盘 Autossh打洞 hive分区表实践 hashlib模块 linux 通过inode删除文件 那些年踩过的坑 htpdate代替ntpdate同步时间 Openfire搭建聊天系统 zookeeper运维 centos ping添加丢包记录 Linux tee命令输出显示在屏幕和保存到日志文件中
rm 防误删脚本
centosboy · 2020-12-15 · via 博客园 - centosboy

前记:
脚本称为《rm后悔药》吧,虽然多年运维,依旧无法避免误操作,既然无法避免,就做好误删准备。脚本参考之前转载的文章,进行了简单的完善。
脚本内容:

#创建回收站
mkdir -p /opt/apps/.recover/
chmod 777 /opt/apps/.recover
mkdir -p /opt/apps/shell/
#创建回收脚本
cat >/opt/apps/shell/remove.sh <<EOF
#!/bin/bash
RECOVER_DIR="/opt/apps/.recover/\$USER"
if [[ ! -d "/opt/apps/.recover/\$USER" ]]
then
    mkdir "/opt/apps/.recover/\$USER"
fi
for i in \$*; do
    STAMP=\`date +%s\`
    fileName=\`basename \$i\`
    mv \$i \$RECOVER_DIR/\$fileName.\$STAMP
done
EOF

chmod +x /opt/apps/shell/remove.sh

#增加环境变量
cat >>/etc/bashrc <<EOF
alias rm='sh /opt/apps/shell/remove.sh'
EOF

#设置rm别名,用于删除back文件
ln -s /usr/bin/rm /usr/bin/rm-rm

#设置凌晨清空文件
cat >>/etc/crontab <<EOF
0 3 * * * root rm -rf /opt/apps/.recover/*
EOF
#生效环境变量
source /etc/bashrc

简单说明下:
1.将要删除的文件mv至回收站,凌晨3点清空。
2.为什么要加入/etc/bashrc呢?因为用户交互式登陆系统或者交互式非登陆系统,/etc/bashrc都会执行,这样,系统所有用户都可以共享此功能。
3.将rm软连接rm-rm,有临时需要永久删除的文件,可以使用此命令立即生效
4.为什么计划任务可以使用rm呢?因为计划任务是非交互非登陆的,环境变量不生效