0

I have a CentOS Linux VM running in VirtualBox. I wrote a bash script to do some automated tasks. But I accidentally deleted all root folders because I put a line in the script

rm -r $my_dir/*

However, $my_dir is not found so the above will execute as rm -r /*. So now all my root folder contents are gone! When I restart my VM, it goes to grub terminal and can't launch the GNOME and bash shell because everything under / was accidentally deleted due to the above fault (lesson learned). So is there anyway to safely recover and restore the system either from in the VirtualBox or outside?

  • I assume you didn't make a clone of the virtual machine or branched it or any similar behavior? Due to the nature of how virtual machines work file recover isn't possible. – Ramhound Jun 29 '15 at 16:44
  • if you don't have backups, there is likely nothing that can be done. you can try booting off a live CD and attempting an undelete with TestDisk, but as Ramhound said, recovering data from a virtual machine harddisk file is not the same as recovering it from a physical filesystem. linux.org/threads/undelete-files-on-linux-systems.4316 – Frank Thomas Jun 29 '15 at 16:46
  • @FrankThomas Thanks for your suggestions. I have all my data backed up somewhere else. So I just want to restore the system. If I boot off from a live CD, it will reinstall Linux instead of restoring to my original system, right? – tonga Jun 29 '15 at 17:04
  • You can also boot a data recovery CD within virtual box to try to recover the data: runtime.org/data-recovery-live-cd.htm – Jeroen Jun 29 '15 at 17:11
  • BTW, is this considered a bash scripting bug? If I run rm -r $my_dir/* but $my_dir doesn't exist, then bash script will run as rm -r /*? It is really really unexpected. – tonga Jun 29 '15 at 17:26
0

You could try to recover the files using a bootable CD from https://www.runtime.org/data-recovery-live-cd.htm

Boot the CD within Virtualbox and follow the instructions on the website.

Extra:

In all languages it is recommended to always perform input validation. This is not different when using bash scripting. I have seen servers been compromised because of the lack of input validation.

man test

This will explain how to perform some validation to determine if a string is null or empty:

   -n STRING
          the length of STRING is nonzero

   STRING equivalent to -n STRING

   -z STRING
          the length of STRING is zero

So what you should do is:

if [ ! -z $my_dir ]; then

   rm -r $my_dir/* 

fi

NOTE: If the variable is not properly sanitized like in the example above, and the input is coming from an argument, it is possible to break out and run other commands:

Example:

$ ./mybashscript.sh [directory]

By giving the following argument for [directory]: test; rm -rf /

The script will delete a folder called "test" (if it exists) and then the root directory (/).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.