Ubuntu – How to recover data from /var on a broken system


After installing KDE (and attempting to fix it by installing Gnome 3), I can no longer log in to my system as it presents me with a black screen.

This is a really bad place for me to be, because I have a bunch of data I need to recover from my installation (particularly, in /var/www).

Is there a way to pull this data off of my system using a Live CD before re-installing my OS?

Best Answer

If you're on live media, your data and installation should be just fine. You'll need a terminal for this, so open one from the Dash menu, or by pressing the trusty Ctrl+Alt+T shortcut.

  1. Get a spare USB and plug it in. It should mount to /media/ubuntu/<something>. Make note of the mount path.
  2. Type the command blkid and look for something with ext* in the name:
    Ex: /dev/sda4: UUID="..." TYPE="ext4" PARTUUID="..."
    Make note of the partition ID (/dev/sda4).
  3. Mount the partiton using sudo mount <id> /mnt, replacing <id> with whatever you got in step 3, in my case, /dev/sda4.
  4. Run cd /mnt/var.
  5. Run sudo cp -R www/ <mountpath_from_step_1>.

Finally, use Nautilus or ls to check that all of your files did actually get copied over successfully.

After it's all over and done, unmount both drives with this command:

sudo sync; sudo umount /mnt <mountpath_from_step_1>

Alternatively, you could have done with without live media. On the broken Ubuntu install, you could have gone to TTY4 (Ctrl+Alt+F4), inserted a USB drive, and done this:

  1. Run lsblk and find the USB drive's lettering (ex, /dev/sdb1).
  2. Mount the USB drive to /mnt with the command: sudo mount <step_1> /mnt
  3. Make a folder for your drive (sudo mkdir /mnt/web-backup)
  4. Copy everything over: sudo cp -R /var/www /mnt/web-backup
  5. Use ls to verify: ls /mnt/web-backup.
  6. Unmount your USB using: sudo sync; sudo umount /mnt

You could also do these steps from the Recovery Shell. You can access this by pressing SHIFT while Grub boots, selecting (advanced options), (recovery mode) for the latest kernel, and then going to the root option.

Note: With either solution, filesystem permissons might have become a bit wonky. You're going to want to sudo chown -R $USER:$USER <path_to_backup> on your new system, so you can actually read the data.