하드디스크를 이미지로 백업하는 법
파티션 확인
lsblk fdisk -l /dev/sdc
# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT ... sdc 8:32 0 74.5G 0 disk ├─sdc1 8:33 0 73.2G 0 part ├─sdc2 8:34 0 1K 0 part └─sdc5 8:37 0 1.3G 0 part # fdisk -l /dev/sdc Disk /dev/sdc: 74.5 GiB, 80026361856 bytes, 156301488 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0x000e6f33 Device Boot Start End Sectors Size Id Type /dev/sdc1 * 63 153501074 153501012 73.2G 83 Linux /dev/sdc2 153501075 156296384 2795310 1.3G 5 Extended /dev/sdc5 153501138 156296384 2795247 1.3G 82 Linux swap / Solaris
에러 있는 디스크
에러 난 디스크, 혹은 한참을 묵혀둔 디스크를 백업
백업
dd_rescue /dev/sdc sdc.raw fsck -y sdc.raw
# dd_rescue /dev/sdc sdc.raw
dd_rescue: (info): Using softbs=128.0kiB, hardbs=4.0kiB
dd_rescue: (info): sdc.raw is sparse (50%)
dd_rescue: (info): expect to copy 78150744.0kiB from /dev/sdc
dd_rescue: (info): ipos: 8110080.0k, opos: 8110080.0k, xferd: 8110080.0k
errs: 0, errxfer: 0.0k, succxfer: 8110080.0k
+curr.rate: 59192kB/s, avg.rate: 59353kB/s, avg.load: 2.5%
>-----....................................< 10% ETA: 0:19:41
#
에러 없는 디스크
zero padding
zero padding 하면 이미지로 만들었을 때 용량이 줄어듬
상태가 좋지 않은 디스크에는 하지 않는 것이 좋음
mount /dev/sdc1 /mnt dd if=/dev/zero bs=4M | pv | dd of=/mnt/tmpzero.txt bs=4M sync rm /mnt/tmpzero.txt sync umount /mnt
# mount /dev/sdc1 /mnt # dd if=/dev/zero bs=4M | pv | dd of=/mnt/tmpzero.txt bs=4M. 34.9GiB 0:14:03 [31.1MiB/s] [ <=> ] dd: error writing '/mnt/tmpzero.txt': No space left on device ] 0+403467 records in 0+403466 records out 50254086144 bytes (50 GB, 47 GiB) copied, 1385.26 s, 36.3 MB/s # sync # rm /mnt/tmpzero.txt # sync # umount /mnt
HDD → dd 이미지
하드디스크 백업 명령어. 진행 상태를 보기 위해 pv 명령어를 파이핑 함.
raw 이미지는 마운트 가능함.
dd if=/dev/sdc bs=32M | pv | dd of=sdc.raw bs=512 conv=sparse
# dd if=/dev/sdc bs=32M | pv | dd of=sdc.raw bs=512 conv=sparse 10.4GiB 0:03:29 [39.0MiB/s] [ <=> ]
VM 이미지 생성
VM에 올리지 않는다면 만들지 않아도 무방함
qemu-img convert -O qcow2 -S 512 sdc.raw sdc.qcow2
팁
원격 디스크 백업
dd_rescue /dev/sda1 – | ssh ip@domain 'cat /tmp/backup.img'
참고
https://mapoo.net/os/oslinux/하드디스크-복구하는-방법-ddrescue/
https://moozing.wordpress.com/2015/06/24/converting-a-physical-machine-to-a-virtual-machine/
https://itectec.com/superuser/clone-only-space-in-use-from-hard-disk/
https://serverfault.com/questions/439128/dd-on-entire-disk-but-do-not-want-empty-portion
https://www.reddit.com/r/linuxquestions/comments/ax2af1/is_there_a_way_to_speed_up_the_dd_command/
https://www.geeksforgeeks.org/pv-command-in-linux-with-examples/
https://qemu.readthedocs.io/en/latest/tools/qemu-img.html