I accidently ran rm -rf /
I was writing a bash script to automate deleting something. I used the code rm -rf $path/*
accidently i ran the script without passing path variable and since path variable is null , the command which executed was rm -rf /* as sudo, now everything is removed on my server which was hosting more then 150 websites, i have backup of 1 week old . Linux is so big, it must have some workaround for this situation.
Solution 1:
There are no shortcuts with this. Linux is so powerful because it lets you do anything, including stupid things like removing everything.
You are lucky as you have backups. The next task is to install a fresh server and recover the data from your backups. While it would be possible to try and recover some data using data recovery software, the data on the backups has way better integrity. However, if you might need some data from the old installation anyway, you should not reuse the same disk, but buy a new one.
This will affect your business, so wise communication with your customers is crucial. Do not try and cover up anything. Be honest that there will be some downtime due to human error, and tell that they should not worry, because you had backups. Naturally it would have been better if they were from the last night, instead. After the incident you can revise your backup plans.
Also, when writing scripts, always validate the variables before passing them to rm -rf
. There could also have been other errors with your $path
causing the same outcome, e.g. rm -rf /some/directory /*
, if the variable had a tailing space.
Solution 2:
If you didn't touch anything much further there's a good chance to recover a lot of data with testdisc/photorec, but this isn't funny. Be sure not to mess around on the hardrive you deleted the files on, cause any time you write something to the disc your old data gets overwritten. The rm command usually just deletes the info of where a file is located on the drive, like erasing the toc of a book an leaving the pages untouched. Therefore your formerly files are now free space for the system.
So mount it seperately for recovery and don't write on it. You can recover a lot of files by photorec/testdisc. Don't be confused by the name photorec, it actually can recover a whole bunch of filetypes and not only photos. But in many cases only the data gets recovered and the filename is lost. So you have to be creative.
You can see a some examples here.