Focus On Oracle

Installing, Backup & Recovery, Performance Tuning,
Troubleshooting, Upgrading, Patching

Oracle Engineered System


当前位置: 首页 » 技术文章 » Oracle

关于dd你需要知道的

dd(data description)是Unix操作系统系统上命令行工具,最早出现在Verion 5 Unix上,起初的意图是在ASCII和EBCDIC之间做转化,后来也用于转化或复制文件。后来又陆续出现了其他类似的工具,比如dcfldd ,dd3dd,ddrescure(这几个都是开源的,感兴趣的可以从SourceForge看看)等。在Windows上也可以使用dd工具,详情可参考http://www.chrysocome.net/dd,这个是用delphi写的。dcfldd 也可以在windows上执行,可以从SourceForge.net下载。

DD in Wikipedia

dd is a command-line utility for Unix and Unix-like operating systems whose primary purpose is to convert and copy files.[1]

On Unix, device drivers for hardware (such as hard disk drives) and special device files (such as /dev/zero and /dev/random) appear in the file system just like normal files; ddcan also read and/or write from/to these files, provided that function is implemented in their respective driver. As a result, dd can be used for tasks such as backing up the boot sector of a hard drive, and obtaining a fixed amount of random data. The dd program can also perform conversions on the data as it is copied, including byte order swapping and conversion to and from the ASCII and EBCDIC text encodings.[2]

The name dd is an allusion to the DD statement found in IBM's Job Control Language (JCL),[3][4] in which the initials stand for "Data Description".[5] The command's syntax resembles the JCL statement more than it does other Unix commands, so the syntax may have been a joke.[3]

Originally intended to convert between ASCII and EBCDIC, dd first appeared in Version 5 Unix.[6] The dd command is specified by IEEE Std 1003.1-2008, which is part of theSingle UNIX Specification.


DD help

[root@db1 ~]# dd --help
Usage: dd [OPERAND]...
  or:  dd OPTION
Copy a file, converting and formatting according to the operands.
  bs=BYTES        read and write BYTES bytes at a time (also see ibs=,obs=)
  设置读入/输出的块大小为bytes个字节
  ibs=BYTES       read BYTES bytes at a time (default: 512)
  一次读入bytes个字节,即指定一个块大小为bytes个字节

  obs=BYTES       write BYTES bytes at a time (default: 512)
  一次输出bytes个字节,即指定一个块大小为bytes个字节

  [root@db1 cdb1]# dd if=big_ohs.dbf ibs=1024 obs=4096 of=big_robin
  8+0 records in
  2+0 records out
  8192 bytes (8.2 kB) copied, 0.000521054 s, 15.7 MB/s
  [root@db1 cdb1]  
  if=FILE         read from FILE instead of stdin
  输入文件名,默认是标准输入  
   [root@db1 cdb1]# dd of=block1 <<abc
   > abcedfg
   > quit
   > hello
   > abc
   0+1 records in
   0+1 records out
   19 bytes (19 B) copied, 0.000565558 s, 33.6 kB/s
   [root@db1 cdb1]# ls -l block1
   -rw-r--r-- 1 root root 19 Jun 15 11:29 block1
   [root@db1 cdb1]# cat block1
   abcedfg
   quit
   hello
   [root@db1 cdb1]#  
  of=FILE         write to FILE instead of stdout

  输出文件名,默认是标准输出

   [root@db1 ~]# cat ohsdba.log
   Hello,this is ohsdba
   welcome
   [root@db1 ~]# dd if=ohsdba.log
   Hello,this is ohsdba
   welcome
   0+1 records in
   0+1 records out
   29 bytes (29 B) copied, 0.000459775 s, 63.1 kB/s
   [root@db1 ~]#  
  cbs=BYTES       convert BYTES bytes at a time
  一次转化bytes个字节,也可以说是指定转换缓冲区大小
  count=BLOCKS    copy only BLOCKS input blocks
  要复制blocks的个块,块大小等于ibs指定的字节数
  skip=BLOCKS     skip BLOCKS ibs-sized blocks at start of input
  从输入文件头部开始跳过blocks个块
  seek=BLOCKS     skip BLOCKS obs-sized blocks at start of output  
  从输出文件头部开始跳过blocks个块
  iflag=FLAGS     read as per the comma separated symbol list
  oflag=FLAGS     write as per the comma separated symbol list
  status=WHICH    WHICH info to suppress outputting to stderr;
                  'noxfer' suppresses transfer stats, 'none' suppresses all

  conv=CONVS      convert the file as per the comma separated symbol list
                   
BLOCKS and BYTES may be followed by the following multiplicative suffixes:
c =1, w =2, b =512, kB =1000, K =1024, MB =1000*1000, M =1024*1024, xM =M
GB =1000*1000*1000, G =1024*1024*1024, and so on for T, P, E, Z, Y.
Each CONV symbol may be:
  ascii     from EBCDIC to ASCII
  转换EBCDIC为ASCII
  ebcdic    from ASCII to EBCDIC
  转换ASCII为EBCDIC
  ibm       from ASCII to alternate EBCDIC
  转换ASCII为Alternate EBCDIC
  block     pad newline-terminated records with spaces to cbs-size
  把每一行转换为长度为cbs,不足部分用空格填充
  unblock   replace trailing spaces in cbs-size records with newline
  使每一行的长度都为cbs,不足部分用空格填充
  lcase     change upper case to lower case
  把大写字符转换为小写字符
  nocreat   do not create the output file
  不创建输出文件,输出文件必须已存在

   [root@db1 cdb1]# dd if=big1.dbf of=abc conv=nocreat
   dd: opening `abc': No such file or directory
   [root@db1 cdb1]# touch abc
   [root@db1 cdb1]# dd if=big1.dbf of=abc conv=nocreat
   40976+0 records in
   40976+0 records out
   20979712 bytes (21 MB) copied, 0.223515 s, 93.9 MB/s
   [root@db1 cdb1]# ls -l abc
   -rw-r--r-- 1 root root 20979712 Jun 15 12:43 abc
   [root@db1 cdb1]#
  excl      fail if the output file already exists
  如果输出文件存在停止
  [root@db1 cdb1]# dd if=big1.dbf of=block1 conv=excl
  dd: opening `block1': File exists
  [root@db1 cdb1]#  
  notrunc   do not truncate the output file
  不截断输出文件,这个很重要,比如备份oracle数据库头部,恢复时这个一定要加上,要不把整个文件都毁了
  ucase     change lower case to upper case
  把小写字符转换为大写字符
  sparse    try to seek rather than write the output for NUL input blocks
  swab      swap every pair of input bytes
  交换输入的每对字节
  noerror   continue after read errors
  如果出错不停止
  sync      pad every input block with NULs to ibs-size; when used
            with block or unblock, pad with spaces rather than NULs        
  将每个输入块填充到ibs个字节,不足部分用空(NUL)字符补齐        
  fdatasync  physically write output file data before finishing
  相当于不经过缓存直接写数据
  fsync     likewise, but also write metadata

  和fdatasync类似,包含fdatasync功能再加上元数据

https://www.gnu.org/software/coreutils/manual/html_node/dd-invocation.html

  [root@db1 cdb1]# dd if=system01.dbf of=system01.dbf.bak oflag=direct
  1638416+0 records in
  1638416+0 records out
  838868992 bytes (839 MB) copied, 179.386 s, 4.7 MB/s
  [root@db1 cdb1]# dd if=system01.dbf of=system01.dbf.bak conv=fsync
  1638416+0 records in
  1638416+0 records out
  838868992 bytes (839 MB) copied, 6.07085 s, 138 MB/s
  [root@db1 cdb1]# dd if=system01.dbf of=system01.dbf.bak conv=fdatasync
  1638416+0 records in
  1638416+0 records out
  838868992 bytes (839 MB) copied, 6.55396 s, 128 MB/s
  [root@db1 cdb1]#  

Each FLAG symbol may be:
  append    append mode (makes sense only for output; conv=notrunc suggested)
  direct    use direct I/O for data
  directory  fail unless a directory
  dsync     use synchronized I/O for data
  sync      likewise, but also for metadata
  fullblock  accumulate full blocks of input (iflag only)
  nonblock  use non-blocking I/O
  noatime   do not update access time
  noctty    do not assign controlling terminal from file
  nofollow  do not follow symlinks
Sending a USR1 signal to a running `dd' process makes it
print I/O statistics to standard error and then resume copying.
  $ dd if=/dev/zero of=/dev/null& pid=$!
  $ kill -USR1 $pid; sleep 1; kill $pid
  18335302+0 records in
  18335302+0 records out
  9387674624 bytes (9.4 GB) copied, 34.6279 seconds, 271 MB/s
Options are:
      --help     display this help and exit
      --version  output version information and exit
Report dd bugs to bug-coreutils@gnu.org
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
For complete documentation, run: info coreutils 'dd invocation'
[root@db1 ~]#

dd能干啥?
dd可以备份、还原磁盘,复制文件,Linux上扩大交换分区,销毁数据等
备份、还原磁盘数据
   dd if=/dev/sda of=/dev/sdb
   dd if=/dev/sda of=/root/image
   dd if=/root/image of=/dev/sda
   dd if=/dev/sda|gzip > image.gz
   gzip -dc /root/image.gz | dd of=/dev/sda
   dd if=/dev/sda of=/root/image count=1 bs=512
   dd if=/root/image of=/dev/sda
   dd if=/dev/mem of=/root/mem bs=8192

在Linux上扩大交换分区

   dd if=/dev/zero of=/var/swapfile bs=1M count=102400
   mkswap /var/swapfile
   swapon /var/swapfile
   /var/swapfile swap swap default 0 0
销毁磁盘/文件数据
   dd if=/dev/zero of=/dev/sda3 bs=4k   
   dd if=/dev/urandom of=/dev/sda3 bs=4k
   [root@db1 ~]# cat a
   this is ohsdba
   [root@db1 ~]# dd if=/dev/urandom of=a count=1
   1+0 records in
   1+0 records out
   512 bytes (512 B) copied, 0.000669252 s, 765 kB/s
   [root@db1 ~]#
   [root@db1 ~]# strings -a a
   K7Mk
   @Ha\+
   [root@db1 ~]#
测试硬盘读写速度及最合适的块的大小
   dd if=/dev/zero bs=1M count=1000 of=/root/1G
   dd if=/root/1G bs=8k | dd of=/dev/null
   dd if=/dev/zero bs=1024 count=1000000 of=/root/1G
   dd if=/dev/zero bs=2048 count=500000 of=/root/1G
   dd if=/dev/zero bs=4096 count=250000 of=/root/1G
   dd if=/dev/zero bs=8192 count=125000 of=/root/1G
修复硬盘
   dd if=/dev/sda of=/dev/sda bs=4k
制作USB启动盘
   dd if=rhel6.4.iso of=/dev/sdb bs=1M
  
Reference   

ftp://alpha.gnu.org/gnu/coreutils/

http://forensicswiki.org/wiki/Dcfldd

https://sourceforge.net/projects/dcfldd/

http://www.chrysocome.net/dd

https://en.wikipedia.org/wiki/Dd_%28Unix%29
https://www.gnu.org/software/coreutils/manual/html_node/dd-invocation.html

https://sourceforge.net/projects/parallelhash/

https://sourceforge.net/projects/dc3dd/

http://pkgs.repoforge.org/



关键词:dd 

相关文章

如何使用dd备份数据文件头部和ASM磁盘头部
如何在小端服务器以正常格式查看Oracle数据文件
关于dd你需要知道的
Top