Second, DD application example
1. back up the local /dev/hdb full disk to the /dev/hdd
#dd If=/dev/hdb OF=/DEV/HDD
2. back up the /dev/hdb full data to the image file of the specified path
#dd If=/dev/hdb Of=/root/image
3. Restore the backup file to the specified disk
#dd If=/root/image Of=/dev/hdb
4. back up the /dev/hdb full data and compress it with the gzip tool to save to the specified path
#dd If=/dev/hdb | gzip >/root/image.gz
5. Restore the compressed backup file to the specified disk
#gzip-dc/root/image.gz | DD Of=/dev/hdb
6. Backup and restore MBR
The backup disk starts with 512 byte-sized MBR information to the specified file:
#dd If=/dev/hda of=/root/image count=1 bs=512
Count=1 refers to copying only one block; bs=512 refers to a block size of 512 bytes.
Recovery:
#dd If=/root/image Of=/dev/had
Writes the backed up MBR information to the disk start section
7. Backup floppy disk
#dd if=/dev/fd0 of=disk.img count=1 bs=1440k ( block size 1.44M)
8. Copy the memory contents to the hard disk
#dd if=/dev/mem of=/root/mem.bin bs=1024 ( specify block size 1k)
9. Copy the contents of the disc to the specified folder and save it as a cd.iso file
#dd If=/dev/cdrom (HDC) Of=/root/cd.iso
Increase the swap partition file size
The first step: Create a file of size 256M:
#dd If=/dev/zero of=/swapfile bs=1024 count=262144
Step Two: Turn this file into a swap file:
#mkswap/swapfile
Step three: Enable this swap file:
#swapon/swapfile
Fourth step: Edit the/etc/fstab file to automatically load swap files at each boot:
/swapfile Swap swap default 0 0
destroy disk data
#dd If=/dev/urandom of=/dev/hda1
Note: Populating the hard disk with random data can be used to destroy the data in some necessary situations.
Test Drive Read and write speed
#dd If=/dev/zero bs=1024 count=1000000 of=/root/1gb.file
#dd If=/root/1gb.file bs=64k | DD Of=/dev/null
With the command execution time of the above two commands output, the read and write speed of the hard disk can be calculated.
determine the optimal block size for your hard drive:
#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
By comparing the execution time of the command shown in the above command output, you can determine the optimal block size for the system.
Repair the hard drive:
#dd IF=/DEV/SDA OF=/DEV/SDA or DD If=/dev/hda of=/dev/hda
When the hard disk is placed unused for a longer period of time (more than one year), magnetic flux point is generated on the disk, which can cause difficulties when the heads are read to these areas and may result in 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.
- remote backup with netcat
#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.
Change the value of byte I in a large video file to 0x41 (that is, the ASCII value of capital a)
echo A | DD of=bigfile seek= $i Bs=1 count=1 conv=notrunc
Iii. the difference between/dev/null and/dev/zero
/dev/null, nicknamed the Bottomless pit, you can output any data to it, it take all, and will not hold!
/dev/zero, is an input device that you can use to initialize files. The device provides an exhaustive 0 and can use any number you need-the device offers much more. He can be used to write a string 0 to a device or file.
/dev/null------It is an empty device, also known as a bit bucket (bits bucket). Any output written to it will be discarded. If you do not want the message to display or write to the file in standard output, you can redirect the message to the bin.
#if =/dev/zero of=./test.txt bs=1k count=1
#ls –l
Total 4
-rw-r--r--1 Oracle DBA 16:56 test.txt
#find/-name Access_log 2>/dev/null
3.1 using/dev/null
Think of/dev/null as a "black hole", which is equivalent to a write-only file, and all content written to it will be lost forever. and trying to read from it is nothing. However,/dev/null is very useful for command lines and scripts.
L prohibit standard output
#cat $filename >/dev/null
The contents of the file are lost and not output to standard output.
L Prohibit standard error
#rm $badname 2>/dev/null
This error message [standard error] was thrown into the Pacific Ocean.
L suppress output of standard output and standard error
#cat $filename 2>/dev/null >/dev/null
If the "$filename" does not exist, there will be no error message, and if "$filename" exists, the contents of the file will not be printed to standard output.
Therefore, the above code does not output any information at all. This is useful when you want to test only the exit code of a command without any output.
#cat $filename &>/dev/null
This can actually be, by Baris Cicek pointed out
Automatically empty the contents of a log file
L Deleting contents of a file, but preserving the file itself, with all attendant permissions (from Example 2-1 and Example 2 -3):
#cat/dev/null >/var/log/messages
#: >/var/log/messages has the same effect, but does not produce new processes. (because: Built-in)
#cat/dev/null >/var/log/wtmp
Hide cookies and no longer use them
Ideal for dealing with these annoying "cookies" sent by commercial web sites
#if [-F ~/.netscape/cookies] # Delete if it exists.
#then
#rm-F ~/.netscape/cookies
#fi
#ln-S/dev/null ~/.netscape/cookies
All cookies are now dropped into the black hole and will not be stored on the disk.
3.2 using/dev/zero
Like/dev/null,/dev/zero is also a pseudo-file, but it actually produces a continuous stream of NULL (binary 0 streams, not ASCII). The output written to it is lost, and it is difficult to read a series of NULL from/dev/zero, although this can be done through OD or a hex editor. The main use of/dev/zero is to create an empty file with a specified length for initialization, just like a temporary swap file.
with /dev/zero Create a swap temporary file
#!/bin/bash
# Create a swap file.
root_uid=0 # ROOT the user's $UID is 0.
e_wrong_user=65 # not root?
File=/swap
blocksize=1024
Minblocks=40
Success=0
# This script must be run with root.
If ["$UID"-ne "$ROOT _uid"]
Then
Echo echo "You must is root to run this script."; Echo
Exit $E _wrong_user
Fi
blocks=${1:-$MINBLOCKS} # if the command line is not specified,
#+ is set to the default of 40 blocks.
# the above sentence is equivalent to:
# --------------------------------------------------
# if [-N "$"]
# Then
# blocks=$1
# Else
# blocks= $MINBLOCKS
# fi
# --------------------------------------------------
If ["$blocks"-lt $MINBLOCKS]
Then
blocks= $MINBLOCKS # a minimum of 40 blocks long.
Fi
echo "Creating swap file of size $blocks blocks (KB)."
DD If=/dev/zero of= $FILE bs= $BLOCKSIZE count= $blocks # writes zero to the file.
mkswap $FILE $blocks # This file is built as a swap file (or swap partition).
swapon $FILE # activates the swap file.
echo "Swap file created and activated."
Exit $SUCCESS
Another application for/dev/zero is to use 0 to populate a file of a specified size for a specific purpose, such as mounting a file system to a loopback device (loopback) or "safely" deleting a file
Example Create RAMDisk
#!/bin/bash
# ramdisk.sh
# "RAMDisk" is a section of system RAM memory,
#+ it can be manipulated as a file system.
# it has the advantage of very fast access (including Read and write).
# disadvantage: Volatile, loss of data when the computer restarts or shuts down.
#+ reduces the RAM available to the system.
# ten # So what does RAMDisk do ?
# save a larger set of data in RAMDisk, such as a table or dictionary,
#+ This speeds up the data query because finding in memory is much faster than finding it on disk.
e_non_root_user=70 # must be run with root.
Rootuser_name=root
Mountpt=/mnt/ramdisk
size=2000 # 2K blocks (can be modified appropriately)
blocksize=1024 # each block has a size of 1 K (in bytes)
Device=/dev/ram0 # First RAM device
Username= ' Id-nu '
If ["$username"! = "$ROOTUSER _name"]
Then
echo "must is root to run \" ' BaseName $ ' \ "."
Exit $E _non_root_user
Fi
if [!-D "$MOUNTPT"] # test whether the mount point already exists,
Then #+ If this script has been running several times, it won't be built again .
mkdir $MOUNTPT #+ because the front was already established.
Fi
DD If=/dev/zero of= $DEVICE count= $SIZE bs= $BLOCKSIZE
# fill the contents of the RAM device with 0.
# Why do you need to do this?
mke2fs $DEVICE # Create a ext2 file system on the RAM device.
Mount $DEVICE $MOUNTPT # mount the device.
chmod 777 $MOUNTPT # The RAMDisk can also be accessed by ordinary users.
# However, it can only be downloaded by root unloading.
echo "\" $MOUNTPT \ "now available for use."
# now RAMDisk can be used to access files even for ordinary users.
# Note that RAMDisk is volatile, so when the computer system restarts or shuts down, the contents of the RAMDisk disappear.
# Copy all of the files you want to save to a regular disk directory.
# after restarting, run this script to build up a ramdisk again.
# Reloading /mnt/ramdisk only and no other steps will not work correctly.
# If improved, this script can be placed in the/etc/rc.d/rc.local,
#+ so that a ramdisk can be set up automatically when the system starts.
# this is appropriate for the database server with high speed requirements.
Exit 0
Linux File System backup