How to recover the Linux VM after accidentally deleting all root folders?

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?

tonga

Posted 2015-06-29T16:34:12.597

Reputation: 163

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 – 2015-06-29T16:44:28.150

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. http://www.linux.org/threads/undelete-files-on-linux-systems.4316/

– Frank Thomas – 2015-06-29T16:46:05.070

@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 – 2015-06-29T17:04:15.023

You can also boot a data recovery CD within virtual box to try to recover the data: https://www.runtime.org/data-recovery-live-cd.htm

– Jeroen – 2015-06-29T17:11:07.863

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 – 2015-06-29T17:26:56.377

Well if the variable $my_dir is not checked if it contains data, it is empty which results in rm -r /* – Jeroen – 2015-06-29T17:34:57.420

@Jeroen-ITNerdbox I think you are right, but still bash script should have a way to differentiate between the cases when $my_dir is NULL (non-existed) or empty string '' since they are two different things. – tonga – 2015-06-29T17:40:09.317

Try reading the manual page for test by typing: man test - that should clear things up (I hope) - In addition, it would be better if the topic started would have pasted their code. – Jeroen – 2015-06-29T17:43:02.403

If you boot from a live CD "in the virtual machine" it should give you access to the live cd, not necessarily re-install linux. If you want to re-install, boot the virtual machine off of the Linux Distro Install CD and install it as you normally would. – ssnobody – 2015-06-29T22:16:52.590

Answers

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 (/).

Jeroen

Posted 2015-06-29T16:34:12.597

Reputation: 236