Migrate data from another provider to Hetzner

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

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


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


  • Connect to your old server and create a backup.

    Before you create the backup, make sure the service is not being used. Otherwise, any new data might be saved as you create the backup or after you created it. This data will then be missing in the backup.

    Create a new directory, such as ~/backups and save all the backups you need to migrate within that directory. This example uses tar to create the backups.


    • Save users, their permissions, and their ~/.ssh directories.

      You need to run these commands as root user.
      Replace /home/holu/ with the path of the user that created the backup directory.

      sudo su
      cd /etc && sudo tar -czvf /home/holu/backups/users-and-permissions.tar.gz -P passwd group shadow sudoers
      sudo tar czvf /home/holu/backups/ssh-directories.tar.gz $(find /home -maxdepth 3 -type d -name ".ssh")
      exit

    • Save firewall rules.

      For example iptables:

      sudo iptables-save > ~/backups/iptables_backup.txt
      sudo ip6tables-save > ~/backups/iptables6_backup.txt

    • Save your personal data:
      sudo tar -cvzf ~/backups/example-dir.tar.gz /file/or/directory/to/save

    If you need to migrate any additional data (for example, a database dump), create a backup of that too and save it in the ~/backups directory.


  • Create a new SSH key on your old server that you can use for the migration.

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

    Now copy the new public SSH key.



  • Connect to the new server.

    Edit ~/.ssh/authorized_key and add the public key you just created on the old server for the migration process (~/.ssh/migrate-backups.pub).

    nano ~/.ssh/authorized_keys

  • Connect to the old server again.

    Now that the public SSH key of the old server is saved on the new server, you should be able to copy the backup directory to the new server via SSH.

    Replace 203.0.113.1 with the IP address of the new Hetzner Cloud server.

    scp -i ~/.ssh/migrate-backups -r ~/backups root@203.0.113.1:~/

  • Next, connect to the new Hetzner Cloud server.

    The server should now also have the ~/backups directory with all the backup files. Example:

    root@new-server:~/backups# ls 
    example-dir.tar.gz  iptables6_backup.txt  iptables_backup.txt  users-and-permissions.tar.gz ssh-directories.tar.gz

    Extract the backups for the users, their permissions and the SSH settings:

    tar -xvf ~/backups/users-and-permissions.tar.gz -C /etc
    tar -xzvf ~/backups/ssh-directories.tar.gz -C /

    Now set up your server (for example, set up a firewall, install any required software, ...)

    iptables-restore < ~/backups/iptables_backup.txt
    ip6tables-restore < ~/backups/iptables6_backup.txt

    After the server is set up and any required software was installed, extract your personal data:

    Replace /target/directory with the target path.

    tar -xvf ~/backups/example-dir.tar.gz -C /target/directory

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.