I just deleted "/bin". What's the best way to recover?

I just ran (not on purpose!) rm -rf /bin.

I've booted down the computer and am using Finnix to try to recover from it. I have succeeded in mounting the drive, and confirmed that, yes, the entire /bin folder is deleted.

Is it possible to recover from this without reinstalling the OS?

I'm thinking that I could setup a VM with the same OS and architecture (Ubuntu Server 11.10 alpha release, x86) and install all the packages I had installed on the server, then just copy the /bin folder.

Will this work? Am I better off just starting over?


Solution 1:

In general, I'd lean towards a reinstall (from the backups you are absolutely supposed to have). But I'm feeling hackish, so here's another way (assuming that your system is mounted under /target):

  1. Get a list of all the installed packages that have files in /bin:

    grep ^/bin/ /target/var/lib/dpkg/info/*.list | sed 's%^.*/\([^/\.]*\).list%\1%' >/tmp/pkglist
    

    (On my Debian Squeeze system that's a total of 34 packages, pretty much all of which are core)

  2. Download each of those packages (I couldn't be bothered to script this bit, so just hit up packages.ubuntu.com and download them into somewhere under /target). If you're lucky some of the packages might still be floating around in /target/var/cache/apt/archives.

  3. For each package, run /target/usr/bin/dpkg -x <package> /target. I'm fairly certain dpkg is all self-contained these days, and shouldn't call out to anything in /bin itself.

Once you've got the system back on it's feet, you should make sure and run an apt-get --reinstall install <all the packages from step 1>, because (since you're running a pre-release, which Sysadmin Cat says is a no-no on production systems) the versions of the packages you just extracted are likely to be different from those that were on the system before, and you'll want to make sure the system knows exactly what's what.

Solution 2:

While your strategy may work (there are good chances for it, more if once you copy back the bin directory, you do an apt-get reinstall of all the packages in you system), it can yield problems in the future because you may get an unstable server.

If that would have happened to me, I would reinstall and restore from the daily backup. You have a daily backup - a disaster recovery plan - don't you?

In case you didn't have a disaster recovery plan, I would suggest to backup all the config files and the data you want to preserve and go ahead with a reinstall. That way you'll be sure that you'll end with a stable server again.

Solution 3:

I would try creating a VM then copying the contents of /bin to your damaged machine. That should get you up and running. Then run

dpkg --get-selections  | awk '{print $1}' | xargs -l1 aptitude reinstall

which should reinstall all the packages that were on your system.