====== Clone Linux Boot Device ======
We have had this problem a few times. A drive tells us it is thinking about dying, and we need to replace it, very fast.
===== Same Size Drive =====
If you have a drive that is the same size (exact, don't think two 2T drives are necessarily the same size) or larger, the simplest thing to do is plug them into a computer, then use the dd command. Assume /dev/sdf is the original drive, and /dev/sdg is the drive you want to clone it to. **Be sure you know which drive is which**.
lsblk # read the output of this and MAKE SURE you know which is the original and which is the replacement
fdisk -l /dev/sdf #now, use fdisk to MAKE SURE you know which is the original and which is the replacement
fdisk -l /dev/sdg #now, use fdisk to MAKE SURE you know which is the original and which is the replacement
# at this point, you should know for sure which is which. I hope
dd if=/dev/sdf of=/dev/sdg
The last command will take hours on a large drive. It takes, on my laptop, about 30 minutes on a 16G USB Thumbdrive. And, if it fails, you get to start over.
If you want feedback, at the cost of a little bit of speed, you can use pv to give you a thermometer bar. The 2T in the following command tells pv that we will be transferring 2 terabytes of data:
dd if=/dev/sdf | pv -petrs 2T | dd of=/dev/sdg
===== Target Drive Smaller =====
**This has not been tested. Do not use before we have a chance to test it**
# plug old drive in, see what the letter is (sda here)
# plug new drive in, see what the letter is (sdb here)
# copy partition
sfdisk -d /dev/sda | sfdisk /dev/sdb
# create a file system
mkfs.ext4 /dev/sdb1
# if you have a second partition, do the same for it
mkfs.ext4 /dev/sdb1
# mount sda1 on /mnt/sda, sdb1 on /mnt/sdb
mkdir /mnt/sda
mount /dev/sda1 /mnt/sda
mkdir /mnt/sdb
mount /dev/sdb1 /mnt/sdb
# copy all the files
cp -av /mnt/sda/* /mnt/sdb
# unmount everthing, remove devices
umount /dev/sda
umount /dev/sdb
# plug NEW drive back in and see what it's name is (sda here)
# then mount it
mount /dev/sda2 /mnt/sda
#IF you have a separate boot partition, mount it correctly
#mount /dev/sda1 /mnt/sda/boot
# now, we need to mount some stuff from the running system so we can
# do a chroot
mount --bind /dev /mnt/sda/dev
mount --bind /sts /mnt/sda/sys
mount --bind /proc /mnt/sda/proc
# put yourself in a chroot jail
chroot /mnt/sda
# verify fstab is correct, ie change required mount points
vi /etc/fstab # change any required mount points
#vi /etc/default/grub*WHATEVER* .... sorry - I don't remember these specifics either -- but you really need to point at the right boot device
# install grub on the thumbdrive
update-grub
# leave chroot jail
exit
# umount everything
umount /mnt/sda/proc
umount /mnt/sda/sys
umount /mnt/sda/dev
umount /mnt/sda/boot
umount /mnt/sda
# remove the drive and try it out.