Linux dd--Backup command

Source: Internet
Author: User

Linux Learning notes Backup command DD
Function: Copies the specified input file into the specified output file, and can be converted in format during the copy process. You can use this command to implement the diskcopy command under DOS. First use the DD command to write the data on the floppy disk to a storage file, and then the storage file written to the second floppy disk, complete the function of diskcopy. It should be noted that the storage file on the hard disk should be removed with the RM command. The system uses standard input files and standard output files by default.

Syntax: DD [Options]

if = input file (or device name).

of = output file (or device name).

IBS = Bytes Reads bytes bytes At a time, that is, the number of bytes read into the buffer.

Skip = Blocks skips the ibs*blocks block that reads into the beginning of the buffer.

Obs = bytes Writes bytes bytes At a time, that is, the number of bytes written to the buffer.

BS = bytes Sets the number of bytes in the read/write buffer (equal to the set IBS and OBS).

CBS = Byte converts bytes bytes at a time.

The count=blocks only copies the input blocks blocks.

CONV = ASCII Converts the EBCDIC code to Ascil code.

Conv = EBCDIC converts ascil code to EBCDIC code.

CONV = IBM converts ascil code to alternate EBCDIC code.

CONV = Block Converts the change bit to a fixed character.

CONV = Ublock Converts the fixed bit into a change bit.

CONV = UCase Converts the letter from lowercase to uppercase.

CONV = LCase converts letters from uppercase to lowercase.

CONV = Notrunc does not truncate the output file.

CONV = swab swaps each pair of input bytes.

CONV = NoError does not stop processing when an error occurs.

Conv = sync puts the size of each input record to the size of the IBS (filled with nul).

Conversion parameters:

The ASCII conversion EBCDIC is ASCII.

EBCDIC converts ASCII to EBCDIC.

IBM converts ASCII to alternate EBCDIC.

Block converts each row to a cbs-length record, and the insufficient portion is padded with spaces.

Unblock the length of each line is CBS, and the insufficient portion is padded with spaces.

LCase converts uppercase characters to lowercase characters.

UCase converts lowercase characters to uppercase characters.

Swab swap each byte of the input.

NoError does not stop when an error occurs.

NotruNC does not truncate the output file.

Sync fills each input block into IBS bytes, and the less part is padded with empty (NUL) characters.

2. Example Analysis

2.1. Data backup and Recovery

2.1.1 Full disk data backup and recovery
Backup:

DD IF=/DEV/HDX Of=/dev/hdy
Back up the local/DEV/HDX full disk to the/dev/hdy


DD IF=/DEV/HDX Of=/path/to/image
To back up the/DEV/HDX full data to the image file of the specified path


DD IF=/DEV/HDX | Gzip >/path/to/image.gz
Backup/DEV/HDX full data and compress with gzip tool to save to specified path


Recovery:
DD If=/path/to/image OF=/DEV/HDX
Restore the backup file to the specified disk


gzip-dc/path/to/image.gz | DD OF=/DEV/HDX
Restores a compressed backup file to a specified disk


2.1.2. Using netcat remote Backup


DD If=/dev/hda bs=16065b | Netcat < TARGETHOST-IP > 1234
Perform this command on the source host backup/dev/hda


Netcat-l-P 1234 | DD OF=/DEV/HDC bs=16065b
Execute this command on the destination host to receive data and write to/DEV/HDC


Netcat-l-P 1234 | bzip2 > Partition.img
Netcat-l-P 1234 | gzip > Partition.img
The above two instructions are the change of the destination host instruction using bzip2 gzip to compress the data and save the backup file in the current directory.

2.1.3. Backing up the MBR
Backup:

DD IF=/DEV/HDX of=/path/to/image count=1 bs=512
The backup disk starts with the 512Byte size of the MBR information to the specified file


Recovery:

DD If=/path/to/image OF=/DEV/HDX
Writes the backed up MBR information to the disk start section

2.1.4. Backing up floppy disks

DD if=/dev/fd0 of=disk.img count=1 bs=1440k
Backing up floppy data to the current directory's disk.img file

2.1.5. Copying memory data to the hard disk

DD If=/dev/mem Of=/root/mem.bin bs=1024
Copy the in-memory data to the Mem.bin file in the root directory

2.1.6. Copy ISO image from disc

DD If=/dev/cdrom Of=/root/cd.iso
Copy the disc data to the root folder and save it as a Cd.iso file

2.2. Increase the size of the swap partition file

DD If=/dev/zero of=/swapfile bs=1024 count=262144
Create a file that is large enough (256M here)


Mkswap /swapfile
Turn this file into a swap file.


swapon /swapfile
Enable this swap file


/swapfile swap swap defaults 0 0
Automatically load swap files on each boot, adding a line to the/etc/fstab file

2.3. Destroying disk data

DD If=/dev/urandom OF=/DEV/HDA1
The use of random data to populate the hard disk, in some necessary occasions can be used to destroy data. After you do this, the/dev/hda1 cannot be mounted, and the Create and copy operations cannot be performed.


2.4 Disk Management

2.4.1. Get the most appropriate block size

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
The best block size for the system can be determined by comparing the command execution time shown in the output of the DD instruction.


2.4.2 Test Drive Read and write speed

DD If=/root/1gb.file bs=64k | DD Of=/dev/null
DD If=/dev/zero of=/root/1gb.file bs=1024 count=1000000
The read/write speed of the test drive can be calculated by the execution time of the last two command outputs.


2.4.3. Repairing the hard drive

DD IF=/DEV/SDA OF=/DEV/SDA
Magnetic flux point is generated on the disk when the hard drive is not used for a long time (for example, 1, 2 years). When the heads read these areas, they encounter difficulties and can cause I/O errors. When this condition affects the first sector of the hard disk, it may cause the hard disk to retire. The above command may bring the data back to the dead. And the process is safe and efficient.

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.