Clone your computer through the network

Source: Internet
Author: User
Tags rsync

Source: www.linuxfocus.org

Abstract:

When you need to copy data from one computer to another, the network-based, secure, simple, and effective methods described in this article will come in handy.

Abstract:

Although the studies of cloned animals ("Dolly goat") and even human embryos are still controversial and risky, the knowledge of "clone" computers is not harmful (correct operation is the premise), but will improve your ability to write and configure scripts carefully. According to Moore's law and the rapid process of computer production, when using a computer, we are likely to encounter the need to "clone" our computer-whether it is replaced by a laptop or a faster machine. Instead, we need to copy all the file partitions on computer A to computer B and make it work properly. There are usually two methods: one is to open the chassis directly, and then replace the hard disk -- but opening the chassis usually means losing the warranty, and this is usually dangerous-an inexperienced user may cause mechanical or electronic damage to the hardware. The other method requires that both computers have NICs (even in home computers). This method is more secure as described below.

All the methods described below are built on the network connection, that is, the "Source" Computer (that is, the computer with data to be copied) there is a network connection with the target computer (that is, the computer on which data is needed. You can connect two NICs directly through a hub or through a special connection line (crossovercable) (Note: normal network cables do not work ). For the target computer, a Live-CD (such as Knoppix or LNX-BBC) or a minimal installation is required to ensure that the NIC can be operated and that ssh and/or netcat can be used. Even some systems on a floppy disk (just like I can work very well with tomsrtb), and if you want to install another brand new release version, this is a good choice. The IP addresses of the two computers must be in the same CIDR block so that they can "talk" to each other ",

Possible solutions:

There are several methods for copying through basic preparation:

* Copy through dd

* Tar/cpio Pipeline

* Rsync

* Dump and restore

If your two hard disks are not of the same model and size, the first method is not feasible or very complex (copying an iso image (dd if =/dev/cdrom of =. iso) or floppy disk through dd is good. Here (the diskcopy script) is a dd script diskcopy ). Another disadvantage of using dd is that the unused space will also be copied, which wastes a lot of unnecessary time. Pipeline via tar and cpio will take a long time (several hours), and there will be some restrictions on file name and symbolic connection, and will be blocked at/dev, and so on. Therefore, this method is not recommended. If the file system on your source and target computers is different, rsync (1) may be the best choice. This only requires ssh to run normally and the file transfer protocol is normal. It also provides the-D option for device files and many other options for various scenarios. This is a useful tool for daily backup, mirroring, or other tasks. There are many examples worth learning in its Manual. The example of rsync replication can be found in [1.

Here, we use dump and restore to re-allocate the entire file system. This is a fast, effective, and can be achieved through minimal effort-an ideal solution. I need to perform this operation twice because there are two target computers. The two target computers can start and work well, and it takes me about one hour to copy the GB Data. This method requires that the source computer and the target computer have the same file system. Here, we assume it is ext2 or ext3, because this is the two most widely used file systems () (see below ).

Configure ssh

Once the minimal installation system or Live-CD is configured, the next step is to configure ssh (if you do not use netcat to transfer files as described below ). This requires the source computer to run sshd (the secure shell daemon ). If you are not sure, check/etc/init. d /. Enter (root Account) on the target computer ):

Ssh-keygen-t rsa

For simplicity, do not enter a password. The Public Key is saved in the/root/. ssh/id_rsa.pub file. Copy this file to the source computer

Scp/root/. ssh/id_rsa SourcePC:/tmp

Here, SourcePC is the IP address of your source computer. When you are prompted whether you are sure, enter the complete "yes" (a separate "y" may not work ). On the source computer, you will also be asked to enter the root password. Now add the target computer to the trusted network node of your source computer.

Cat/tmp/id_rsa.pub>/root/. ssh/authorized_keys

Repeat the preceding Copy command to check whether the password has been successfully entered! Create a file system on the target computer.

Usually the first step is to partition your hard disk and then create an ext2/ext3 file system. For ext3, you need to add a-j (journalling) option to the mke2fs command (requires Kernel support for ext3 ). You can even convert the ext2 partition to ext3. For more information, see tune2fs (8 ). Assume that the source computer has the following partitions:

Filesystem Size Used Use % Mounted on

/Dev/hda3 2.7G 552 M 22%/

/Dev/hda5 7.8G

1.6G 22%/usr

/Dev/hda7 6.3G 1.7G 28%/usr/share

/Dev/hda8 3.4G 601 M 19%/home

/Dev/hda12 5.3G 1.9G 37%/opt

/Dev/hda1 587 M 70 M 13%

/Var/backup

I recommend that you do some partitioning. Otherwise, all your data will be completely destroyed if the file system is used incorrectly or the hard disk track is damaged. In addition, according to Murphy's Law, this usually happens when you directly use the entire hard disk instead of partitioning the hard disk. I have recently encountered such a situation. If no partition is prepared, all my data will be lost due to a problem with the primary partition. The above file indicates that the/usr partition is growing too much, so/usr/share must be added. It's time to change a larger hard disk. On the target computer, use parted or your favorite partition tool (Qtparted is a good graphic interface tool, said to be a clone of PartitionMagic ). The created partition cannot be smaller than the corresponding partition on the source computer. In addition, do not forget the swap partition. Save the partition table and create a file system on the created partition. You can use

Mke2fs-j-L/dev/xxx

Here xxx is your partition name, and then set the volume label. I often use a volume label such as "/usr. You can also set various tasks through tune2fs (8), such as periodic file system checks.

Transfer File System

First, you need to load all the newly created partitions. First, we start from the main File System ("/"), and the others are carried out in order. Of course, the two partitions on the source computer can be integrated into one partition on the target computer. In fact, this is exactly what we need to do -- merge/usr/and/usr/share in the above example into a partition. We will load the future main file system:

Mount/dev/xxx/mnt
During replication, it is necessary to go to the target directory.

Cd/mnt

Type

Ssh targetPC dump-0-f-/| restore-r-f-

TargetPC is the IP address of your target computer. The "-0" parameter indicates full backup, and "-f-" indicates using stdin/stdout as the file descriptor, "-r" indicates that the restore will re-create a file system transmitted over the network to the target computer. For more information, see dump (8) and restore (8 ). What you see below is the output of the main file system.

$ Ssh 10.42.3.42 dump-0-f-/| restore-r-f-

DUMP: Date of this level 0 dump: Tue Feb 22 15:50:12 2005

DUMP: Dumping/dev/hda3 (/) to standard output

DUMP: Label: debian

DUMP: Writing 10 Kilobyte records

DUMP: mapping (Pass I) [regular files]

DUMP: mapping (Pass II) [directories]

DUMP: estimated 547312 blocks.

DUMP: Volume 1 started with block 1 at: Tue Feb 22 15:50:14 2005

DUMP: dumping (Pass III) [directories]

DUMP: dumping (Pass IV) [regular files]

DUMP: Volume 1 completed at: Tue Feb 22 15:51:43 2005

DUMP: Volume 1 546590 blocks (533.78 MB)

DUMP: Volume 1 took 0:01:29

DUMP: Volume 1 transfer rate: 6141 kB/s

DUMP: 546590 blocks (533.78 MB)

DUMP: finished in 89 seconds, throughput 6141 kBytes/sec

DUMP: Date of this level 0 dump: Tue Feb 22 15:50:12 2005

DUMP: Date this dump completed: Tue Feb 22 15:51:43 2005

DUMP: Average transfer rate: 6141 kB/s

DUMP: DUMP IS DONE

Restore usually creates a file named restoresymtable. If you are sure there is no error during File System Reconstruction, you can delete the file. Complete the replication of the primary file system. Next we will complete the replication of other sub-partitions in sequence. Start with/usr (assuming that your current working directory is the future main file system ).

Mount/dev/xxx./usr

Cd./usr

Ssh targetPC dump-0-f-/usr | restore-r-f-

This mount-cd-dump/restore loop can now repeat all your directories. The processing of/usr/share (an independent partition on the source computer) mentioned above can be simply switched. /usr/share (pay attention to this ". "), and then simply repeat

Ssh targetPC dump-0-f-/usr/share | restore-r-f-

When the target file system has the file you want to restore, the Restore will report an error. It may take about one hour and 100 MB to copy data from a computer over ssh (or a special data cable, crossover cable ).

Note: When dumping a file system, you do not need to load it. You can just specify the partition name, such as/dev/hda6, rather than the name of the loaded directory.

Another option is netcat.

Another method is to use netcat (1) instead of ssh. netcat is short for nc. netcat is a very easy-to-use "Swiss Army Knife" based on TCP/IP protocol (C/S model). It allows us to create pipelines over the network (pipe ). In the above example, you only need to change something as below. We assume that the partitions loaded on/var/backup are transmitted through dump/restore. On the receiving end (target computer), create a netcat listening routine (-l), which outputs the pipeline to the restore.

Nc-l-p 2000-q 1 | restore-r-f-

Create another netcat routine on the source computer, which sends the input it obtained from the pipeline to the target computer. Here the target-IP is

Related Article

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.