Migrate partition from another provider to Hetzner

Last change on 2024-05-14 • Created on 2024-05-13 • ID: CL-F7002

Note that the method below shows how to migrate an entire partition to the new server. To save personal data only, see "Migrate data from another provider to Hetzner".


To migrate from a server with another provider to a Hetzner server, follow these steps:



  • Save and copy system configuration files:

    Connect to both servers:

    • To the new Hetzner cloud server
    • To the old server that will be migrated

    On the new Hetzner cloud server, create a new SSH key for the migration:

    ssh-keygen -t ed25519 -f ~/.ssh/migrate-backups

    Save the public SSH key you just created (~/.ssh/migrate-backups.pub) on the old server in the file ~/.ssh/authorized_keys.

    Now that the new Hetzner cloud server can connect to the old server via SSH, you can copy the system configuration files from the new server to the old server.

    First, save the system configuration files of your old server as a backup. Note that you might need to save different files depending on your operating system. This example uses Ubuntu.

    # On the old server, run these commands
    mv /etc/fstab /etc/fstab.bak
    mv /etc/netplan /etc/netplan.bak
    mv /etc/network/interfaces /etc/network/interfaces.bak
    mv /etc/default/grub /etc/default/grub.bak
    mv /boot/grub /boot/grub.bak

    Once the files are saved, you can use the new SSH key to copy the system configuration files of the new Hetzner cloud server to the old server. Note that you might need to copy different files depending on your operating system. This example uses Ubuntu.

    Replace 203.0.113.1 with the IP of your old server.

    # Run these commands on the new Hetzner cloud server
    scp -i ~/.ssh/migrate-backups /etc/fstab root@203.0.113.1:/etc/fstab
    scp -i ~/.ssh/migrate-backups -r /etc/netplan root@203.0.113.1:/etc/netplan
    scp -i ~/.ssh/migrate-backups /etc/network/interfaces root@203.0.113.1:/etc/network/interfaces
    scp -i ~/.ssh/migrate-backups /etc/default/grub root@203.0.113.1:/etc/default/grub
    scp -i ~/.ssh/migrate-backups -r /boot/grub root@203.0.113.1:/boot/grub

    The files should now also be available on the old server.


  • Start the new Hetzner cloud server in rescue mode.

    » Getting Started: Using Rescue


  • Copy the partitions of your old server to the new Hetzner cloud server.

    Connect to the new Hetzner cloud server in rescue.

    If you get a warning, see "CLI warning".

    • Create an SSH key.

      Create a new SSH key in rescue and save the public SSH key (~/.ssh/migrate-backups.pub) on the old server in the file ~/.ssh/authorized_keys.

      ssh-keygen -t ed25519 -f ~/.ssh/migrate-backups

    The next steps might be different depending on whether your old server has two different partitions for root and boot, or just a single partition for both.

    Examples:

    • Two partitions:
      holu@<old_server>:~# lsblk
      NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
      sda           8:0    0  100G  0 disk 
      ├─sda1        8:1    0  512M  0 part /boot
      └─sda2        8:2    0    20G  0 part /

    • One partition:
      holu@<old_server>:~# lsblk
      NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
      sda           8:0    0  100G  0 disk 
      └─sda1        8:1    0  100G  0 part 
        ├─myvg-root 253:0    0   95G  0 lvm  /
        └─myvg-boot 253:1    0    5G  0 lvm  /boot

    This "Getting Started" guide assumes that the old server used seperate partitions.

    • Copy the partitions:

      Run lsblk on both servers, the old one and the new one. This will show you the partition names. Make sure to adapt the commands below so that you use the correct partition names. In the example commands below, the partitions on the new Hetzner cloud server are called sda1, sda14, and sda15.

      ssh -i ~/.ssh/migrate-backups root@<old_server> "dd bs=4M if=/dev/<old-root-partition> status=progress" | dd bs=4M of=/dev/sda1
      ssh -i ~/.ssh/migrate-backups root@<old_server> "dd bs=4M if=/dev/<old-efi-partition> status=progress" | dd bs=4M of=/dev/sda14
      ssh -i ~/.ssh/migrate-backups root@<old_server> "dd bs=4M if=/dev/<old-boot-partition> status=progress" | dd bs=4M of=/dev/sda15
    • Mount the partitions:

      Before you mount the partitions, make sure they have a file system. You can use lsblk -o NAME,FSTYPE,LABEL to view them.

      Example:

      root@<new-server># lsblk -o NAME,FSTYPE
      sda
      ├─sda1  ext4
      ├─sda14
      └─sda15 vfat

      (see "Linux: Formatting and mounting a block storage device")

      If the partitions have a file system, you can now mount them:

      If you're not sure about the boot mount point (e.g. /mnt/boot or /mnt/boot/efi), you can check the output of cat /mnt/etc/fstab, for example.

      mount /dev/sda1 /mnt
      mount /dev/sda15 /mnt/boot/efi

  • Update the bootloader:

    Before you update the bootloader, double-check if all UUIDs in /boot/grub/grub.cfg and /etc/fstab are correct. Those need to be the UUIDs of the new server.

    Add write permissions and chroot into the system:

    mount -o bind /dev /mnt/dev
    mount --rbind /sys /mnt/sys
    mount -t proc /proc /mnt/proc
    chroot /mnt /bin/bash

    Reinstall grub and exit the chroot environment:

    Replace /dev/sda with the respective partition.

    update-grub
    grub-install /dev/sda
    exit

  • Restart the Hetzner cloud server

    reboot

    After the restart, the Hetzner cloud server should no longer be in rescue, and all data should be available.

The new server should be ready to go, and your data should be available. If you have a domain, remember to update your DNS records with the new IP address. Before you delete the old server, verify that all data was migrated successfully, and make sure everything is working as expected.