Complete Guide for Migrating to a New BTRFS Formatted Hard Drive

Using a USB enclosure to transfer data from your old hard drive to a new one is a common and practical approach, especially when you don’t have extra slots in your machine.

Considerations:


Format and initialize

Identify the Drive: list block devices to find your new hard drive:

lsblk

Look for the new device (e.g., /dev/sdb).


(Option 1) Partition the SSD using `parted`

Start parted with your SSD:

# Replace `/dev/sdX` with the correct device identifier.
sudo parted /dev/sdX

Create a new partition table:

mklabel gpt

Create a new partition:

mkpart primary btrfs 0% 100%

This command creates a primary partition that occupies the entire drive.

Exit parted:

quit

(Option 2) Partition the SSD using `fdisk`

Open fdisk to Create a New Partition:

Start fdisk:

# Replace sdX with your drive identifier (e.g., /dev/sdb).
sudo fdisk /dev/sdX

Create a New Partition:

Type g to create a new GPT partition table (if you want GPT; skip if you prefer MBR).

Type n to create a new partition.

Choose the partition number (default is fine).

For the first sector, press Enter to accept the default.

For the last sector, press Enter to use the entire disk (or specify a size).

Type w to write changes and exit fdisk.


Format the New Partition as Btrfs:

Format the partition you just created (e.g., /dev/sdX1):

sudo mkfs.btrfs /dev/sdX1

Mount the Partition: create a mount point and mount it:

sudo mkdir /mnt/mybtrfs
sudo mount /dev/sdX1 /mnt/mybtrfs

Verify the Filesystem: check the newly created Btrfs filesystem:

sudo btrfs filesystem show

Check if the partition is mounted:

df -h

You should see your Btrfs partition listed.

Add to /etc/fstab for Automatic Mounting: to mount the partition automatically at boot, edit the /etc/fstab file:

sudo vim /etc/fstab
/dev/sdX1 /mnt/mybtrfs btrfs defaults 0 0

Files Migration

To replace your old hard drive with the new one and copy all data—including hidden files and preserving permissions—you can use the rsync command. Here’s how to do it step by step:

Steps to Replace Old Hard Drive with New One

Mount Both Hard Drives:

If not already mounted, create mount points and mount them:

sudo mkdir /mnt/old_drive
sudo mount /dev/sda1 /mnt/old_drive

Copy Data Using rsync:

Use the following rsync command to copy all data from the old drive to the new drive while preserving permissions and copying hidden files:

sudo rsync -aAv /mnt/old_drive/ /mnt/mybtrfs/

Breakdown of options:

Verify the Copy: after the copying process completes, you can verify that the data has been copied correctly:

sudo diff -r /mnt/old_drive/ /mnt/mybtrfs/

Unmount Both Drives: once the data is copied and verified, unmount both drives:

sudo umount /mnt/old_drive
sudo umount /mnt/mybtrfs

Replace the Old Drive: physically replace the old hard drive with the new one in your system.


Data Integrity

While rsync has features that help ensure data integrity, it’s a good idea to perform additional verification after the transfer to ensure everything has copied correctly. Using checksums or file comparison tools will help confirm that your data is intact and uncorrupted.

rsync itself does not guarantee data integrity, but it provides several mechanisms to help ensure that data is transferred correctly and is uncorrupted.

Here’s how you can use rsync effectively and what you can do to enhance data integrity:

Features of `rsync`

File Comparison: by default, rsync checks file sizes and timestamps to determine if files need to be copied. This helps prevent unnecessary copies.

Checksum Verification: you can use the --checksum option, which forces rsync to compare files using checksums instead of relying solely on size and timestamp. This is more resource-intensive but increases the likelihood of identifying changes or corruption:

sudo rsync -aAv --checksum /mnt/old_drive/ /mnt/mybtrfs/

Verbose Output: the -v option gives you a detailed output of the transfer, allowing you to monitor the process.

Post-Transfer Verification

To ensure that data is uncorrupted after the transfer, consider using additional verification methods:

diff Command: After using rsync, you can compare the source and destination directories using:

sudo diff -r /mnt/old_drive/ /mnt/mybtrfs/

This will report any differences between the two directories.

Checksums: you can generate checksums (e.g., using md5sum or sha256sum) for all files in both locations and compare them:

cd /mnt/old_drive && find . -type f -exec sha256sum {} + | sort > /tmp/old_checksums.txt
cd /mnt/mybtrfs && find . -type f -exec sha256sum {} + | sort > /tmp/new_checksums.txt
diff /tmp/old_checksums.txt /tmp/new_checksums.txt

NOTE: several default settings have changed in version 5.15, please make sure this does not affect your deployments:



snippet.cpp
by:
▖     ▘▖▖
▌ ▌▌▛▘▌▙▌
▙▖▙▌▙▖▌ ▌
 
written: a while ago. Edited: June 8 2025