Database accidentally deleted with a bash script
Easy enough. The //
sequence isn't a comment in bash (#
is).
The statement OUT_DIR=x // text
had no effect* except a cryptic error message.
Thus, with the OUT_DIR being an empty string, one of the commands eventually executed was rm -rf /*
. Some directories placed directly underneath /
weren't removed due to user not having permissions, but it appears that some vital directories were removed. You need to restore from backup.
* The peculiar form of bash statement A=b c d e f
is roughly similar to:
export A=b
c d e f
unset A
A common example:
export VISUAL=vi # A standard visual editor to use is `vi`
visudo -f dummy_sudoers1 # Starts vi to edit a fake sudo config. Type :q! to exit
VISUAL=nano visudo -f dummy_sudoers2 # Starts nano to edit a fake sudo config
visudo -f dummy_sudoers3 # Starts vi again (!)
And the problematic line of script amounted to this:
export OUT_DIR=/data/backup/mongod/tmp
// 备份文件临时目录 # shell error as `//` isn't an executable file!
unset OUT_DIR
1) He erroneously assumed that //
was a bash comment. It is not, only #
is.
The shell interpreted // text
as a normal command, and did not find a binary called //
, and did nothing.
In bash, when you have a variable assignment (OUT_DIR=/data/backup/mongod/tmp
) directly preceding a command (// text
), it only sets the variable while running the command. Therefore, it unsets OUT_DIR
immediately, and when the rm line is reached, OUT_DIR
is now unset, and rm -rf /
is now called, deleting everything you have permission to delete.
2) The solution is the same as all rm -rf /
cases: restore from backup. There is no other solution because you do not have physical access to the hard drive.
1) Bash comments start with #. Sorry for your loss. 2) Restore from backup is the only way to proceed here, unfortunately.