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

推荐订阅源

D
Darknet – Hacking Tools, Hacker News & Cyber Security
AI
AI
S
Security Affairs
S
Secure Thoughts
Attack and Defense Labs
Attack and Defense Labs
Application and Cybersecurity Blog
Application and Cybersecurity Blog
H
Heimdal Security Blog
Forbes - Security
Forbes - Security
Security Archives - TechRepublic
Security Archives - TechRepublic
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Spread Privacy
Spread Privacy
C
Cybersecurity and Infrastructure Security Agency CISA
Latest news
Latest news
T
Tor Project blog
I
Intezer
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Project Zero
Project Zero
V2EX - 技术
V2EX - 技术
V
Vulnerabilities – Threatpost
L
Lohrmann on Cybersecurity
C
CXSECURITY Database RSS Feed - CXSecurity.com
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Hacker News: Ask HN
Hacker News: Ask HN
S
Schneier on Security
T
Threat Research - Cisco Blogs
Webroot Blog
Webroot Blog
T
Threatpost
Help Net Security
Help Net Security
W
WeLiveSecurity
Know Your Adversary
Know Your Adversary
WordPress大学
WordPress大学
L
LINUX DO - 最新话题
月光博客
月光博客
PCI Perspectives
PCI Perspectives
小众软件
小众软件
爱范儿
爱范儿
博客园 - Franky
Security Latest
Security Latest
Microsoft Azure Blog
Microsoft Azure Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Stack Overflow Blog
Stack Overflow Blog
Google DeepMind News
Google DeepMind News
Scott Helme
Scott Helme
P
Privacy International News Feed
Y
Y Combinator Blog
P
Palo Alto Networks Blog
M
MIT News - Artificial intelligence
N
Netflix TechBlog - Medium

博客园 - 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呢?因为计划任务是非交互非登陆的,环境变量不生效