0

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.

  • 1
    Does this answer your question? [Monday morning mistake: sudo rm -rf --no-preserve-root /](https://serverfault.com/questions/587102/monday-morning-mistake-sudo-rm-rf-no-preserve-root) – Bob Dec 06 '20 at 05:44
  • happy Christmas and i am sorry hopefully you have backups, usually you never need -f on most cases – djdomi Dec 06 '20 at 08:14

2 Answers2

3

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.

Esa Jokinen
  • 43,252
  • 2
  • 75
  • 122
  • 3
    Any script that invokes `rm` with a variable should really `set -u`. It won't safeguard against set but empty variable but will already catch the most common case of unset variable and/or typos. – Ginnungagap Dec 06 '20 at 10:56
  • Thanks @Ginnungagap for your advice, i restored my server from backup, will take care of this thing said my you next time writing bash scripts. But can you elaborate what 'set -u' and 'set -i' do in bash script, what will they do when a variable is empty, do they throw any error or something. – Keshav Bhamaan Dec 09 '20 at 07:21
  • `-u` will `Treat unset variables and parameters other than the special parameters ‘@’ or ‘*’ as an error when performing parameter expansion. An error message will be written to the standard error, and a non-interactive shell will exit.` I don't know what `-i` is. – Ginnungagap Dec 09 '20 at 07:24
1

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.

Netnoob
  • 11
  • 1
  • 1
    That's useful for recovering individual files on a personal computer, but not suitable for servers with multiple websites. The structure is as important as the contents. Therefore, restoring from backups is the only real solution to this. File recovery tools might be useful only if something crucial but limited is missing from the latest backup. – Esa Jokinen Dec 06 '20 at 15:26
  • Thanks for your advice, but i restored my backup which was some days old – Keshav Bhamaan Dec 09 '20 at 07:19