2015-02-21

How to clone machine with failing drive to another machine

A server's main disk is about to fail, you see a lot os I/O error in the /var/log/messages, it's not good.

Let's clone that server before it dies. I've choosen to clone it into a VM but the steps are exactly the same with another physical machine/disk

Create or use any existing linux machine, I'm using a toolbox VM running Ubuntu on a Xen hypervisor. Create a disk about the same size as the failing one and attach it to your toolbox machine.

If the old disk is OK, copy the whole disk using dd over the network (netcat or ssh), there is a dedicated tutorial for that here : http://matfrapp.blogspot.com/2015/01/how-to-clone-disk-using-dd-over-network.html

If the old disk is not good looking and you cannot read everything but the machine still works, here is the procedure :

- Backup the MBR ```dd if=/dev/sda of=/root/mbr bs=514 count=1```
Save the UUID of the partition on a notepad, you will need it:
ls -l /dev/disk/by-uuid

-Copy it on the new host using scp for example and restore it on the new drive
```scp failingmachine:/root/mbr /tmp/oldmbr
dd if=/tmp/oldmbr of=/dev/<freshlyattacheddrive> bs=514 count=1```

Reboot the toolbox machine so the new MBR can be read. Using fdik -l, format the partitions with the same filesystem than before.
```
mkfs.ext3 /dev/xvb2
mkfs.ext4 /dev/xvb3
mkswap /dev/xvdb4
```

Change the UUID of the freshly formated partitions to match the previous UUID
EG.  tune2fs /dev/xvdb3 -U 20e9a9d5-809c-4773-8d33-a0434c712345

Mount the partitions and Launch the copy
```
mount /dev/xvdb3 /mnt/newmachine
rsync -avP --numeric-ids --exclude='/dev' --exclude='/proc' --exclude='/sys' root@failingmachine:/ /mnt/newmachine
```
Remember to manually  create a /dev, /proc, /sys paritition otherwise the system won't boot.
```
mkdir /mnt/newmachine/proc
mkdir /mnt/newmachine/sys
mkdir /mnt/newmachine/dev
```

Delete the /mnt/newmachine/etc/udev/rules.d/70-persistent-net.rules file
edit the /mnt/newmachine/etc/network/interfaces file to match the new NIC names if the configuration if different.

That's it

No comments:

Post a Comment

Popular posts