When a single hard disk got hardware fault, it is possible that you will have data loss. Nowaday low-end desktop hard disk is cheap and you can purchase it at around HK$400 (US$50) for a 1TB hard disk. So you may migrate your existing ext4 partitions on single hard disk to RAID1. The simply method is that we create a Ubuntu bootable USB stick.
After installed new hard disk with equal or larger disk capacity on your server, we boot the system with Ubuntu bootable USB stick. If the system cannot access internet, we should configure its IP address and gateway information with following commands.
$ ifconfig eth0 192.168.1.10/24 up
$ route add default gw 192.168.1.1
mdadm should be installed, it is a required software package to manage LVM RAID.
$ apt-get install mdadm
Old and new hard disk is installed as /dev/sda (old) and /dev/sdb (new) in our example, then new partitions for LVM RAID1 should be created on new hard disk.
$ fdisk /dev/sdb
Then, press ‘n’ for new partition. Size of new partition should be equal to or larger than original, and partition type should be ‘fd’ Linux raid autodetect.
When creation of new partitions are finished, we press ‘w’ to write changes on the disk,
Then, we create a LVM RAID1 with the following mdadm command. Since we got 1 hard disk only for RAID1 in this moment, so we use missing to represent another drive for RAID is missing.
$ mdadm –create /dev/md0 –verbose –level=1 –raid-devices=2 /dev/sdb1 missing
Next, we make a new ext4 filesystem on RAID1 partition.
$ mkfs.ext4 /dev/md0
After that, we mount and copy files from old partition (/dev/sda1 in this example) to new created filesystem with rsync command.
$ mkdir /aaa /bbb
$ mount /dev/sda1 /aaa
$ mount /dev/md0 /bbb
$ rsync -av /aaa/ /bbb
After completion of file copying, Filesystem UUID should be updated on new or existing filesystem table /etc/fstab, you may check the UUID with dumpe2fs command.
$ dumpe2fs -h /dev/md0 | grep UUID
dumpe2fs 1.42 (29-Nov-2011)
Filesystem UUID: d9a55f97-fafa-4740-a694-456c8514fa84
Then, we update as the following on /etc/fstab file.
UUID=d9a55f97-fafa-4740-a694-456c8514fa84 /home ext4 defaults,user_xattr 0 2
If the partition is new root filesystem, we should chroot into new root filesystem and install boot loader on new hard disk.
$ mount -o bind /dev /bbb/dev
$ mount -o bind /proc /bbb/proc
$ mount -o bind /sys /bbb/sys
$ chroot /bbb
$ mkinitrd /boot/initrd.img#ramdisk-image-filename# #kernel-version-here#
$ grub-install /dev/sdb$ exit
$ umount /bbb/dev /bbb/proc /bbb/sys
After completion, we un-mount all partitions for reboot.
$ umount /aaa /bbb