File Replication tool dd

Source: Internet
Author: User
[Switch] use dd tool to back up data reposted from 9812658 final edit 9812658
I. Introduction to dd commands

Dd is a very useful command in Linux/Unix. It is used to copy an object with a specified size block and
At the same time, the specified Conversion

========================================================== ======================================

Ii. dd command usage

1. Syntax format

Dd [operand]
Dd [Option]

2. Option Parameters

Explanation of DD Command Options

If = File

Input File name. The default value is standard input.

Of = File

Output file name. The default value is standard output.

IBS = bytes

Read bytes at a time (that is, the size of a block is bytes)

Obs = bytes

Write bytes at a time (that is, the size of a block is bytes)

BS = bytes

Set the size of the read/write block to bytes, which can replace IBS and obs.

CBS = bytes

Bytes converted at a time, that is, the size of the conversion buffer.

Skip = Blocks

Skip blocks from the beginning of the input file and then start copying

Seek = Blocks

The blocks are skipped from the beginning of the output file and then copied. (Usually only valid when the output file is a disk or tape)

Count = Blocks

Copy only blocks. The block size is equal to the number of bytes specified by IBS.

Conv = Conversion [, conversion...]

Convert a file with the specified parameter

ASCII

Convert ebcdic to ASCII

Ebcdic

Convert ASCII to ebcdic

IBM

Convert ASCII to alternate ebcdic

Block

Converts each line to a record with a length of CBS. The missing part is filled with spaces.

Unblock

The length of each line is CBS, and the remaining part is filled with spaces.

Lcase

Converts uppercase to lowercase.

Ucase

Converts lowercase to uppercase.

Swab

Swap each pair of input bytes

Noerror

Do not stop when an error occurs

Notrunc

Output file not truncated

Sync

Fill each input block with IBS bytes. The missing part is filled with null (NUL) characters.

Note: If the following column ends with a specified number, multiply it by the corresponding number: B = 512, c = 1, K = 1024, W = 2, XM = Number m, KB = 1000, K = 1024, MB = 1000*1000, M = 1024*1024, GB = 1000*1000*1000, G = 1024*1024*1024

========================================================== ======================================

Iii. dd examples

1. Data backup and recovery

1) full disk data backup and recovery

(1) backup
# Back up local/dev/hdx to/dev/hdy
Dd If =/dev/hdx of =/dev/hdy
# Back up the full/dev/hdx data to the image file in the specified path
Dd If =/dev/hdx of =/path/to/Image
# Back up/dev/hdx full data, compress it with gzip, and save it to the specified path (the Bzip2 tool can also be used)
Dd If =/dev/hdx | gzip>/path/to/image.gz

(2) Recovery
# Restore the backup file to the specified Disk
Dd If =/path/to/image of =/dev/hdx
# Restore the compressed backup file to the specified Disk
Gzip-DC/path/to/image.gz | dd OF =/dev/hdx

Bytes ----------------------------------------------------------------------------------------------------------------------------

2) Remote Backup Using Netcat and DD commands

(1) backup
Assume that the IP address of the receiving end is 192.168.1.200, And the IP address of the sending end is 192.168.1.100.
Step 1: Execute the command on the receiving end: NC-l 1234 | dd OF =/dev/sdb1 or NC-l 1234 | gzip-DC | dd OF =/dev/sdb1
That is, the output of NC commands is a standard output. The output of NC commands is used as the input of DD commands in the pipeline, and data is written to/dev/sdb1.
(2) Recovery
Step 2: run the command on the sending terminal: dd If =/dev/hda1 | NC 192.168.1.200 1234 or dd If =/dev/hda1 | gzip | NC 192.168.1.200 1234, that is: the data on the/dev/hda1 device is used as the DD input, while the standard output is used as the output of the DD command. However, due to the pipeline, the standard output of the DD is used as the output of the NC command.

Bytes ----------------------------------------------------------------------------------------------------------------------------

3) Back up MBR

(1) backup
# Copy the MBR information starting from the backup disk to the specified file
Dd If =/dev/hdx of =/path/to/image COUNT = 1 bs = 512

(2) Recovery
# Write the backup MBR information to the start part of the Disk
Dd If =/path/to/image of =/dev/hdx

Bytes ----------------------------------------------------------------------------------------------------------------------------

4) backup floppy disk
# Back up the data in the drive to the disk. imgfile in the current directory
Dd If =/dev/fd0 of = disk. img count = 1 bs = 1440 K

Bytes ----------------------------------------------------------------------------------------------------------------------------

5) copy the memory data to the hard disk
# Copy the data in the memory to the mem. binfile In the Backup Directory
Dd If =/dev/MEM of =/backup/MEM. Bin BS = 1024

Bytes ----------------------------------------------------------------------------------------------------------------------------

6) Make the CD into an ISO Image File
# Copy the disc data to the backup folder, save it as a CD. ISO file, and then burn it
Dd If =/dev/CDROM of =/backup/CD. ISO
Cdrecord-v cd. ISO

Bytes ----------------------------------------------------------------------------------------------------------------------------

2. Increase the size of the SWAp partition File

Step 1: create a large enough file (250 MB)
Dd If =/dev/Zero of =/swapfile BS = 1024 COUNT = 262144
Step 2: change this file to a swap file
Mkswap/swapfile
Step 3: Enable this swap file
Swapon/swapfile
Step 4: automatically load the swap file every time you start the system. Add a line to the/etc/fstab file.
/Swapfile swap defaults 0 0
Note: To view the current swap usage, check the file/proc/SWAPs. Another method is to create a swap partition: fdisk command.

Bytes ----------------------------------------------------------------------------------------------------------------------------

3. Destroy Disk Data
# Use random data to fill the hard disk and destroy data as necessary. After this operation is performed, the/dev/hda1 cannot be mounted, and the creation and copy operations cannot be performed.
Dd If =/dev/urandom of =/dev/hda1

Bytes ----------------------------------------------------------------------------------------------------------------------------

4. disk management

(1) obtain the most appropriate block size
# Compare the command execution time displayed in the DD command output (select the least time) to determine the optimal Block Size of the system
Dd If =/dev/Zero BS = 1024 COUNT = 1000000 of =/root/1gb. File
Dd If =/dev/Zero BS = 2048 COUNT = 500000 of =/root/1gb. File
Dd If =/dev/Zero BS = 4096 COUNT = 250000 of =/root/1gb. File
Dd If =/dev/Zero BS = 8192 COUNT = 125000 of =/root/1gb. File

As you can see, it is best to use block size = 4096 for the/dev/SDB hard disk.

(2) test hard disk read/write speed
# Run the following two commands to calculate the read/write speed of the hard disk
Dd If =/root/1gb. File BS = 64 k | dd OF =/dev/null
Dd If =/dev/Zero of =/root/1gb. File BS = 1024 COUNT = 1000000

It can be seen that the write speed of the/dev/SDB hard disk is

It can be seen that the read speed of the/dev/SDB Hard Disk

(3) Hard Disk repair
# Magnetic flux point is generated on the disk when the hard disk is not used for a long time (for example, 1 or 2 years. When the head reads these areas, it may encounter difficulties and may cause I/O errors. When this affects the first sector of the hard disk, the hard disk may be decommissioned. The above command may bring these data back to life. This process is safe and efficient.
Dd If =/dev/SDA of =/dev/SDA

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.