mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-04 22:00:34 +08:00
Merge pull request #5413 from ictlyh/master
Translated tech/20170112 Partition Backup.md
This commit is contained in:
commit
c27f13e27d
@ -1,147 +0,0 @@
|
||||
ictlyh Translating
|
||||
Partition Backup
|
||||
============
|
||||
|
||||
Many times you may have data on a partition, especially on a Universal Serial Bus (USB) drive. It may be necessary at times to make a copy of a drive or a single partition on it. Raspberry Pi users definitely have this need for the bootable SD Cards. Owners of other small form computers can also find this useful. Sometimes it is best to make a backup quickly if a device seems to be failing.
|
||||
|
||||
To perform the examples in this article you will need a utility called 'dcfldd'.
|
||||
|
||||
**The dcfldd Utility**
|
||||
|
||||
The utility is an enhancement of the 'dd' utility from the 'coreutils' package. The 'dcfldd' was made by Nicholas Harbour while he worked at the Department of Defense Computer Forensics Lab (DCFL). The name is then based off where he worked – 'dcfldd'.
|
||||
|
||||
For systems still using the CoreUtils 8.23 or less there isn't an option to easily see the progress of the copy being performed. Sometimes it seems as if nothing is happening and you are tempted to stop the copy.
|
||||
|
||||
**NOTE:** If you have the ‘dd’ version 8.24 or later then you need not use ‘dcfldd’ and simply replace any ‘dcfldd’ with ‘dd’. All other parameters will work.
|
||||
|
||||
On a Debian system simply use your Package Manager and search for 'dcfldd'. You can also open a Terminal and use the command:
|
||||
|
||||
_sudo apt-get install dcfldd_
|
||||
|
||||
For a Red Hat system try the following:
|
||||
|
||||
_cd /tmp
|
||||
wget dl.fedoraproject.org/pub/epel/6/i386/dcfldd-1.3.4.1-4.el6.i686.rpm
|
||||
sudo yum install dcfldd-1.3.4.1-4.el6.i686.rpm
|
||||
dcfldd --version_
|
||||
|
||||
**NOTE:** The above installs the 32-bit version. For the 64-bit version use the following commands:
|
||||
|
||||
_cd /tmp
|
||||
wget dl.fedoraproject.org/pub/epel/6/x86_64/dcfldd-1.3.4.1-4.el6.x86_64.rpm
|
||||
sudo yum install dcfldd-1.3.4.1-4.el6.x86_64.rpm
|
||||
dcfldd --version_
|
||||
|
||||
The last statement of each set of commands will list the version of the 'dcfldd' and show you that the command file has been downloaded.
|
||||
|
||||
**NOTE: **Be sure you execute the ‘dd’ or ‘dcfldd’ as root.
|
||||
|
||||
Now that you have the command installed you can continue on with using it to backup and restore partitions.
|
||||
|
||||
**Backup Partitions**
|
||||
|
||||
When backing up a drive it is possible to back up the whole drive or a single partition. If the drive has multiple partitions then each can be backed up separately.
|
||||
|
||||
Before we get too far in performing a backup let's look at the difference of a drive and partition. Let's assume we have an SD Card which is formatted as one large drive. The SD Card contains only one partition. If the space is split to allow the SD Card to be seen as two drives, then it has two partitions. If a program like GParted were opened for an SD Card, as shown in Figure 1, you can see it has two partitions.
|
||||
|
||||
**FIGURE 1**
|
||||
|
||||
The drive is /dev/sdc and contains both partitions /dev/sdc1 and /dev/sdc2.
|
||||
|
||||
Let's take, for instance, an SD Card from a Raspberry Pi. The SD Card is 8 GB in size and has two partitions (as shown in Figure 1). The first partition contains BerryBoot which is a boot loader. The second partition contains Kali. There is no space available to install a second Operating System (OS). A second SD Card is to be used which is 16 GB in size, but to copy it to the second SD Card the first one must be backed up.
|
||||
|
||||
To back up the first SD Card we will back up the drive which is /dev/sdc. The command to perform the backup is as follows:
|
||||
|
||||
_dcfldd if=/dev/sdc of=/tmp/SD-Card-Backup.img_
|
||||
|
||||
The backup is made of the Input File (if) and the output file (of) is set to the folder '/tmp' and a file called 'SD-Card-Backup.img'.
|
||||
|
||||
The 'dd' and 'dcfldd' both read and write the files one byte at a time. Technically with the above command it reads and writes a default of 512 bytes at a time. Keep in mind that the copy is an exact copy – bit for bit and byte for byte.
|
||||
|
||||
The default of 512 bytes can be changed with the parameter for Block Size - 'bs='. For instance, to read/write 1 megabyte at a time the parameter 'bs=1M'. There can be some discrepancy in the abbreviations used as follows:
|
||||
|
||||
* b – 512 bytes
|
||||
* KB – 1000 bytes
|
||||
* K – 1024 bytes
|
||||
* MB – 1000x1000 bytes
|
||||
* M – 1024x1024 bytes
|
||||
* GB – 1000x1000x1000 bytes
|
||||
* G – 1024x1024x1024 bytes
|
||||
|
||||
You can specify the read blocks and write blocks separately. To specify the read amount use ‘ibs=’. To specify the write amount then use ‘obs=’.
|
||||
|
||||
I performed a test to backup a 120 MB partition using three different Block Sizes. The first was the default of 512 bytes and it took 7 seconds. The second was a Block Size of 1024 K and took 2 seconds. The third had a Block Size of 2048 K and took 3 seconds. The times will vary depending on the system, various other hardware implementations, but in general larger block sizes than the defaults can be a little faster.
|
||||
|
||||
Once you have a backup made you will need to know how to restore the data to a drive.
|
||||
|
||||
**Restore Partitions**
|
||||
|
||||
Now that we have a backup at some point we can assume the data may become corrupted or need to be restored for some reason.
|
||||
|
||||
The command is the same as the backup except that the source and target are reversed. In the above example the command would be changed to:
|
||||
|
||||
_dcfldd of=/dev/sdc if=/tmp/SD-Card-Backup.img_
|
||||
|
||||
Here, the image file is being used as the input file (if) and the drive (sdc) is used as the output file (of).
|
||||
|
||||
**NOTE:** Do remember that the output device will be overwritten and all data on it will be lost. It is usually best to remove all partitions from the SD Card with GParted before restoring data.
|
||||
|
||||
If you have a use for multiple SD Cards, such as having multiple Raspberry Pi boards, you can write to multiple cards at once. To do this you need to know the card’s ID on the system. For example, let’s say we want to copy the image ‘BerryBoot.img’ to two SD Cards. The SD Cards are at /dev/sdc and /dev/sdd. The command will be set to read/write in 1 MB Blocks while showing the progress. The command would be:
|
||||
|
||||
_dcfldd if=BerryBoot.img bs=1M status=progress | tee >(dcfldd of=/dev/sdc) | dcfldd of=/dev/sdd_
|
||||
|
||||
In this command the first dcfldd uses the input file and sets the Block Size to 1 MB. The status is set to show the progress. The input is then piped (|) to the command ‘tee’. The ‘tee’ is used to split the input to multiple places. The first output is to the command ‘(dcfldd of=/dev/sdc)’. The command is in parenthesis and will be performed as a command. The last pipe (|) is needed, otherwise the ‘tee’ command will send the information to the ‘stdout’ (screen) as well. So the final output is to the command ‘_dcfldd of=/dev/sdd’_. If you had a third card, or even more, simply add another redirector and command such as ‘_>(dcfldd of=/dev/sde_’.
|
||||
|
||||
**NOTE:** Do remember that the final command must be after a pipe (|).
|
||||
|
||||
Data being written must be verified at times to be sure the data is correct.
|
||||
|
||||
**Verify Data**
|
||||
|
||||
Once an image is made or a backup restored you can verify the data being written. To verify data you will use a different program called ‘_diff_’.
|
||||
|
||||
To use ‘diff’ you will need to designate the location of the image file and the physical media where it was copied from or written to on the system. The ‘_diff_’ command can be used after the backup was made or after the image was restored.
|
||||
|
||||
The command has two parameters. The first is the physical media and the second is the image file name.
|
||||
|
||||
From the example of ‘_dcfldd of=/dev/sdc if=/tmp/SD-Card-Backup.img’_ the ‘_diff_’ command would be:
|
||||
|
||||
_diff /dev/sdc /tmp/SD-Card-Backup.img_
|
||||
|
||||
If any differences are found between the image and the physical device you will be alerted. If no messages are given then the data has been verified as identical.
|
||||
|
||||
Making sure the data is identical is key to verifying the integrity of the backup or restore. One main problem to watch for when performing a Backup is image size.
|
||||
|
||||
**Splitting The Image**
|
||||
|
||||
Let’s assume you want to back up a 16GB SD Card. The image will then be approximately the same size. What if you can only back it up to a FAT32 partition? The maximum file size limit is 4 GB on FAT32.
|
||||
|
||||
What must be done is that the file will have to be split into 4 GB pieces. The splitting of the image file can be done while it is being written by piping (|) the data to the ‘_split_’ command.
|
||||
|
||||
The backup is performed in the same way, but the command will include the pipe and split command. The example backup command is ‘_dcfldd if=/dev/sdc of=/tmp/SD-Card-Backup.img_’ and the new command for splitting the file is as follows:
|
||||
|
||||
_dcfldd if=/dev/sdc | split -b 4000MB - /tmp/SD-Card-Backup.img_
|
||||
|
||||
**NOTE:** The size suffix means the same as for the ‘_dd_’ and ‘_dcfldd_’ command. The dash by itself in the ‘_split_’ command is used to fill the input file which is being piped from the the ‘_dcfldd_’ command.
|
||||
|
||||
The files will be saved as ‘_SD-Card-Backup.imgaa_’ and ‘_SD-Card-Backup.imgab_’ and so on. If you are worried about the file size being too close to the 4 GB limit, then try 3500MB.
|
||||
|
||||
Restoring the files back to the drive is simple. You use the command ‘_ca_’ to join them and then write the output using ‘_dcfldd_’ as follows:
|
||||
|
||||
_cat /tmp/SD-Card-Backup.img* | dcfldd of=/dev/sdc_
|
||||
|
||||
You can include any desired parameters to the command for the ‘_dcfldd_’ portion.
|
||||
|
||||
I hope you understand and can perform any needed backup and restoration of data as you need for SD Cards and the like.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.linuxforum.com/threads/partition-backup.3638/
|
||||
|
||||
作者:[Jarret][a]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://www.linuxforum.com/members/jarret.268/
|
146
translated/tech/20170112 Partition Backup.md
Normal file
146
translated/tech/20170112 Partition Backup.md
Normal file
@ -0,0 +1,146 @@
|
||||
分区备份
|
||||
============
|
||||
|
||||
通常你可能会把数据放在一个分区上,尤其是通用串行总线(USB)设备。有时候可能需要对该设备或者上面的一个分区进行备份。树莓派用户为了可引导 SD 卡当然有这个需求。其它小型机的用户也会发现这非常有用。有时候设备看起来要出现故障时最好快速做个备份。
|
||||
|
||||
进行本文中的实验你需要一个叫 ‘dcfldd’ 的工具。
|
||||
|
||||
**dcfldd 工具**
|
||||
|
||||
该工具是 'coreutils' 软件包中 ‘dd’ 工具的增强版。‘dcfldd’ 是 Nicholas Harbour 在国防部计算机取证实验室(DCFL)工作期间研发的。该工具的名字也基于他工作的地方 - ‘dcfldd’。
|
||||
|
||||
对于仍然在使用 CoreUtils 8.23 或更低版本的系统,无法轻松查看正在创建副本的进度。有时候看起来就像什么都没有发生然后你就想取消掉备份。
|
||||
|
||||
**注意:**如果你使用 8.24 或更新版本的 ‘dd’ 工具,你就不需要使用 ‘dcfldd’,只需要用 ‘dd’ 替换 ‘dcfldd’ 即可。所有其它参数仍然适用。
|
||||
|
||||
在 Debian 系统上你只需要在 Package Manager 中搜索 ‘dcfldd’。你也可以打开一个终端然后输入下面的命令:
|
||||
|
||||
_sudo apt-get install dcfldd_
|
||||
|
||||
对于 Red Hat 系统,可以用下面的命令:
|
||||
|
||||
_cd /tmp
|
||||
wget dl.fedoraproject.org/pub/epel/6/i386/dcfldd-1.3.4.1-4.el6.i686.rpm
|
||||
sudo yum install dcfldd-1.3.4.1-4.el6.i686.rpm
|
||||
dcfldd --version_
|
||||
|
||||
**注意:** 上面的命令安装的是 32 位版本。对于 64 位版本,使用下面的命令:
|
||||
|
||||
_cd /tmp
|
||||
wget dl.fedoraproject.org/pub/epel/6/x86_64/dcfldd-1.3.4.1-4.el6.x86_64.rpm
|
||||
sudo yum install dcfldd-1.3.4.1-4.el6.x86_64.rpm
|
||||
dcfldd --version_
|
||||
|
||||
每组命令中的最后一个语句会列出 ‘dcfldd’ 的版本并显示该命令文件已经被加载。
|
||||
|
||||
**注意:**确保你以 root 用户执行 ‘dd’ 或者 ‘dcfldd’ 命令。
|
||||
|
||||
安装完该工具后你就可以继续使用它备份和恢复分区。
|
||||
|
||||
**备份分区**
|
||||
|
||||
备份设备的时候可以备份整个设备也可以只是其中的一个分区。如果设备有多个分区,我们可以分别备份每个分区。
|
||||
|
||||
在进行备份之前,先让我们来看一下设备和分区的区别。假设我们有一个已经被格式化为一大块设备 SD 卡。SD 卡只有一个分区。如果空间被切分使得 SD 卡看起来是两个设备,那么它就有两个分区。如果用类似 GParted 的程序打开 SD 卡,如图 1 所示,你可以看到它有两个分区。
|
||||
|
||||
**图 1**
|
||||
|
||||
设备 /dev/sdc 有 /dev/sdc1 和 /dev/sdc2 两个分区。
|
||||
|
||||
假设我们有一个树莓派中的 SD 卡。SD 卡容量为 8 GB,有两个分区(如图 1 所示)。第一个分区存放 BerryBoot 启动引导器。第二个分区存放 Kali(译者注:Kali Linux 是一个 Debian 派生的 Linux 发行版)。现在已经没有可用的空间用来安装第二个操作系统。我们使用大小为 16 GB 的第二个 SD 卡,但拷贝到第二个 SD 卡之前第一个 SD 卡必须先备份。
|
||||
|
||||
要备份第一个 SD 卡我们需要备份设备 /dev/sdc。进行备份的命令如下所示:
|
||||
|
||||
_dcfldd if=/dev/sdc of=/tmp/SD-Card-Backup.img_
|
||||
|
||||
备份包括输入文件(if)以及被设置为 '/tmp' 目录下名为 'SD-Card-Backup.img' 的输出文件(of)。
|
||||
|
||||
‘dd’ 和 ‘dcfldd’ 都是每次读写文件中的一个字节。通过上述命令,它一次读写的默认值为 512 个字节。记住,该复制是一个精准的拷贝 - 逐位逐字节。
|
||||
|
||||
默认的 512 个字节可以通过块大小参数 - ‘bs=’ 更改。例如,要每次读写 1 兆字节,参数为 ‘bs=1M’。以下所用的缩写有一些差异:
|
||||
|
||||
* b – 512 字节
|
||||
* KB – 1000 字节
|
||||
* K – 1024 字节
|
||||
* MB – 1000x1000 字节
|
||||
* M – 1024x1024 字节
|
||||
* GB – 1000x1000x1000 字节
|
||||
* G – 1024x1024x1024 字节
|
||||
|
||||
你也可以单独指定读和写的块大小。要指定读块的大小使用 ‘ibs=’。要指定写块的大小使用 ‘obs=’。
|
||||
|
||||
我使用三个不同的块大小做了一个 120 MB 分区的备份测试。第一个时候默认的 512 字节,它用了 7 秒钟。第二个块大小为 1024 K,它用时 2 秒。第三个块大小是 2048 K,它用时 3 秒。用时会随系统以及其它硬件实现的不同而变化,但通常来说更大的块大小会比默认的稍微快一点。
|
||||
|
||||
一旦你完成了一次备份,你还需要知道如何把数据恢复到设备中。
|
||||
|
||||
**恢复分区**
|
||||
|
||||
现在我们已经有了一个备份点,假设数据可能被损毁了或者由于某些原因需要进行恢复。
|
||||
|
||||
命令和备份时相同,只是源和目标相反。对于上面的例子,命令会变为:
|
||||
|
||||
_dcfldd of=/dev/sdc if=/tmp/SD-Card-Backup.img_
|
||||
|
||||
这里,镜像文件被用作输入文件(if)而设备(sdc)被用作输出文件(of)。
|
||||
|
||||
**注意:** 要记住输出设备会被重写,它上面的所有数据都会丢失。通常来说在恢复数据之前最好用 GParted 删除 SD 卡上的所有分区。
|
||||
|
||||
假如你在使用多个 SD 卡,例如多个树莓派主板,你可以一次性写多块 SD 卡。为了做到这点你需要知道系统中卡的 ID。例如,假设我们想把镜像 ‘BerryBoot.img’ 拷贝到两个 SD 卡。SD 卡分别是 /dev/sdc 和 /dev/sdd。下面的命令在显示进度时每次读写 1 MB 的块。命令如下:
|
||||
|
||||
_dcfldd if=BerryBoot.img bs=1M status=progress | tee >(dcfldd of=/dev/sdc) | dcfldd of=/dev/sdd_
|
||||
|
||||
在这个命令中,第一个 dcfldd 指定输入文件并把块大小设置为 1 MB。status 被设置为显示进度。然后输入通过管道(|)传输给命令 ‘tee’。‘tee’ 用于将输入分发到多个地方。第一个输出是到命令 ‘(dcfldd of=/dev/sdc)’。命令被放到小括号内被作为一个命令执行。我们还需要最后一个管道(|),否则命令 ‘tee’ 会把信息发送到 ‘stdout’ (屏幕)。因此,最后的输出是被发送到命令 ‘_dcfldd of=/dev/sdd’_。如果你有第三个 SD 卡,甚至更多,只需要添加另外的重定向和命令,类似 ‘_>(dcfldd of=/dev/sde_’。
|
||||
|
||||
**注意:**记住最后一个命令必须在管道(|)后面。
|
||||
|
||||
必须验证写的数据确保数据是正确的。
|
||||
|
||||
**验证数据**
|
||||
|
||||
一旦创建了一个镜像或者恢复了一个备份,你可以验证这些写的数据。要验证数据你会使用名为 ‘_diff_’ 的领一个不同程序。
|
||||
|
||||
使用 ‘diff’ 你需要指定镜像文件的位置以及系统中拷贝自或写入的物理媒介。你可以在创建备份或者恢复了一个镜像之后使用 ‘_diff_’ 命令。
|
||||
|
||||
该命令有两个参数。第一个是物理媒介,第二个是镜像文件名称。
|
||||
|
||||
对于例子 ‘_dcfldd of=/dev/sdc if=/tmp/SD-Card-Backup.img’_,对应的 ‘_diff_’ 命令是:
|
||||
|
||||
_diff /dev/sdc /tmp/SD-Card-Backup.img_
|
||||
|
||||
如果镜像和物理设备有任何的不同,你会被告知。如果没有显示任何信息,那么数据就验证为完全相同。
|
||||
|
||||
确保数据完全一致是验证备份和恢复完整性的关键。进行备份时需要注意的一个主要问题是镜像大小。
|
||||
|
||||
**分割镜像**
|
||||
|
||||
假设你想要备份一个 16GB 的 SD 卡。镜像文件大小会大概相同。如果你只能把它备份到 FAT32 分区会怎样呢?FAT32 最大文件大小限制是 4 GB。
|
||||
|
||||
必须做的是文件必须被切分为 4 GB 的分片。通过管道(|)将数据传输给 ‘_split_’ 命令可以切分正在被写的镜像文件。
|
||||
|
||||
创建备份的方法相同,但命令会包括管道和切分命令。对于命令为 ‘_dcfldd if=/dev/sdc of=/tmp/SD-Card-Backup.img_’ 的事例备份,切分文件的新命令如下:
|
||||
|
||||
_dcfldd if=/dev/sdc | split -b 4000MB - /tmp/SD-Card-Backup.img_
|
||||
|
||||
**注意:** 大小后缀和 ‘_dd_’ 及 ‘_dcfldd_’ 命令的意义相同。 ‘_split_’ 命令中的破折号用于将通过管道从 ‘_dcfldd_’ 命令传输过来的数据填充到输入文件。
|
||||
|
||||
文件会被保存为 ‘_SD-Card-Backup.imgaa_’ 和 ‘_SD-Card-Backup.imgab_’,如此类推。如果你担心文件大小太接近 4 GB 的限制,可以试着用 3500MB。
|
||||
|
||||
将文件恢复到设备也很简单。你使用 ‘_cat_’ 命令将它们连接起来然后像下面这样用 ‘_dcfldd_’ 写输出:
|
||||
|
||||
_cat /tmp/SD-Card-Backup.img* | dcfldd of=/dev/sdc_
|
||||
|
||||
你可以在 “_dcfldd_” 命令中包含任何需要的参数。
|
||||
|
||||
我希望你了解并能执行任何需要的数据备份和恢复,正如 SD 卡和类似设备所需的那样。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.linuxforum.com/threads/partition-backup.3638/
|
||||
|
||||
作者:[Jarret][a]
|
||||
译者:[ictlyh](https://github.com/ictlyh)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://www.linuxforum.com/members/jarret.268/
|
Loading…
Reference in New Issue
Block a user