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 – 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.863BTW, 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 asrm -r /*
? It is really really unexpected. – tonga – 2015-06-29T17:26:56.377Well 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.317Try 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