Wednesday 9 April 2014

Copying a Debian Linux system to a new drive whilst it's running.

I know you can use dd for this, but a Debian Wheezy install on a USB stick of mine needed moving to another drive, and all I had available were the old and new sticks. Partition the new drive with a fresh bootable Linux partition (use fdisk rather than cfdisk, so you get some spare sectors for grub at the beginning of the disk), then execute the following commands (adjusting device names where necessary):
mkfs -t ext4 /dev/sdb1
mount /dev/sdb1 /mnt
init 1
cp -ax / /mnt
for i in /dev /dev/pts /proc /sys /run; do sudo mount -B $i /mnt$i; done
chroot /mnt
grub-install /dev/sdb
exit
export uuid=`tune2fs -l /dev/sda1 | grep UUID | awk '{ print $3 }'`
tune2fs -U "$uuid" /dev/sdb1
halt
Various one-liners borrowed from the web. Surprised no-one else has documented anything similar before.