wanderings/content/posts/partitions-backup.md

56 lines
1.7 KiB
Markdown
Raw Normal View History

2020-10-08 23:07:35 +02:00
+++
title = "disks backup"
date = 2020-09-16
tags = ["admin"]
+++
Backing up a whole disk is always quite sensible ; you rarely have the occasion
to test if your backup really works. Besides, the logical architecture of a disk
and its partitions can be quite sophisticated, with numerous different options,
that vary much between operating systems. Here we will describe how to backup
and restore a disk with some primary partitions. We will assume it is a bootable
disk with a Master Boot Record at the very beginning of it (MBR is limited to
446 bytes by conception). Note that most recent machines boot in UEFI mode,
which is not covered here.
## Backup a bootable disk
Let's say your disk is called sdX.
### Backup Master Boot Record
The MBR is a portion of bootable binary code. Do not try to read it with any
text editor ! They may slightly modify it, rendering it unbootable.
$ dd if=/dev/sdX of=path/to/disk_backup.mbr bs=446 count=1
### Backup partition table
$ sfdisk -d /dev/sdX > path/to/disk_backup.partition_table.txt
### Backup useful partitions
For each partition (numbered Y) :
$ dd if=/dev/sdXY of=path/to/partition_backup.dd status=progress
Compressed variant :
$ dd if=/dev/sdXY status=progress | gzip > path/to/partition_backup.dd.gz
## Restore a backed up disk into sdX
Edit `path/to/disk_backup.partition_table.txt` to reflect the partitions you
will actually restore. Then :
$ dd if=path/to/disk_backup.mbr of=/dev/sdX
$ sfdisk /dev/sdX < path/to/disk_backup.partition_table.txt
You might have to eject and re-plug the disk to see the added
partitions. Then for each of those :
$ zcat if=arch_zero_pY.dd.gz | dd of=/dev/sdXY
And finally restore MBR :
$ dd if=path/to/disk_backup.mbr of=/dev/sdX bs=446 count=1