2021 您所在的位置:网站首页 dd命令备份系统 2021

2021

2023-08-16 10:02| 来源: 网络整理| 查看: 265

dd命令拷贝数据错误的问题定位及解决方法 dd迁移数据变化的问题说明定位过程从SD卡备份的文件,恢复为普通文件后不能挂载重新备份:不压缩dd直接写的文件挂载成功,但文件的内容错误弯路1:mount时把单词拼写错误无法挂载正确挂载及发现问题 问题详细说明dd直接备份的文件,dd直接恢复写到SD卡,仍然是内容错误 规避方法:用tar包备份的内容来恢复。弯路2:关于tar包解压正确tar恢复:指定目录拷贝隐藏目录/文件 准确定位笔记本,统信UOS用dd备份单个分区的文件,可挂载,但内容错误只备份SD卡的第二个分区,可挂载,内容错误把SD卡放入读卡器,只备份第二个分区,不能挂载UOS软件信息 台式机,mint linux,文件内容正常回到笔记本,在相同硬件情况下,更换操作系统puppy,文件内容正常puppy软件信息: 解决方法:替换dd文件总结:

dd迁移数据变化的问题说明

使用dd命令把coreelec系统从32G的SD卡迁移到4G卡后,机顶盒正常启动,可以正常进入coreelec的界面,但无网络;在设置-coreelec-网络/连接2个菜单下是空的,遥控器不能使用;更换为原SD卡则完全正常。

定位过程 从SD卡备份的文件,恢复为普通文件后不能挂载

参考之前命令: 备份:dd if=/dev/mmcblk0 bs=1M count=1025 status=progress | gzip > sdcard_bak1G.img.gz 恢复:gzip -dc sdcard_bak1G.img.gz | dd of=/dev/mmcblk0 bs=1M conv=fsync status=progress

只解压其中一个分区,解压到普通文件: gzip -dc /root/sdcard_bak1G.img.gz | dd of=/tmp/sd/sd1 skip=1056768 conv=fsync status=progress 没法识别分区,也不能挂载。可能是skip参数不对,未研究。

解压整个SD卡 root@xyhp:/tmp/sd# gzip -dc /root/sdcard_bak1G.img.gz | dd of=/tmp/sd/stor bs=1M conv=fsync status=progress

971833344 bytes (972 MB, 927 MiB) copied, 4 s, 243 MB/s 记录了0+32450 的读入 记录了0+32450 的写出 1074790400 bytes (1.1 GB, 1.0 GiB) copied, 4.71743 s, 228 MB/s

可以正常识别分区 root@xyhp:/tmp/sd# fdisk -l ./stor

Disk ./stor: 1 GiB, 1074790400 bytes, 2099200 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: 0x3c4e2362 Device Boot Start End Sectors Size Id Type ./stor1 * 8192 1056767 1048576 512M c W95 FAT32 (LBA) ./stor2 1056768 2105343 1048576 512M 83 Linux

普通文件中包含的分区,不同于/dev/xxx的设备文件,不能直接加数字来挂载。 root@xyhp:/tmp/sd# mount ./stor2 /mnt/a

mount: /mnt/a: special device ./stor2 does not exist.

这里要用到 offset=,sizelimit两个参数,单位是字节,比如offset=1056768*512

挂载失败,可能sizelimit还是错误拼写为sizelimt了 mount ./stor -o loop,offset= [ 1056768 ∗ 512 ] , s i z e l i m i t = [1056768*512],sizelimit= [1056768∗512],sizelimit=[1048576*512] /mnt/a mount -o loop,offset=541065216,sizelimit=536870912 ./stor /mnt/a mount -o loop,offset=4194304,sizelimit=536870912 ./stor /mnt/a

重新备份:不压缩dd直接写的文件

root@xyhp:/tmp/sd# df -h

文件系统 容量 已用 可用 已用% 挂载点 。。。 /dev/mmcblk0p2 488M 6.0M 447M 2% /media/xxyy/STORAGE /dev/mmcblk0p1 511M 213M 299M 42% /media/xxyy/COREELEC

root@xyhp:/tmp/sd# umount /media/xxyy/COREELEC/ root@xyhp:/tmp/sd# umount /media/xxyy/STORAGE/ root@xyhp:/tmp/sd# fdisk -l

。。。 Disk /dev/mmcblk0: 29.1 GiB, 31268536320 bytes, 61071360 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: 0x3c4e2362 Device Boot Start End Sectors Size Id Type /dev/mmcblk0p1 * 8192 1056767 1048576 512M c W95 FAT32 (LBA) /dev/mmcblk0p2 1056768 2105343 1048576 512M 83 Linux

root@xyhp:/tmp/sd# dd if=/dev/mmcblk0 of=/tmp/sd/sdbak1g_org.img bs=1M count=1100

status=progress 1143996416 bytes (1.1 GB, 1.1 GiB) copied, 51 s, 22.4 MB/s 这是进度信息 记录了1100+0 的读入 记录了1100+0 的写出 1153433600 bytes (1.2 GB, 1.1 GiB) copied, 51.4872 s, 22.4 MB/s root@xyhp:/tmp/sd#

备份文件的分区信息 root@xyhp:/tmp/sd# fdisk -l sdbak1g_org.img

Disk sdbak1g_org.img: 1.1 GiB, 1153433600 bytes, 2252800 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: 0x3c4e2362 Device Boot Start End Sectors Size Id Type sdbak1g_org.img1 * 8192 1056767 1048576 512M c W95 FAT32 (LBA) sdbak1g_org.img2 1056768 2105343 1048576 512M 83 Linux root@xyhp:/tmp/sd# 挂载成功,但文件的内容错误 弯路1:mount时把单词拼写错误无法挂载

挂载测试:单词sizelimt拼写错误 root@xyhp:/tmp/sd# mount -o loop,offset=4194304,sizelimt=536870912 sdbak1g_org.img /mnt/a

mount: /mnt/a: wrong fs type, bad option, bad superblock on /dev/loop0, missing codepage or helper program, or other error.

root@xyhp:/tmp/sd# losetup --find --show --offset=4194304 --sizelimt=536870912 sdbak1g_org.img

losetup: unrecognized option '--sizelimt=536870912' Try 'losetup --help' for more information. 正确挂载及发现问题

dd直接文件,挂载检查第一个分区的内容正确 root@xyhp:/tmp/sd# mount -o loop,offset=4194304,sizelimit=536870912 sdbak1g_org.img /mnt/a root@xyhp:/tmp/sd# ls /mnt/a aml_autoscript cfgload device_trees dtb.xml kernel.img.md5 resolution.ini SYSTEM.md5 Android config.ini dtb.img kernel.img LOST.DIR SYSTEM ‘System Volume Information’ root@xyhp:/tmp/sd# vi /mnt/a/config.ini

第二个分区挂载成功,文件正确,但文件中包含的内容错误 root@xyhp:/tmp/sd# mount -o loop,offset=541065216,sizelimit=536870912 sdbak1g_org.img /mnt/b root@xyhp:/tmp/sd# ls /mnt/b backup lost+found music pictures screenshots tvshows videos root@xyhp:/tmp/sd# vi /mnt/b/.config/hosts.conf root@xyhp:/tmp/sd# umount /mnt/b 而直接查看文件是对的 root@xyhp:/tmp/sd# mount /dev/mmcblk0p2 /mnt/c root@xyhp:/tmp/sd# vi /mnt/c/.config/hosts.conf root@xyhp:/tmp/sd#

问题详细说明

通过检查文件发现 /media/xxyy/STORAGE 和 /media/xxyy/COREELEC这2个文件系统的文件大小和日期都正常; 但vi检查对应文件发现STORAGE下的文本文件为全空白(比如STORAGE/.config/hosts.conf二进制ff),而COREELEC则正常(比如vi dtb.xml)

dd直接备份的文件,dd直接恢复写到SD卡,仍然是内容错误

root@xyhp:/tmp/sd/media/xxyy# dd if=/tmp/sd/sdbak1g_org.img of=/dev/mmcblk0 bs=1M conv=fsync status=progress 记录了1100+0 的读入 记录了1100+0 的写出 1153433600 bytes (1.2 GB, 1.1 GiB) copied, 224.214 s, 5.1 MB/s root@xyhp:/tmp/sd/media/xxyy# sync root@xyhp:/tmp/sd/media/xxyy# 一样的情况,第一个分区内容正常,第二个分区内容错误。

规避方法:用tar包备份的内容来恢复。 弯路2:关于tar包解压

root@xyhp:/tmp/sd# tar -zxf ~/COREELEC_STORAGE.tar.gz 不加星号会把目录直接拷贝过去 root@xyhp:/tmp/sd/media/xxyy# cp -rp STORAGE/ /mnt/b/

root@xyhp:/tmp/sd/media/xxyy# ls /mnt/b backup lost+found music pictures screenshots STORAGE tvshows videos root@xyhp:/tmp/sd/media/xxyy# rm /mnt/b/STORAGE/ -r

加星号,无法拷贝隐藏目录和文件 root@xyhp:/tmp/sd/media/xxyy# cp -rp STORAGE/* /mnt/b/

root@xyhp:/tmp/sd/media/xxyy# vi /mnt/b/.config/hosts.conf root@xyhp:/tmp/sd/media/xxyy# 正确tar恢复:指定目录拷贝隐藏目录/文件

root@xyhp:/tmp/sd# tar -zxf ~/COREELEC_STORAGE.tar.gz root@xyhp:/tmp/sd/media/xxyy# cd STORAGE/

root@xyhp:/tmp/sd/media/xxyy/STORAGE# ls backup lost+found music pictures screenshots tvshows videos root@xyhp:/tmp/sd/media/xxyy/STORAGE# ls -a . .. .ash_history backup .cache .config .kodi lost+found music pictures screenshots .ssh tvshows .update videos

root@xyhp:/tmp/sd/media/xxyy/STORAGE# cp -rp .ash_history backup .cache .config .kodi lost+found music pictures screenshots .ssh tvshows .update videos /mnt/b/ root@xyhp:/tmp/sd/media/xxyy/STORAGE#

准确定位 笔记本,统信UOS用dd备份单个分区的文件,可挂载,但内容错误 只备份SD卡的第二个分区,可挂载,内容错误

root@xyhp:~# dd if=/dev/mmcblk0p2 of=/tmp/sd2.img bs=1M status=progress

523239424 bytes (523 MB, 499 MiB) copied, 23 s, 22.7 MB/s 记录了512+0 的读入 记录了512+0 的写出 536870912 bytes (537 MB, 512 MiB) copied, 23.5989 s, 22.7 MB/s root@xyhp:~# fdisk -l /tmp/sd2.img Disk /tmp/sd2.img: 512 MiB, 536870912 bytes, 1048576 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 root@xyhp:~# file /tmp/sd2.img /tmp/sd2.img: Linux rev 1.0 ext4 filesystem data, UUID=44a7dec3-2b8f-435c-8a3c-1ffd60445d35, volume name "STORAGE" (needs journal recovery) (extents) (64bit) (large files) (huge files)

可挂载 root@xyhp:~# mount /tmp/sd2.img /mnt/a root@xyhp:~# df -h

文件系统 容量 已用 可用 已用% 挂载点 udev 7.5G 0 7.5G 0% /dev tmpfs 1.6G 3.2M 1.5G 1% /run /dev/nvme0n1p5 119G 14G 99G 13% / tmpfs 7.6G 17M 7.5G 1% /dev/shm tmpfs 5.0M 4.0K 5.0M 1% /run/lock tmpfs 7.6G 0 7.6G 0% /sys/fs/cgroup tmpfs 1.6G 36K 1.6G 1% /run/user/1000 /dev/nvme0n1p4 704G 322G 383G 46% /media/xxyy/data /dev/nvme0n1p3 130G 83G 47G 64% /media/xxyy/Windows /dev/mmcblk0p2 488M 6.0M 447M 2% /media/xxyy/STORAGE /dev/mmcblk0p1 511M 213M 299M 42% /media/xxyy/COREELEC /dev/loop0 488M 6.0M 447M 2% /mnt/a

文件列表正常

root@xyhp:~# cd /mnt/a root@xyhp:/mnt/a# ls backup lost+found music pictures screenshots tvshows videos root@xyhp:/mnt/a# ls -a . .. .ash_history backup .cache .config .kodi lost+found music pictures screenshots .ssh tvshows .update videos root@xyhp:/mnt/a# cd .config root@xyhp:/mnt/a/.config# ls aacs hosts.conf logind.conf.d procps rc_maps.cfg sysctl.d udev.rules.d boot.hint hwdb.d modprobe.d pulse rc_maps.cfg.sample system.d wireguard boot.status idmapd.conf modules-load.d pulse-daemon.conf.d samba.conf.sample timesyncd.conf.d exports.sample iptables nfs.conf rc_keymaps sleep.conf.d tmpfiles.d

文件内容显示错误

root@xyhp:/mnt/a/.config# vi nfs.conf root@xyhp:/mnt/a/.config# vi hosts.conf root@xyhp:/mnt/a/.config# 把SD卡放入读卡器,只备份第二个分区,不能挂载

把SD卡安装到读卡器,笔记本的UOS操作系统识别为sda

root@xyhp:~# fdisk -l Disk /dev/nvme0n1: 953.9 GiB, 1024209543168 bytes, 2000409264 sectors 。。。 Disk /dev/sda: 29.1 GiB, 31268536320 bytes, 61071360 sectors Disk model: STORAGE DEVICE 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: 0x3c4e2362 Device Boot Start End Sectors Size Id Type /dev/sda1 * 8192 1056767 1048576 512M c W95 FAT32 (LBA) /dev/sda2 1056768 2105343 1048576 512M 83 Linux

直接访问正常

root@xyhp:~# mount /dev/sda2 /mnt/a root@xyhp:~# vi /mnt/a/.config/hosts.conf root@xyhp:~# umount /mnt/a

dd拷贝的文件不能挂载 root@xyhp:~# dd if=/dev/sda2 of=/tmp/sd3-usb.img bs=1M status=progress

533725184 bytes (534 MB, 509 MiB) copied, 33 s, 16.2 MB/s 记录了512+0 的读入 记录了512+0 的写出 536870912 bytes (537 MB, 512 MiB) copied, 33.1615 s, 16.2 MB/s root@xyhp:~# file /tmp/sd3-usb.img /tmp/sd3-usb.img: Linux rev 1.0 ext4 filesystem data, UUID=44a7dec3-2b8f-435c-8a3c-1ffd60445d35, volume name "STORAGE" (needs journal recovery) (extents) (64bit) (large files) (huge files)

root@xyhp:~# mount /tmp/sd3-usb.img /mnt/b

mount: /mnt/b: can't read superblock on /dev/loop0. root@xyhp:~# UOS软件信息 root@xyhp:~# uname -rp 5.7.7-amd64-desktop unknown root@xyhp:~# dd --version dd (coreutils) 8.30 Copyright (C) 2018 Free Software Foundation, Inc. 许可证 GPLv3+:GNU 通用公共许可证第 3 版或更新版本。 本软件是自由软件:您可以自由修改和重新发布它。 在法律范围内没有其他保证。 由Paul Rubin、David MacKenzie 和Stuart Kemp 编写。 root@xyhp:~# cat /etc/os-release PRETTY_NAME="UnionTech OS Desktop 20 Home" NAME="uos" VERSION_ID="20 Home" VERSION="20 Home" ID=uos HOME_URL="https://www.chinauos.com/" BUG_REPORT_URL="http://bbs.chinauos.com" root@xyhp:~# 台式机,mint linux,文件内容正常

SD仍然装入读卡器,在台式机的mint系统下,惊奇的发现,同样的命令,dd备份第二个分区时,写入的文件;可以正常挂载,而且其中的内容也是正确的! 下一步就是想回到笔记本继续验证。

回到笔记本,在相同硬件情况下,更换操作系统puppy,文件内容正常

把SD装入读卡器,识别为sda root# fdisk -l

。。。 Disk /dev/sda: 29.12 GiB, 31268536320 bytes, 61071360 sectors Disk model: STORAGE DEVICE 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: 0x3c4e2362 Device Boot Start End Sectors Size Id Type /dev/sda1 * 8192 1056767 1048576 512M c W95 FAT32 (LBA) /dev/sda2 1056768 2105343 1048576 512M 83 Linux

dd备份到本地普通硬盘 root# dd if=/dev/sda2 of=/tmp/sd.img bs=1M

512+0 records in 512+0 records out 536870912 bytes (537 MB, 512 MiB) copied, 33.0558 s, 16.2 MB/s root# file /tmp/sd.img /tmp/sd.img: Linux rev 1.0 ext4 filesystem data, UUID=44a7dec3-2b8f-435c-8a3c-1ffd60445d35, volume name "STORAGE" (extents) (64bit) (large files) (huge files) root# ls /mnt cdrom data dvd flash floppy msdos ram1 swap zip

备份文件可以正常挂载,文件内容也正确 root# mount /tmp/sd.img /mnt/data root# cat /mnt/data/.config/hosts.conf

# hosts.conf # This configuration file allows you to manually map hostnames to # IP addresses # Format: # Example: 192.168.0.3 libreelec libreelec.mynetwork # NOTE: do not edit /etc/hosts directly # edit /storage/.config/hosts.conf then reboot puppy软件信息: root# uname -rp 5.4.53 x86_64 root# dd --version dd (coreutils) 8.30 Copyright (C) 2018 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later . This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Paul Rubin, David MacKenzie, and Stuart Kemp. root# cat /etc/os-release NAME=Puppy VERSION="9.5" ID=puppy_fossapup64 VERSION_ID=9.5 PRETTY_NAME="fossapup64 9.5" ANSI_COLOR="0;34" CPE_NAME="cpe:/o:puppy:puppy_linux:9.5" HOME_URL="http://puppylinux.com/" SUPPORT_URL="http://www.murga-linux.com/puppy/index.php" BUG_REPORT_URL="https://github.com/puppylinux-woof-CE/woof-CE" root# 解决方法:替换dd文件

回到UOS笔记本,仍然是SD卡装入读卡器。

root@xyhp:/tmp# fdisk -l

Disk /dev/nvme0n1: 953.9 GiB, 1024209543168 bytes, 2000409264 sectors 。。。 Disk /dev/sda: 29.1 GiB, 31268536320 bytes, 61071360 sectors Disk model: STORAGE DEVICE 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: 0x3c4e2362 Device Boot Start End Sectors Size Id Type /dev/sda1 * 8192 1056767 1048576 512M c W95 FAT32 (LBA) /dev/sda2 1056768 2105343 1048576 512M 83 Linux

更换dd文件: 把puppy启动后,挂载UOS所在的磁盘,然后后puppy的/bin/dd文件,拷贝到UOS的/usr/bin目录下,注意修改权限。 替换后对比:2个dd文件的版本都是8.30,但UOS的文件更小。

root@xyhp:/tmp# which dd /usr/bin/dd root@xyhp:/tmp# which dd.bak_uos /usr/bin/dd.bak_uos root@xyhp:/tmp# cd /usr/bin/ root@xyhp:/usr/bin# ls -l dd dd.bak_uos -rwxr-xr-x 1 root root 80256 Sep 5 2019 dd -rwxr-xr-x 1 root root 76712 Feb 28 2019 dd.bak_uos root@xyhp:/usr/bin# dd --version dd (coreutils) 8.30 Copyright (C) 2018 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later . This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Paul Rubin, David MacKenzie, and Stuart Kemp. root@xyhp:/usr/bin# dd.bak_uos --version dd (coreutils) 8.30 Copyright (C) 2018 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later . This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Paul Rubin, David MacKenzie, and Stuart Kemp.

用新的dd文件,备份第二个分区到文件 root@xyhp:/tmp# dd if=/dev/sda2 of=/tmp/sd.img bs=1M status=progress

530579456 bytes (531 MB, 506 MiB) copied, 33 s, 16.1 MB/s 512+0 records in 512+0 records out 536870912 bytes (537 MB, 512 MiB) copied, 33.3744 s, 16.1 MB/s root@xyhp:/tmp# file /tmp/sd.img /tmp/sd.img: Linux rev 1.0 ext4 filesystem data, UUID=44a7dec3-2b8f-435c-8a3c-1ffd60445d35, volume name "STORAGE" (needs journal recovery) (extents) (64bit) (large files) (huge files)

正常挂载 root@xyhp:/tmp# mount /tmp/sd.img /mnt/a 文件内容也正常了。

root@xyhp:/usr/bin# vi /mnt/a/.config/hosts.conf root@xyhp:/usr/bin# 总结:

dd逐自己拷贝到工具,拷贝出来的内容居然会发生变化,这完全颠覆了我的认知!因此对这个问题有极大的不解和好奇,经过3天的定位,终于定位到时UOS采用的dd命令有问题。 期间,也学习并实践了,重建分区表扩容文件系统不丢失数据的方法。 对于利用win10操作系统已有的EFI分区,修改grub.cfg的方法,有了进一步的认识。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有