Safely Copying Entire Ubuntu Root Directory to a Different Drive (System Drive) via SSH
In this article, we will discuss how to safely copy the entire Ubuntu root directory from one drive to another, specifically from an HDD to an SSD, using SSH. This process is useful when you want to upgrade your system drive or migrate your operating system to a new drive. We will cover the key concepts and provide detailed instructions using subtitles, paragraphs, and code blocks. The content inside the code blocks will be properly formatted according to the programming language, including indentation and tabulation where needed.
Prerequisites
- Remote Ubuntu server with two drives: one HDD and one SSD
- Both drives are mounted: HDD at
/and SSD at/mnt/nvm - SSH access to the remote Ubuntu server
Preparing the New Drive
Before copying the root directory, you need to prepare the new drive by formatting it and creating a new file system. In this example, we will use the ext4 file system. You can use the following command to format the new drive:
sudo mkfs.ext4 /dev/nvme0n1p1
Next, you need to mount the new drive to a temporary directory. In this example, we will use /mnt/new_root:
sudo mkdir /mnt/new_root
sudo mount /dev/nvme0n1p1 /mnt/new_root
Copying the Root Directory
Now you can copy the root directory from the HDD to the SSD using the rsync command. The following command will copy all the files and directories, including hidden files, while preserving the ownership, permissions, and timestamps:
sudo rsync -aAXv --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} / /mnt/new_root
Updating the Fstab File
After copying the root directory, you need to update the fstab file to mount the new drive automatically at boot time. You can use the following command to generate the new fstab file:
sudo blkid
This command will display the UUIDs of all the drives. Find the UUID of the new drive and use it to update the fstab file:
sudo nano /mnt/new_root/etc/fstab
Add the following line to the fstab file, replacing UUID with the UUID of the new drive:
UUID=UUID / ext4 defaults 0 1
Changing the Root Directory
Now you need to change the root directory to the new drive. You can use the following command to boot into the new root directory:
sudo mount --bind /dev /mnt/new_root/dev
sudo mount --bind /proc /mnt/new_root/proc
sudo mount --bind /sys /mnt/new_root/sys
sudo chroot /mnt/new_root
After changing the root directory, you need to update the GRUB bootloader to boot from the new drive. You can use the following command to update the GRUB bootloader:
update-grub
Rebooting the System
Finally, you can reboot the system to boot from the new drive. Use the following command to reboot the system:
reboot
- Formatted the new drive and created a new file system
- Copied the root directory from the HDD to the SSD using the
rsynccommand - Updated the
fstabfile to mount the new drive automatically at boot time - Changed the root directory to the new drive and updated the GRUB bootloader
- Rebooted the system to boot from the new drive