Backup a full hard disk locally or to a remote server and restore it

If you want to create a full backup of your entire disk, not just certain files and folders but you want an image file of your entire hard disk this can be done fairly simply no matter what operating system you are running, Linux, Windows, or even Unix/BSD variants. What you will need is somewhere to store the disk image file, it will be large, at least as big as the amount of data on your hard disk + around 10% for all the compressed empty space, this could be a remote network location or a local additional hard disk.
NOTE 1: This guide is not suitable for those using mdadm or ZFS software raid in raid 0, 5, 6 or 10

NOTE 2: Everything in this guide is potentially destructive to your data if you do not pay careful attention to the instructions.

Step 1 – Preparation of your server #

You will need some way to boot your server into a rescue mode, the hard disk you want to back up cannot be active while this happens, in most cases, you can boot your server into our rescue system if that is not available to you because you have a private VLAN or for some other reason you will be able to boot your server into rescue using a sysrescuecd ISO image via the servers IPMI we also have a guide to that here

Step 2 – Copy the disk image #

Once you have logged into the rescue system you can run the command: lsblk

# lsblk
sda                         8:0    0   8.2T  0 disk 
├─sda1                      8:1    0     2M  0 part 
├─sda2                      8:2    0 119.2G  0 part [SWAP]
├─sda3                      8:3    0  93.1G  0 part /
└─sda4                      8:4    0     8T  0 part 

At the top level, you should see your disk name, in the above example it is /dev/sda, it could also be /dev/vda if it is a virtual machine or /dev/nvme0n1 if it is a virtual disk.

Once you have identified the correct disk to backup you can now make the image file.

Method 1 – Copy to another disk #

Source: /dev/sda
Destination: /backups directory on /dev/sdb2

Make a mount point for /dev/sdb2 which contains the /backup directory then mount the partition and dd and compress the /dev/sda disk image

mkdir /mnt/mybackup
mount /dev/sdb2 /mnt/mybackup
dd bs=32M if=/dev/sda status=progress | gzip > /mnt/mybackup/backup/sda-backup-file.gz

If you ever need to restore this you would do the following assuming the same system a few days later:

mkdir /mnt/mybackup
mount /dev/sdb2 /mnt/mybackup
gunzip -c /mnt/mybackup/backup/sda-backup-file.gz | dd bs=32M of=/dev/sda status=progress

That will then restore your backup file to the disk /dev/sda.

Method 2 – Copy the disk image over ssh to another server #

Source: /dev/sda
Destination: A server with the IP into the /backup directory

We will copy the disk image over ssh using gzip to compress the 0’s (empty space)

dd bs=32M if=/dev/sda status=progress | gzip -9 | ssh root@ "dd of=/backup/sda-backup-file.gz"

You will be prompted for your root user password then it will start.

To restore your remote disk image backup to the same source server a few days later for example you would run the following on the remote backup server:

gunzip -c /backup/sda-backup-file.gz | ssh root@original-source-server-ip "dd bs=32M of=/dev/sda"

You will not be able to get progress on this restore