Home > System Tutorial > LINUX > body text

Linux dd command analysis: detailed examples of data backup and format conversion

王林
Release: 2024-01-05 15:46:12
forward
986 people have browsed it

dd command is mainly used for data backup and can perform format conversion during the backup process. In fact, the dd command can copy source data into target data, and data backup can be performed regardless of whether the source data is a file, partition, disk, or CD.

The basic format of the dd command is as follows:

[root@localhost ~]# dd if="输入文件" of="输出文件" bs="数据块" count="数量"
Copy after login

parameter:

  1. if: Define the file for input data, or it can be an input device;
  2. of: defines the file for output data, or it can be an output device;
  3. bs: Specify the size of the data block, that is, define how many bytes to read or write at one time. Mode data block size is 512 bytes;
  4. count: Specify the number of bs;
  5. conv=flags: Convert files based on flags. The signs include the following:
    • ascii: Convert from EBCDIC code to ASCII code;
    • ebcdic: Convert from ASCII code to EBCDIC code;
    • ibm: Convert from ASCII code to replaced EBCDIC code;
    • block: Replace the newline in the ending character block with a space of equal length;
    • unblock: Replace trailing spaces in cbs-sized blocks with a newline character;
    • lcase: Convert uppercase characters to lowercase;
    • notrunc: Do not truncate the output file;
    • ucase: Convert lowercase characters to uppercase;
    • swab: Swap each pair of input data bytes;
    • noerror: continue reading data after an error occurs;
    • sync: Fill each input data block with NUL characters to the size of ibs; when used with block or unblock, spaces will be used instead of NUL characters;

【Example 1】Backup file

[root@localhost ~]# dd if=/etc/httpd/conf/httpd.conf of=/tmp/httpd.bak
记录了67+1 的读入
#数据占了写满的67个数据块,以及1个没有写满的数据块
记录了67+1 的写出
#默认数据块大小是512字节
34439字节(34 kB)已复制,0.0524897 秒,656 kB/秒
#如果要备份文件,那么dd命令和cp命令非常类似
[root@localhost ~]# ll -h /tmp/httpd.bak
-rw-r--r--.1 root root 34K 6月 5 18:04 /tmp/httpd.bak
#查看一下生成的备份文件的大小
Copy after login

【Example 2】The backup partition is a backup file

[root@localhost ~]# df -h
文件系统 容量 已用 可用 已用%% 挂载点
/dev/sda3 20G 2.0G 17G 11% /
tmpfs 306M 0 306M 0% /dev/shm
/dev/sda1 194M 27M 157M 15% /boot
/dev/sr0 3.5G 3.5G 0 100% /mnt/cdrom
#查看一下分区容量,我们备份/boot分区
[root@localhost ~]# dd if=/dev/sda1 of=/tmp/boot.bak
#备份完成
[root@localhost ~]# ll -h /tmp/boot.bak
-rw-r--r--.1 root root 200M 6月 5 18:14 /tmp/boot.bak
#查看生成的备份文件
#如果需要恢复,则执行以下命令
[root@localhost ~]# dd if=/tmp/boot.bak of=/dev/sda1
Copy after login

If you want to back up a partition directly to another partition, you need to generate a new partition. The size of this partition cannot be smaller than the source partition, and can only be the same as or larger than the source partition. The command is as follows:

[root@localhost ~]# dd if=/dev/sda1 of=/dev/sdb1
#如果需要恢复,则只需把输入项和输出项反过来即可,命令如下
[root@localhost ~]# dd if=/dev/sdb1 of=/dev/sda1
Copy after login

【Example 3】Full disk backup

[root@localhost ~]# dd if=/dev/sda of=/dev/sdb
#把磁盘a备份到磁盘b
[root@localhost ~]# dd if=/dev/sda of=/tmp/disk.bak
#把磁盘a备份成文件disk.bak
#备份恢复
#如果要备份到另一块硬盘上,那么,当源硬盘数据损坏时,只需用备份硬盘替换源硬盘即可
#如果要备份成文件,那么在恢复时需要把备份数据复制到其他Linux中,然后把新硬盘安装到这台Linux
#服务器上,再把磁盘备份数据复制到新硬盘中。命令如下
[root@localhost ~]# dd if=/tmp/disk.bak of=/dev/sdb
Copy after login

【Example 4】Copy floppy disk

[root@localhost ~]# dd if=/dev/fd0 of=/tmp/fd.bak
#在Linux中软盘的设备文件名是/dev/fd0
#这条命令先把软盘中的数据保存为临时数据文件
[root@localhost ~]# dd if=/tmp/fd.bak of=/dev/fd0
#然后更换新的软盘,把数据备份复制到新软盘中,就实现了软盘的复制
Copy after login

If you need to back up a CD, then use the dd command to create an ISO image of the CD in Linux. The command is as follows:

#制作光盘ISO镜像
[root@localhost ~]# dd if=/dev/cdrom of-/tmp/cd.iso #把光盘中所有的数据制作成ISO镜像
[root@localhost ~J # mkdir /mnt/cd
#建立一个新的挂载点
[root@localhost ~]# mount -o loop /tmp/cd.iso /mnt/cd #挂栽ISO文件到挂载点
[root@localhost ~]# cd /mnt/cd
#进入挂栽点
[root@localhost cd]# ls
CentOS_BuildTag images RELEASE-NOTES-en-tJS.html RPM-GPG-KEY-CentOS-Debug-6 TRANS.TBL
EULAisolinux repodata RPM-GPG-KEY-CentOS-Security-6
GPL Packages RPM-GPG-KEY-CentOS-6 RPM-GPG-KEY-CentOS-Testing-6
#数据是光盘当中的数据,这个ISO镜像是可以被当作真正的光盘使用的
Copy after login

Sometimes we need to create a file of a specified size. For example, when adding a swap partition, we need to create a file of a specified size. In this case, we also use the dd command. The command is as follows:

[root@localhost ~]# dd if=/dev/zero of=/tmp/testfile bs=1M count=10
#数据输入项是/dev/zero会向目标文件中不停地写入二进制的0
#指定数据块大小是1MB
#指定生成10个数据块。也就是定义输出的文件大小为10MB
记录了10+0 的读入
#显示数据占满了10个数据块
记录了10+0 的写出
#不过这里数据块的大小已经是1MB了
10485760字节(10 MB)已复制,0.00709902 秒,1.5 GB/秒
[root@localhost ~]# ll -h /tmp/testfile
-rw-r--r--.1 root root 10M 6月 5 18:46 /tmp/testfile
#生成的testfile文件的大小刚好是10MB
Copy after login

The dd command is similar to the function of the GHOST tool when copying the entire disk. However, the hard disk data copied by the dd command is much more stable than the hard disk data copied by GHOST. Although the dd command is powerful, it also has an obvious disadvantage, which is that it takes a long time to copy. It takes 15 to 25 minutes to copy 100GB of data (depending on the performance of the server).

There are many methods and tools for data backup, such as tar and cpio commands. As for network replication tools, such as rsync and scp, they require relatively complete network knowledge to learn and will not be introduced here.

The above is the detailed content of Linux dd command analysis: detailed examples of data backup and format conversion. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:jb51.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!