by phaemon · 9y ago · 0 comments
set -eu; set -o pipefail
The key missing one in this case was `-u`. That stops the script if you have an unset variable.This script would do some stuff, and put a new website in place, and then remove the old one. So, my bash script had the line:
rm -rf /var/www/$olddir
You can see it already. I ran it with $olddir unset. I think I had it in my head that the directory would simply not be found so that was fine. For those of you unfamiliar with bash, since olddir="", what actually ran was: rm -rf /var/www/
Gigabytes lost (back then, a GB was a lot!). We had backups but they took hours to restore. Horrible, horrible day. rm -rf /var/www/${olddir:?}
Which causes a specific error if olddir is unset. A good thing to do regardless of if the Brexit options are set!I'd written a script to clean out /tmp (since it wasn't a virtual fs back then) at boot. Problem is that it hadn't successfully changed to /tmp but was running instead in /etc
Goodbye /etc, it was nice knowing you... first I knew about it was when my box spectacularly failed to boot.
However, this was _the_ best learning experience of my life. No internet (since that was my only computer at the time) gradually rebuilding /etc by hand from a root prompt.
sudo rm -rf /home/username/something/$SOME_VAR/*
by some circumstances, SOME_VAR ended up being set to a space. Turns our that rm takes a list of directories to delete, so that deleted everything from the entire server.Fortunately I had backups. But yeah.. don't do this.
It makes you think - if we had these threads more often, perhaps we'd all get to learn more about these little process changes that could avert a disaster.