Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mastef/0f59a1e601e4a747d3afcac9812f03f4 to your computer and use it in GitHub Desktop.
Save mastef/0f59a1e601e4a747d3afcac9812f03f4 to your computer and use it in GitHub Desktop.
Clone and restore a physical server's harddisk

Clone a server's harddisk

Login to Hetzner's web interface https://robot.your-server.de/ and activate the Rescue System for the server whose disk you wish to clone.

Ssh into the Rescue System (Replace 1.2.3.4 in the command below with the actual ip address of the server)

ssh -o HostKeyAlias=hetzner-rescue.1.2.3.4 root@1.2.3.4

Install partclone

apt-get update
apt-get install partclone

Spin up a Digital Ocean droplet for an hour and mount the droplet's filesystem to use it as a temporary store for the the harddisk image partclone generates (Hetzner's Rescue System runs entirely in memory and the server may not have sufficient memory to hold partclone's image)

modprobe fuse
mkdir -p /mnt/server_harddisk_backups/
sshfs deploy@5.6.7.8:/home/rails/server_harddisk_backups /mnt/server_harddisk_backups

Clone the server's root partition

partclone.ext3 --clone --source /dev/md2 --output /mnt/server_hardisk_backups/web_server.partclone

If the image is larger than 15 GB, gzip it

cd /mnt/server_hardisk_backups/
pv web_server.partclone | gzip > mb_web_server.partclone.gz

Create a server_backup user in AWS's IAM and attach a policy like

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Stmt1444932459000",
            "Effect": "Allow",
            "Action": [
                "s3:PutObject"
            ],
            "Resource": [
                "arn:aws:s3:::com.mywebsite.backup/server_harddisk_backups/*"
            ]
        }
    ]
}

Upload the harddisk image to Amazon S3

pip install awscli
cd /mnt/server_harddisk_backups/
AWS_ACCESS_KEY_ID=aws_access_key_id_for_server_backup_user AWS_SECRET_ACCESS_KEY=aws_secret_access_key_for_server_backup_user cp mb_web_server.partclone s3://com.mywebsite.backup/server_harddisk_backups/web_server.partclone 

Restore a server's harddisk

The steps for restoring server's harddisk from partclone images are very similar

  • Ssh into Hetzner's Rescue System
  • Mount a different server's filesystem so it can serve as temporary storage
  • Download a partclone image from Amazon S3
  • Restore the server's disk from the partclone image using https://partclone.org/usage/partclone.restore.php

Refer to Partclone's Usage Docs https://partclone.org/usage/ for information on using Partclone. I've based this wiki on information I've obtained from the Usage Docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment