歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Unix知識 >> Unix基礎知識

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 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=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

由上看出裸設備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,可以實際測試後確定。

Copyright © Linux教程網 All Rights Reserved