转自:[url]http://blog.chinaunix.net/u2/66233/showart_1098266.html[/url]
北亚中心()张宇略作修改。
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 size, superseding 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.
同时指定输入输出块大小,代替ibs和obs参数,块大小必须是 物理块的倍数。
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
逻辑卷raw1的lvcb信息
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
由上看出 裸设备raw的lvcb信息已经被破坏,在下次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,可以实际测试后确定。
阅读(1259) | 评论(0) | 转发(0) |