我的命令克隆驱动器是否最佳?

我已经花了半个小时阅读准备克隆我的硬盘。 它有多个分区,包括Windows分区。 我打算买一个非常大的外置硬盘进行备份。 我希望能够使用这个克隆来恢复整个驱动器,以防出现问题(我即将进行一些操作系统重新洗牌)。 我想用dd学习如何做到这一点,因为我喜欢不需要安装任何东西的低级工具。

我从ubuntu论坛找到了以下有用的代码(使用live CD从root shell输入):

dd if=/dev/hda of=/dev/hdb & pid=$! while kill -USR1 $pid; do sleep 1; done 

(我知道我必须编辑输入和输出位置。)但是我有两个问题。 第一个是非常noobie:这个命令分为两行。 当我在感叹号后按回车键时,它会启动这个过程吗?

二,在其他网站上,它建议输入块大小。 像这样:

 # dd if=/dev/hda conv=sync,noerror bs=64K of=/mnt/sda1/hda.img 

我对块大小一无所知。 64K对吗? 看起来我的块大小是以下512字节,sudo fdisk -ul的输出:

 Disk /dev/sda: 750.2 GB, 750156374016 bytes 255 heads, 63 sectors/track, 91201 cylinders, total 1465149168 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 4096 bytes I/O size (minimum/optimal): 4096 bytes / 4096 bytes Disk identifier: 0xc3ffc3ff Device Boot Start End Blocks Id System /dev/sda1 * 63 143364059 71681998+ 7 HPFS/NTFS/exFAT Partition 1 does not start on physical sector boundary. /dev/sda2 976867328 1465147391 244140032 7 HPFS/NTFS/exFAT /dev/sda3 143364094 976867327 416751617 5 Extended Partition 3 does not start on physical sector boundary. /dev/sda5 143364096 162895871 9765888 82 Linux swap / Solaris /dev/sda6 162897920 205864959 21483520 83 Linux /dev/sda7 205867008 976867327 385500160 83 Linux Partition table entries are not in disk order Disk /dev/mapper/cryptswap1: 10.0 GB, 10000269312 bytes 255 heads, 63 sectors/track, 1215 cylinders, total 19531776 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 4096 bytes I/O size (minimum/optimal): 4096 bytes / 4096 bytes Disk identifier: 0x433bb3a7 Disk /dev/mapper/cryptswap1 doesn't contain a valid partition table 

谢谢。

进展

您列出的命令

 dd if=/dev/hda of=/dev/hdb & pid=$! while kill -USR1 $pid; do sleep 1; done 

是一个很好的双线,以定期获得dd的进展。 我也使用非常相似的一个。 看起来不错。 或许在这里找到它?

使用dd模块化:对齐和性能

您可以添加执行操作的块大小。 底层块设备的块大小对于完成同样好的操作并不重要,但出于性能原因,您可能希望选择一个适合您需要的块。

首先,有对齐的东西 。 如果你的块设备运行为512KiB(就像闪存驱动器一样),那么运行带有bs=512 (字节)的dd将是非常不幸的,因为这将从设备的角度为每个块引起1024次写入(!)。 在实践中,它不会那么糟糕,因为写入被缓冲并一次性使用,但在同步期间它仍然可以放大写入量。

然后还要考虑处理大量小型操作时的普通CPU使用开销。 复制大量数据时,一次获取兆字节效率更高。

我的最佳实践是从1MB开始,因为这是大多数设置的很好的倍数,包括RAID条带大小,LVM范围大小等。在我的带SSD的笔记本电脑上,我倾向于看到使用10MB作为块大小的轻微改进,而我在我的物理硬盘上看不到它了。

最后一块

不要担心驱动器/卷大小不是块大小的倍数。 将复制的最后一个块dd将被调整以匹配其上的最后一位数据。 您可以通过查看输出来查看最后一个块是否具有不同的大小。

 18335302+0 records out 

+0意味着它是完全匹配, +1意味着它不是。 没什么大不了。

也可以看看

  • 使用diskdump(dd)进行磁盘克隆的良好块大小

正如其他人所说,没有普遍正确的块大小; 对于一种情况或一种硬件来说,最佳选择对另一种情况来说可能非常低效。 而且,取决于盘的健康状况,可能优选使用与“最佳”块不同的块大小。

在现代硬件上非常可靠的一件事是,512字节的默认块大小往往比更优化的替代方案慢一个数量级。 如果有疑问,我发现64K是一个相当坚实的现代默认值。 虽然64K通常不是最佳块大小,但根据我的经验,它往往比默认值更有效。 64K还具有可靠性能的可靠历史:您可以从Eug-Lug邮件列表中找到一条消息 ,大约2002年,推荐块大小为64K。

为了确定最佳输出块大小,我编写了以下脚本,该脚本测试使用dd在不同块大小的范围内编写128M测试文件,默认值为512字节,最大值为64M。 请注意,此脚本在内部使用dd,因此请谨慎使用。

dd_obs_test.sh

 #!/bin/bash # Since we're dealing with dd, abort if any errors occur set -e TEST_FILE=${1:-dd_obs_testfile} TEST_FILE_EXISTS=0 if [ -e "$TEST_FILE" ]; then TEST_FILE_EXISTS=1; fi TEST_FILE_SIZE=134217728 if [ $EUID -ne 0 ]; then echo "NOTE: Kernel cache will not be cleared between tests without sudo. This will likely cause inaccurate results." 1>&2 fi # Header PRINTF_FORMAT="%8s : %s\n" printf "$PRINTF_FORMAT" 'block size' 'transfer rate' # Block sizes of 512b 1K 2K 4K 8K 16K 32K 64K 128K 256K 512K 1M 2M 4M 8M 16M 32M 64M for BLOCK_SIZE in 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864 do # Calculate number of segments required to copy COUNT=$(($TEST_FILE_SIZE / $BLOCK_SIZE)) if [ $COUNT -le 0 ]; then echo "Block size of $BLOCK_SIZE estimated to require $COUNT blocks, aborting further tests." break fi # Clear kernel cache to ensure more accurate test [ $EUID -eq 0 ] && [ -e /proc/sys/vm/drop_caches ] && echo 3 > /proc/sys/vm/drop_caches # Create a test file with the specified block size DD_RESULT=$(dd if=/dev/zero of=$TEST_FILE bs=$BLOCK_SIZE count=$COUNT conv=fsync 2>&1 1>/dev/null) # Extract the transfer rate from dd's STDERR output TRANSFER_RATE=$(echo $DD_RESULT | \grep --only-matching -E '[0-9.]+ ([MGk]?B|bytes)/s(ec)?') # Clean up the test file if we created one if [ $TEST_FILE_EXISTS -ne 0 ]; then rm $TEST_FILE; fi # Output the result printf "$PRINTF_FORMAT" "$BLOCK_SIZE" "$TRANSFER_RATE" done 

在GitHub上查看

我只在Debian(Ubuntu)系统和OSX Yosemite上测试过这个脚本,因此可能需要进行一些调整才能完成其他Unix风格的工作。

默认情况下,该命令将在当前目录中创建名为dd_obs_testfile的测试文件。 或者,您可以通过在脚本名称后面提供路径来提供自定义测试文件的路径:

 $ ./dd_obs_test.sh /path/to/disk/test_file 

脚本的输出是测试块大小及其各自传输速率的列表,如下所示:

 $ ./dd_obs_test.sh block size : transfer rate 512 : 11.3 MB/s 1024 : 22.1 MB/s 2048 : 42.3 MB/s 4096 : 75.2 MB/s 8192 : 90.7 MB/s 16384 : 101 MB/s 32768 : 104 MB/s 65536 : 108 MB/s 131072 : 113 MB/s 262144 : 112 MB/s 524288 : 133 MB/s 1048576 : 125 MB/s 2097152 : 113 MB/s 4194304 : 106 MB/s 8388608 : 107 MB/s 16777216 : 110 MB/s 33554432 : 119 MB/s 67108864 : 134 MB/s 

(注意:传输速率的单位因操作系统而异)

要测试最佳读取块大小,您可以使用或多或少相同的进程,但不是从/dev/zero读取并写入磁盘,而是从磁盘读取并写入/dev/null 。 执行此操作的脚本可能如下所示:

dd_ibs_test.sh

 #!/bin/bash # Since we're dealing with dd, abort if any errors occur set -e TEST_FILE=${1:-dd_ibs_testfile} if [ -e "$TEST_FILE" ]; then TEST_FILE_EXISTS=$?; fi TEST_FILE_SIZE=134217728 # Exit if file exists if [ -e $TEST_FILE ]; then echo "Test file $TEST_FILE exists, aborting." exit 1 fi TEST_FILE_EXISTS=1 if [ $EUID -ne 0 ]; then echo "NOTE: Kernel cache will not be cleared between tests without sudo. This will likely cause inaccurate results." 1>&2 fi # Create test file echo 'Generating test file...' BLOCK_SIZE=65536 COUNT=$(($TEST_FILE_SIZE / $BLOCK_SIZE)) dd if=/dev/urandom of=$TEST_FILE bs=$BLOCK_SIZE count=$COUNT conv=fsync > /dev/null 2>&1 # Header PRINTF_FORMAT="%8s : %s\n" printf "$PRINTF_FORMAT" 'block size' 'transfer rate' # Block sizes of 512b 1K 2K 4K 8K 16K 32K 64K 128K 256K 512K 1M 2M 4M 8M 16M 32M 64M for BLOCK_SIZE in 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864 do # Clear kernel cache to ensure more accurate test [ $EUID -eq 0 ] && [ -e /proc/sys/vm/drop_caches ] && echo 3 > /proc/sys/vm/drop_caches # Read test file out to /dev/null with specified block size DD_RESULT=$(dd if=$TEST_FILE of=/dev/null bs=$BLOCK_SIZE 2>&1 1>/dev/null) # Extract transfer rate TRANSFER_RATE=$(echo $DD_RESULT | \grep --only-matching -E '[0-9.]+ ([MGk]?B|bytes)/s(ec)?') printf "$PRINTF_FORMAT" "$BLOCK_SIZE" "$TRANSFER_RATE" done # Clean up the test file if we created one if [ $TEST_FILE_EXISTS -ne 0 ]; then rm $TEST_FILE; fi 

在GitHub上查看

这种情况的一个重要区别是测试文件是由脚本编写的文件。 不要将此命令指向现有文件,否则现有文件将被随机数据覆盖!

对于我的特定硬件,我发现128K是HDD上最优的输入块大小,32K是SSD上最优的。

虽然这个答案涵盖了我的大部分发现,但我需要确定一个最佳的dd块大小,足以写一篇关于它的博客文章 。 您可以在我执行的测试中找到更多细节。

这个StackOverflowpost也可能有用: dd:如何计算最佳块大小?