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:
- Safety: You can ensure that the new drive is fully prepared and tested before replacing the old one.
- Data Verification: Transferring data over USB allows you to verify the transfer before making any changes to your system.
- Transfer Speed: Depending on the USB version (e.g., USB 2.0 vs. USB 3.0), transfer speeds may vary. USB 3.0 is significantly faster and is recommended if your drives support it.
- Power Supply: Ensure that the USB enclosure provides enough power for your hard drive, especially if itβs a larger 3.5-inch drive.
- Data Integrity: Always verify the data after transfer, as mentioned earlier, using
diffor similar tools to ensure nothing was lost or corrupted.
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:
- Ensure both the old and new hard drives are mounted. You might have something like this:
- Old drive (e.g.,
/dev/sda1mounted at/mnt/old_drive) - New drive (e.g.,
/dev/sdb1mounted at/mnt/mybtrfs)
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:
-a: Archive mode; preserves permissions, timestamps, and symlinks.-A: Preserves ACLs (Access Control Lists).-v: Verbose; shows the progress of the transfer.- Optional
-Xoption, will not allow compression! -X: Preserves extended attributes.
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/
- This will show any differences between the two directories.
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:
- DUP for metadata (-m dup)
- enabled no-holes (-O no-holes)
- enabled free-space-tree (-R free-space-tree)
- snippet.cpp
by: β βββ β βββββββ βββββββ β written: a while ago. Edited: June 8 2025
