最全面的关于LINUX与UNIX下的dd命令详解

简介:
dd 命令功能很强大的,对于一些比较底层的问题,使用 dd 命令往往可以得到出人意料的效果。用的比较多的还是用 dd 来备份裸设备。但是不推荐,如果需要备份 oracle 裸设备,可以使用 rman 备份,或使用第三方软件备份,使用 dd 的话,管理起来不太方便。
   建议 在有需要的时候使用dd 对物理磁盘操作,如果是文件系统的话还是使用tar backup cpio等其他命令更加方便。
  另外,使用dd对磁盘操作时,最好使用块设备文件。

Description



The dd command reads the InFile parameter or standard input, does the


specified conversions, then copies the converted data to the OutFile


parameter or standard output. The input and output block size can be


specified to take advantage of raw physical I/O. Note: The term Block


refers to the quantity of data read or written by the dd command in one


operation and is not necessarily the same size as a disk block.



if=file         输入文件名 标准输入确省
of=file        
输出文件名,标准输出确省
ibs=n        
输入块大小, n 字节(默认 512
obs=n        
输出块大小 ,n 字节(默认 512
bs=n        
同时设置输入输出块大小
cbs=n        
转换缓冲区大小
files=n        
在中断之前拷贝和转换 n 个输入文件
skip=n        
拷贝之前越过 n 个输入块
oseek=n        
拷贝之前从输出文件开始查找 n 个块
iseek        
拷贝之前从输入文件开始查找 n 个块
seek=n        
等同于 oseek
cono=ascic        
EBCDIC 码转换为 ASCII

ebcdic        ASCII->;EBCDIC
      ibm        ASCII
转换为 EBCDIC 码时轻微不同的映象

blolk         将新栈中断的 ASCII 码记录转换为固定长度

unblock         将固定长度的 ASCII 码记录转换为新行中断记录

lcase         变换字将至低等情况

ucase         变换字将至高等情况

swab         交换每对字节

noerrir         出错时不停止处理

sync         将每个块填充到 ibs


输入设备:infile参数指定或标准输入
实施指定的转换,然后复制转换数据到 outfile 参数指定 或标准输出。
输入块和输出块大小可以自己指定,但必须是磁盘物理块大小的倍数。



1
Usually, you need only write access to the output file. However,


when the output file is not on a direct-access device and you use


the seek flag, you also need read access to the file.


2
The dd command inserts new-line characters only when converting



with the conv=ascii or conv=unblock flags set; it pads only when


converting with the conv=ebcdic, conv=ibm, or conv=block flags


set.


3
Use the backup, tar, or cpio command instead of the dd command


whenever possible to copy files to tape. These commands are


designed for use with tape devices. For more information on using

tape devices, see the rmt special file.

4
The block size values specified with the bs, ibs and obs flags


must always be a multiple of the physical block size for the media


being used.


参数说明:
bs=BlockSize

Specifies both the input and output block sizesuperseding the


ibs and obs flags
. The block size values specified with the bs


flag must always be a multiple of the physical block size for the


media being used.

同时指定输入输出块大小,代替ibsobs参数,块大小必须是 物理块的倍数。


cbs=BlockSize

Specifies the conversion block size for variable-length to fixed-


length and fixed-length to variable-length conversions, such as


conv=block.

用于转换缓冲区大小

count=InputBlocks

Copies only the number of input blocks specified by the


InputBlocks variable.

只复制由count参数指定数量的输入块

conv= Conversion,....

Specifies one or more conversion options. Multiple conversions


should be separated by commas. The following list describes the


possible options:


ascii


Converts EBCDIC to ASCII. This option is incompatible with


the ebcdic, ibm, block, and unblock options.


block



Converts variable-length records to fixed-length. The


length is determined by the conversion block size (cbs).


This option is incompatible with the ascii, ebcdic, ibm,


and unblock options.


ebcdic


Converts ASCII to standard EBCDIC. This option is


incompatible with the ascii, ibm, block, and unblock


options.


ibm


Converts ASCII to an IBM version of EBCDIC. This option is


incompatible with the ascii, ebcdic, block, and unblock



options.


iblock, oblock


Minimize data loss resulting from a read or write error on



direct access devices. If you specify the iblock variable


and an error occurs during a block read (where the block


size is 512 or the size specified by the ibs=InputBlockSize


variable), the dd command attempts to reread the data block


in smaller size units. If the dd command can determine the


sector size of the input device, it reads the damaged block


one sector at a time. Otherwise, it reads it 512 bytes at a


time. The input block size ( ibs) must be a multiple of


this retry size. This option contains data loss associated


with a read error to a single sector. The oblock conversion



works similarly on output.


lcase


Makes all alphabetic characters lowercase.


noerror


Does not stop processing on an error.


notrunc


Does not truncate the output file. Instead, blocks not


explicitly written to output are preserved.


ucase


Makes all alphabetic characters uppercase.


swab


Swaps every pair of bytes.


sync



Pads every input block to the ibs value.


unblock


Converts fixed-length blocks to variable-length. The length


is determined by the conversion block size (cbs). This


option is incompatible with the ascii, ebcdic, ibm, and


block options.

files=InputFiles

Copies the number of files specified by the InputFiles variable


value of input files before ending (makes sense only where input



is a magnetic tape or similar device
).

复制由inputfile指定的文件,在中断之前,对磁带设备有意义


fskip=SkipEOFs


Skips past the number of end-of-file characters specified by the


SkipEOFs variable before starting to copy; this SkipEOFs variable


is useful for positioning on multifile magnetic tapes.

跳过磁带上的多个记录

ibs=InputBlockSize

Specifies the input-block size; the default is 512 bytes or one


block. The block-size values specified with the ibs flag must


always be a multiple of the physical block size for the media


being used.

bs参数,但如果bs参数指定,就不需要再使用ibs参数


if=InFile


Specifies the input file name; standard input is the default.



obs=OutputBlockSize


Specifies the output-block size; the default is 512 bytes or one


block. The block size values specified with the obs flag must


always be a multiple of the physical block size for the media


being used.

指定输出块大小


of=OutFile



Specifies the output file name; standard output is the default.



seek=RecordNumber


Seeks the record specified by the RecordNumber variable from the


beginning of output file before copying.

在复制到of文件之前先查找由recordnumber指定的记录,一个记录的大小由bs参数指定,
注意是在of 上查找



skip=SkipInputBlocks


Skips the specified SkipInputBlocks value of input blocks before


starting to copy.

在复制之前跳过skipinputblocks指定的块,块大小由bs参数指定。
注意是在if上跳过

span=yes|no

Allows spanning across devices if specified yes and works as


default if specified as no. See Spanning Across Devices, for more


information..


以下列几个例子:

标准输入到标准输出
F50:[/]#echo "123" | dd
123
0+1 records in.
0+1 records out.

先创建几个裸设备
F50:[/]#mkvg -y datavg hdisk1
Datavg
F50:[/]#mklv -y raw -t raw datavg 5
raw
F50:[/]#lsvg -l datavg
datavg:
LV NAME
TYPE
LPs
PPs
PVs
LV STATE
MOUNT POINT

raw
raw
5
5
1
closed/syncd
N/A

F50:[/]#mklv -y raw1 -t raw datavg 5
raw1
查看逻辑卷lvcb块信息,标准输入为逻辑卷raw,输出到标准输出
以下块大小为512字节,只复制1个块
F50:[/]#dd if=/dev/rraw count=1 | od -x
1+0 records in.
1+0 records out.
0000000 
4149 5820 4c56 4342 0000 7261 7700 0000

0000020
0000 0000 0000 0000 0000 0000 0000 0000

0000040
0000 0000 0000 0000 0000 3030 3061 6637

0000060
3064 3030 3030 3463 3030 3030 3030 3031

0000100
3061 0072 6177 0000 0000 0000 0000 0000

0000120
0000 0000 0000 0000 0000 0000 0000 0000

*
0000200
0000 0054 6875 2041 7072 2032 3720 3134

0000220
3a35 343a 3539 2032 3030 360a 0000 0000

0000240
0054 6875 2041 7072 2032 3720 3134 3a35

0000260
343a 3539 2032 3030 360a 0000 0000 0041

0000300
4637 3044 3443 3030 0079 6d6d 0079 0020

0000320
0005 0001 4e6f 6e65 0000 0000 0000 0000

0000340
0000 0000 0000 0000 0000 0000 0000 0000

*
0000720
0000 0000 0000 0000 0000 6461 3230 3066

0000740
3233 2e31 0000 0000 0000 0000 0000 0000

0000760
0000 0000 0000 0000 0000 0000 0000 0000

0001000
F50:[/]#dd if=/dev/raw bs=1 skip=80 count=8 |od –x
跳过80个字节且复制8个字节

0000000
0000

0000002
0000 0000 0000

0000010
8+0 records in.
8+0 records out.

使用dd在裸设备之间进行数据复制
F50:[/]#dd if=/dev/raw count=1
逻辑卷raw lvcb信息

AIX LVCBraw000af70d00004c000000010arawThu Apr 27 14:54:59 2006
Thu Apr 27 14:54:59 2006
AF70D4C00ymmy Noneda200f23.1
1+0 records in.
1+0 records out.

F50:[/]#dd if=/dev/raw1 count=1
逻辑卷raw1lvcb信息

AIX LVCBraw000af70d00004c000000010araw1Thu Apr 27 14:55:22 2006
Thu Apr 27 14:55:22 2006
AF70D4C00ymmy Noneda200f23.2
1+0 records in.
1+0 records out.

F50:[/]#dd if=/dev/raw1 skip=1 count=1
2个块没有信息

1+0 records in.
1+0 records out.

跳过raw1的第1个块,将raw的第lvcb信息复制到raw1的第2个块
F50:[/]#dd if=/dev/raw count=1 of=/dev/raw1 seek=1

1+0 records in.
1+0 records out.
F50:[/]#dd if=/dev/raw1 skip=1 count=1
AIX LVCBraw000af70d00004c000000010arawThu Apr 27 14:54:59 2006
Thu Apr 27 14:54:59 2006
AF70D4C00ymmy Noneda200f23.1
1+0 records in.
1+0 records out.
F50:[/]#dd if=/dev/raw1 count=1
AIX LVCBraw000af70d00004c000000010araw1Thu Apr 27 14:55:22 2006
Thu Apr 27 14:55:22 2006
AF70D4C00ymmy Noneda200f23.2
1+0 records in.
1+0 records out.


使用dd破坏lvcb信息,以此 验证dd 顺序读取磁盘
F50:[/]#more pvid.txt
#!/usr/bin/ksh
pvid=$1
disk=$2
set -A a `echo $pvid|\
awk ' {
for (f=1; f <= length($0); f=f+2) {
print "ibase=16\nobase=8\n"toupper(substr($0,f,2))
}
}'|\
bc 2>/dev/null`
/usr/bin/echo "\0"${a[0]}"\0"${a[1]}"\0"${a[2]}"\0"${a[3]}"\0"\
${a[4]}"\0"${a[5]}"\0"${a[6]}"\0"${a[7]}"\0\0\0\0\0\0\0\0\c"|\
dd bs=1 seek=128 of=/dev/$disk
F50:[/]#ls -al pvid.txt
-rw-r-----
1 root
system
341 Apr 21 18:07 pvid.txt

由于该文件只有341个字节,可以考虑大点的文件操作
F50:[/]#dd if=/pvid.txt bs=1024 of=/dev/raw 
0+1 records in.
0+1 records out.
F50:[/]#dd if=/dev/raw count=1
#!/usr/bin/ksh
pvid=$1
disk=$2
set -A a `echo $pvid|\
awk ' {
for (f=1; f <= length($0); f=f+2) {
print "ibase=16\nobase=8\n"toupper(substr($0,f,2))
}
}'|\
bc 2>/dev/null`
/usr/bin/echo "\0"${a[0]}"\0"${a[1]}"\0"${a[2]}"\0"${a[3]}"\0"\
${a[4]}"\0"${a[5]}"\0"${a[6]}"\0"${a[7]}"\0\0\0\0\0\0\0\0\c"|\
dd bs=1 seek=128 of=/dev/$diskda200f23.1
1+0 records in.
1+0 records out.
F50:[/]#lsvg -l datavg
datavg:
LV NAME
TYPE
LPs
PPs
PVs
LV STATE
MOUNT POINT

raw
5
5
1
closed/syncd
N/A

raw1
raw
5
5
1
closed/syncd
N/A

由上看出 裸设备rawlvcb信息已经被破坏,在下次importvg的时候将出现报警信息。
F50:[/]#importvg -y datavg hdisk1
0516-622 synclvodm: Warning, cannot write lv control block data.
Datavg
这在许多数据库中 经常出现这种情况,数据库会直接去读写裸设备的前512个字节,所以就容易导致出现以上的错误,但是并不影响数据库的运行。
可以再次使用dd 将其从raw1上的第2块上复制回来,
F50:[/]#dd if=/dev/raw1 skip=1 count=1
AIX LVCBraw000af70d00004c000000010arawThu Apr 27 14:54:59 2006
Thu Apr 27 14:54:59 2006
AF70D4C00ymmy Noneda200f23.1
1+0 records in.
1+0 records out.
F50:[/]#dd if=/dev/raw1 skip=1 count=1 of=/dev/raw
1+0 records in.
1+0 records out.
F50:[/]#varyoffvg datavg
F50:[/]#exportvg datavg
F50:[/]#importvg -y datavg hdisk1
Datavg
没有出现报警信息


dd备份裸设备到磁带
需要注意的几个方面:
1,
磁带的块大小
2,
恢复的时候注意不要把lvcb覆盖
偷点懒,直接复制ibm的··
1. 备份裸的逻辑卷的步骤:
(1)
设定磁带备份设备的合适的块大小:
使用下述命令查看当前的块大小:
# tctl -f /dev/rmt0 status
使用下述命令设置块大小:
# chdev -l rmt0 -a block_size=<
块的字节数 >
(2)
不经过软件压缩进行备份,使用下述命令:
# dd if=<
逻辑卷名 > of=/dev/rmt0 ibs=512 obs=< 块的字节数 > conv=sync
(3)
备份并进行软件压缩,使用下述命令:
# dd if=<
逻辑卷名 > bs=512 | compress | dd of=/dev/rmt0 ibs=512 obs=< 块的字节数 > conv=sync 
2. 恢复备份的裸逻辑卷数据的步骤:
(1)
在恢复备份的逻辑卷数据之前需要确定是否保留当前的逻辑卷控制块信息
(2)
若保留当前的逻辑卷控制块信息,使用下面的命令恢复数据:
a.
恢复未经过软件压缩的数据:
# dd if=/dev/rmt0 ibs=<
块的字节数 > obs=512 | dd of=/dev/< 逻辑卷名 > bs=512 skip=1 seek=1
b.
恢复经过软件压缩的数据:
# dd if=/dev/rmt0 ibs=<
块的字节数 > obs=512 | uncompress | dd of=/dev/< 逻辑卷名 > bs=512 skip=1 seek=1 
(3)
若覆盖当前的逻辑卷控制块信息,使用下面的命令恢复数据:
a.
恢复未经过软件压缩的数据:
# dd if=/dev/rmt0 of=/dev/<
逻辑卷名 > ibs=< 块的字节数 > obs=512
b.
恢复经过软件压缩的数据:
# dd if=/dev/rmt0 ibs=<
块的字节数 > obs=512 | uncompress | dd of=/dev/< 逻辑卷名 > bs=512
张宇加:
在对文件系统或物理存储设备进行dd时,bs的大小不可太小,通常建议4MB或1MB,可以实际测试后确定。










本文转自 张宇 51CTO博客,原文链接:http://blog.51cto.com/zhangyu/137069,如需转载请自行联系原作者
相关实践学习
基于Hologres轻松玩转一站式实时仓库
本场景介绍如何利用阿里云MaxCompute、实时计算Flink和交互式分析服务Hologres开发离线、实时数据融合分析的数据大屏应用。
阿里云实时数仓实战 - 项目介绍及架构设计
课程简介 1)学习搭建一个数据仓库的过程,理解数据在整个数仓架构的从采集、存储、计算、输出、展示的整个业务流程。 2)整个数仓体系完全搭建在阿里云架构上,理解并学会运用各个服务组件,了解各个组件之间如何配合联动。 3&nbsp;)前置知识要求 &nbsp; 课程大纲 第一章&nbsp;了解数据仓库概念 初步了解数据仓库是干什么的 第二章&nbsp;按照企业开发的标准去搭建一个数据仓库 数据仓库的需求是什么 架构 怎么选型怎么购买服务器 第三章&nbsp;数据生成模块 用户形成数据的一个准备 按照企业的标准,准备了十一张用户行为表 方便使用 第四章&nbsp;采集模块的搭建 购买阿里云服务器 安装 JDK 安装 Flume 第五章&nbsp;用户行为数据仓库 严格按照企业的标准开发 第六章&nbsp;搭建业务数仓理论基础和对表的分类同步 第七章&nbsp;业务数仓的搭建&nbsp; 业务行为数仓效果图&nbsp;&nbsp;
目录
相关文章
|
1月前
|
Oracle Ubuntu Unix
Unix与Linux区别
Unix: Unix是一个操作系统家族的名称,最早由贝尔实验室(Bell Labs)的肖像电机公司(AT&T)开发。最早的Unix版本是在1969年创建的。 Linux: Linux是由芬兰计算机科学家Linus Torvalds在1991年创建的。它是作为一个免费、开放源代码的Unix克隆而开始的。
19 1
|
11月前
|
存储 监控 网络协议
Linux与Unix常见配置命令
Linux与Unix常见配置命令
137 0
|
Unix Linux Shell
Linux 与 Unix 到底有啥区别和联系?
如果你是一名20多岁或30多岁的软件开发人员,那么你已成长在一个由Linux主导的世界中。数十年来,它一直是数据中心的重要参与者,尽管很难找到明确的操作系统市场份额的报告,但Linux在数据中心操作系统上的份额可能高达70%,而Windows变体几乎涵盖了所有剩余的比例。
|
Unix Linux iOS开发
Linux与Unix到底有什么不同
来自:开源中国 原文:Linux vs. Unix: What's the difference? 链接: https://opensource.com/article/18/5/differences-between-linux-and-unix 译者:Tocy, LinuxTech, 琪花亿草, lnovonl, 雪落无痕xdj, kevinlinkai, 无若 如果你是一名20多岁或30多岁的软件开发人员,那么你已成长在一个由Linux主导的世界中。
1167 0
|
Unix Linux 开发工具