search
HomeSystem TutorialLINUXLinux dd command analysis: detailed examples of data backup and format conversion

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="数量"

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
#查看一下生成的备份文件的大小

【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

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

【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

【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
#然后更换新的软盘,把数据备份复制到新软盘中,就实现了软盘的复制

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镜像是可以被当作真正的光盘使用的

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

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!

Statement
This article is reproduced at:脚本之家. If there is any infringement, please contact admin@php.cn delete
什么是linux设备节点什么是linux设备节点Apr 18, 2022 pm 08:10 PM

linux设备节点是应用程序和设备驱动程序沟通的一个桥梁;设备节点被创建在“/dev”,是连接内核与用户层的枢纽,相当于硬盘的inode一样的东西,记录了硬件设备的位置和信息。设备节点使用户可以与内核进行硬件的沟通,读写设备以及其他的操作。

Linux中open和fopen的区别有哪些Linux中open和fopen的区别有哪些Apr 29, 2022 pm 06:57 PM

区别:1、open是UNIX系统调用函数,而fopen是ANSIC标准中的C语言库函数;2、open的移植性没fopen好;3、fopen只能操纵普通正规文件,而open可以操作普通文件、网络套接字等;4、open无缓冲,fopen有缓冲。

linux中eof是什么linux中eof是什么May 07, 2022 pm 04:26 PM

在linux中,eof是自定义终止符,是“END Of File”的缩写;因为是自定义的终止符,所以eof就不是固定的,可以随意的设置别名,linux中按“ctrl+d”就代表eof,eof一般会配合cat命令用于多行文本输出,指文件末尾。

linux怎么判断pcre是否安装linux怎么判断pcre是否安装May 09, 2022 pm 04:14 PM

在linux中,可以利用“rpm -qa pcre”命令判断pcre是否安装;rpm命令专门用于管理各项套件,使用该命令后,若结果中出现pcre的版本信息,则表示pcre已经安装,若没有出现版本信息,则表示没有安装pcre。

linux中什么叫端口映射linux中什么叫端口映射May 09, 2022 pm 01:49 PM

端口映射又称端口转发,是指将外部主机的IP地址的端口映射到Intranet中的一台计算机,当用户访问外网IP的这个端口时,服务器自动将请求映射到对应局域网内部的机器上;可以通过使用动态或固定的公共网络IP路由ADSL宽带路由器来实现。

linux怎么查询mac地址linux怎么查询mac地址Apr 24, 2022 pm 08:01 PM

linux查询mac地址的方法:1、打开系统,在桌面中点击鼠标右键,选择“打开终端”;2、在终端中,执行“ifconfig”命令,查看输出结果,在输出信息第四行中紧跟“ether”单词后的字符串就是mac地址。

手机远程linux工具有哪些手机远程linux工具有哪些Apr 29, 2022 pm 05:30 PM

手机远程linux工具有:1、JuiceSSH,是一款功能强大的安卓SSH客户端应用,可直接对linux服务进行管理;2、Termius,可以利用手机来连接Linux服务器;3、Termux,一个强大的远程终端工具;4、向日葵远程控制等等。

linux中lsb是什么意思linux中lsb是什么意思May 07, 2022 pm 05:08 PM

linux中,lsb是linux标准基础的意思,是“Linux Standards Base”的缩写,是linux标准化领域中的标准;lsb制定了应用程序与运行环境之间的二进制接口,保证了linux发行版与linux应用程序之间的良好结合。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor