Use the DD command in Linux to implement the ghost Function

Source: Internet
Author: User

Ghost and g4l

Installing the operating system is too slow and the entire process is too tedious.

During the installation process, you need to answer several questions. The system needs to install countless software and create and write countless files. Because it involves a large number of file location and read/write operations, the speed must be fast.

In Windows, we often use the ghost system to back up and burn the operating system. Ghost can clone the image of the entire system and restore it on a new computer, which is quite simple. Installing the operating system using the ghost system is much faster than installing the system using the installation CD, and you do not need to answer any questions.

So, can we use ghost to back up and restore the Linux system.

The answer is no. Ghost can only identify a few legacy Linux file systems, and cannot identify Boot Loader programs such as grub and Lilo.

In fact, Linux also has a ghost tool, the most famous g4l-ghostForLinux.

I used ghostforlinux. It is easy to copy and restore the entire disk. However, after trying for half a day, I cannot restore one or more partitions.

G4l, too weak to meet my requirements!

Magic fdisk and DD commands

In deep distress, what is the principle of the magic ghost? Isn't it Data Replication? The DD command in Linux is not the most powerful data replication tool!

In this case, why should I use complicated tools like g4l? Can a DD Command help me implement any complicated image copy and recovery requirements? Whether it is grub, ext4, btrfs, FAT32, NTFS... dd, all beings are equal.

Go to the Linux operating system, open the command line, and execute the following command:

Sudo fdisk-u-l

You can view the size and layout of all partitions on all disks.

-U. The unit of the numbers in start and end is 512 bytes, that is, the size of a sector.

Suppose I have A/dev/SDA disk with a size of GB. I have installed a Ubuntu operating system. The following partitions are used:

/Dev/sda1 5 GB

/Dev/sda2 1 GB extended partition

/Dev/sda5 1 GB extended partition

/Dev/sda2 is all the extended partitions, and its size overlaps with/dev/sda5.

/Dev/sda1 is an ext4 file system. Install the Ubuntu operating system.

/Dev/sda5 is a file system in SWAp format and serves as a swap partition.

If I use the mentally retarded g4l tool to create a system image, I need to back up the entire disk to 100 GB instead of 6 GB.

G4l can also be used to back up partitions separately. However, during restoration, grub needs to be installed on the target computer and properly partitioned. Very troublesome!

Here, I can use a DD command to generate a 6 GB image. Then, the complete system can be restored on any computer with a hard disk larger than 6 GB, including MBR and three partitions sda1, sda2, and sda5.

Procedure

Find a USB flash drive and install the ubuntulive CD system. [For details about how to create Ubuntu live CD, refer to the Ubuntu official website .]

Ubuntu live CD is similar to windowspe. It is a CD/U disk-guided Ubuntu operating system and can be directly used without installation.

Start the USB flash disk, enter the Ubuntu system on the disk, open the command line, and execute:

Sudo fdisk-u-l/dev/SDA

View the partition information of the hardware.

Then execute:

Dd BS = 512 COUNT = [fdiskThe maximum number of end in the Command + 1] If =/dev/SDA of =/ghost. img

In this way, you can copy all the required partition data to the ghost. imgfile. The image has been created!

Then, we can insert the USB flash drive to another system, start it with the USB flash drive, enter ubuntulivecd, open the command line, and execute the following command:

Dd If =/ghost. IMG of =/dev/SDA

After the installation is complete, unplug the USB flash drive and start the computer. We can see that our Linux system has been installed!

Note:

Do not run the DD command to generate an image of the local disk directly after starting the system with a local disk on the computer. Instead, use livecd to start the computer.

Therefore, a large number of write operations are performed on the system disk when the computer is running. An image generated directly for the running system disk may fail to be started when it is restored to another hard disk!



Applicable to non-Linux operating systems

Is it easy to back up and restore system images using the DD command in Linux?

For any windows system or even Mac system, you can use the DD command to back up and restore the system image.

Because the fdisk command in Linux can recognize the partition format in any system. Fdisk is not related to the file system on the partition, and does not even care about the file system. Fdisk can always report the sectors occupied by the partition.

The DD command does not care about the file system format of the disk. It simply copies the data in bytes from the specified location as required.

The DD command implements image backup and recovery, which is much simpler and more powerful than the Ghost Software. Using the ghost software, you still need to perform complex and dangerous disk partitioning operations.

The fdisk and DD commands are free of charge!

Compression and decompression may involve a large number of partitions to be backed up, and the image file generated using the DD command is also large. It is not convenient to store and transmit these images. We can also use the compression program to compress the generated image file. Here, I chose to use the gzip program and use it together with the DD command. Gzip parameter:-c Indicates output to stdout-d Indicates decompression-1 indicates the fastest compression-9 indicates the best compression level is-6 by default.

To use dd AND gzip to generate a compressed image file, run the following command:#Dd BS = 512
Count = [maximum end count in the fdisk command + 1] If =/dev/SDA | gzip-6>/ghost.img.gz

During restoration, You can execute the following commands:gzip -dc /ghost.img.gz.gz | dd of=/dev/sda

Reminder:If you restore the image to another computer, you may find that your network adapter is eth1, not eth0. This is because the/etc/udev/rules. d/70-persistent-net.rules file registers the NIC of the computer on which you make the image as eth0. If your network script processes eth0 but does not process eth1, you may not be able to access the Internet without modifying the network script. You may want to delete the/etc/udev/rules. d/70-persistent-net.rules file before creating an image. In this way, when you restore the image, the NIC name is eth0. It will not cause your computer to be unable to access the Internet after recovery.

By Shen Dongliang http://blog.csdn.net/shendl

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.