iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🚑

How to recover data from Ubuntu when the GUI is unresponsive [Recovery Mode]

に公開

Introduction

An issue occurred on a laptop with Ubuntu installed where the mouse and keyboard stopped working after GUI login. This is a record of the steps taken to migrate important data saved on the PC to a USB flash drive using recovery mode.

PC Environment

Item Details
Model HP Elite
OS Ubuntu 19.04

Preparation: Prepare a USB Flash Drive

It is recommended to format the USB flash drive using the highly compatible exFAT file system so that data can be read and written on other operating systems such as Windows or macOS.

General Flow

  1. Boot into recovery mode
  2. Mount the USB flash drive
  3. Copy the data
  4. Post-processing

1. Entering Recovery Mode

What is Recovery Mode?

It is a special mode used to boot with a minimal environment to perform troubleshooting and repair tasks when a system problem occurs. Unlike a normal boot, it starts with the minimum necessary system resources and provides access to a shell with administrative privileges.

  1. After pressing the power button, repeatedly press the ESC key (for UEFI) or SHIFT key (for BIOS) to display the bootloader menu.

  2. When the bootloader menu appears, select Advanced options for Ubuntu.

  3. Select Ubuntu, with Linux... ⁠(recovery mode).

  4. When a screen like the following appears, select root Drop to root shell prompt.

  5. When Press Enter for maintenance is displayed, press the Enter key again.

  6. Success is when you reach a state where you can input commands (the prompt # is displayed).

If the File System is Read-Only

It seems that in recovery mode, the file system may be set to read-only by default. Since data cannot be copied in this state, execute the following command to allow writing.

mount -o remount,rw /

2. Mount the USB Device

Mount the connected USB flash drive to make it recognized and usable.

First, check the current disk configuration before connecting the USB flash drive.

  1. Before connecting the USB flash drive you want to mount, enter the following command to check the list:

    lsblk
    
  2. Connect the USB flash drive and enter the command again:

    lsblk
    
  3. Compare the results with the previous one to find the USB flash drive you want to mount (e.g., sdb). Select a partition with sufficient capacity from sda1, sda2, sda... (in this case, sda2).

    NAME   MAJ:MIN RM   SIZE RO TYPE
    sda      8:0    0  59.5G  0 disk
    ├─sda1   8:1    0   512M  0 part
    └─sda2   8:2    0    59G  0 part
    
  4. Create a mount point directory:

    mkdir /mnt/usb
    
  5. Mount the USB flash drive:

    mount /dev/sda2 /mnt/usb
    
  6. Move to the mount point directory and confirm it is correctly mounted:

    cd /mnt/usb
    ls
    
  7. Success is if the files and folders inside the USB flash drive are displayed.

3. Copying Data to the USB Flash Drive

Copy the data to the USB flash drive using the cp command.

  1. Check the directory containing the data you want to back up (in this example, we will back up the user's home directory).

    cd /home/user
    pwd
    
  2. For good measure, check the mount destination directory as well.

    cd /mnt/usb
    pwd
    
  3. Use the cp command to copy the data to the USB flash drive.

    cp -r -v /home/user /mnt/usb
    
  4. Copying may take some time depending on the amount of data. After executing the command, wait patiently without performing any operations until the prompt (#) is displayed again.

  5. Once the copy is complete, check the contents of the USB flash drive to ensure the data has been copied correctly.

    cd /mnt/usb
    ls
    

4. Post-processing

  1. Unmount (detach) the mounted USB flash drive.

    umount /mnt/usb
    
  2. Shut down the system and remove the USB drive after the power is off.

    shutdown -h now
    

Conclusion

The cp command has many options, so it is important to research them in advance. It is also essential to take regular backups. Please take this opportunity to reconsider your backup habits.

Discussion