dd命令說明

pentium發表於2007-01-27

dd命令功能很強大的,對於一些比較底層的問題,使用dd命令往往可以得到出人意料的效果。用的比較多的還是用dd來備份裸裝置。但是不推薦,如果需要備份oracle裸裝置,可以使用rman備份,或使用第三方軟體備份,使用dd的話,管理起來不太方便。

轉:http://zhulch.itpub.net/post/17395/252366

[@more@]

建議 在有需要的時候使用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.

同時指定輸入輸出塊大小,代替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=16nobase=8n"toupper(substr($0,f,2))

}

}'|

bc 2>/dev/null`

/usr/bin/echo ""${a[0]}""${a[1]}""${a[2]}""${a[3]}""

${a[4]}""${a[5]}""${a[6]}""${a[7]}"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=16nobase=8n"toupper(substr($0,f,2))

}

}'|

bc 2>/dev/null`

/usr/bin/echo ""${a[0]}""${a[1]}""${a[2]}""${a[3]}""

${a[4]}""${a[5]}""${a[6]}""${a[7]}"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

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/14377/viewspace-894350/,如需轉載,請註明出處,否則將追究法律責任。

相關文章