Backup & restore disk/partitions
mouse 3893 · person cloud · link
Last update
2023-11-17
2023
11-17
«squashfs»

1. Partition table

1
2
3
4
5
# dump raw ms-dos table/mbr
dd if=/dev/sdX of=mbr.bin     bs=512 count=1
dd if=/dev/sdX of=mbr_2mb.bin bs=2MB count=1 # paranoia
# restore
dd if=mbr.bin of=/dev/sdX
  • using sfdisk (version > 2.26 for GPT support):
1
2
3
4
5
6
7
8
9
# dump raw partition table
sfdisk -b -O disk /dev/sdX # disk-<device>-<offset>.bak
# restore
ls disk-*.bak | sed -r 's/(.+)-(0x.+).bak/dd if=\0 of=\/dev\/sdX seek=$((\2)) bs=1 conv=notrunc/' | bash

# dump partition table in text format
sfdisk -d /dev/sdX > sdcard.ptable
# restore
sfdisk    /dev/sdX < sdcard.ptable

2. Partition data

You can use FSarchiver or the old school way:

  • ext* efficient backup/restore:
1
2
e2image -ra /dev/sdXY - | pv | 7za a -mx=9 -si sdXY.e2i.7z
7za x -so sdXY.e2i.7z | pv | dd of=/dev/sdXY bs=4K

or create a squashfs mountable image1:

1
2
3
4
5
6
7
8
9
10
11
apt install squashfs-tools

# backup
mkdir tmpdir
mksquashfs tmpdir sdXY.squashfs -p "sdXY.img f 444 root root e2image -ra /dev/sdXY -" -comp xz

# mount and...
mkdir tmpdir mntdir
mount sdXY.squashfs tmpdir
pv tmpdir/sdXY.img | dd of=/dev/sdXY # ...restore or...
mount tmpdir/sdXY.img mntdir         # ...inspect
  • ntfs efficient backup/restore:
1
2
ntfsclone -s -o - /dev/sdXY | pv | 7za a -mx=9 -si sdXY.nc.7z
7za x -so sdXY.nc.7z | pv | ntfsclone -r -O /dev/sdXY -
  • raw full backup/restore:
1
2
dd if=/dev/sdXY | pv | 7za a -mx=9 -si sdXY.dd.7z
7za x -so sdXY.dd.7z | pv | dd of=/dev/sdXY bs=4K

Tips: dd seek/skip