How to clone a running system to a new harddisk using rsync?
Have you ever wanted to shrink an EC2 volume? I tried several ways and this is the only one worked for me.
In my case, I wanted to shrink a 30GB volume down to 8GB.
- Make sure you have a backup first!
- Right-click on the instance that the 30GB volume is attached and Launch more like this
- Accept all default values. Just check the storage info and adjust the size according to your needs. I accepted the 8GB default volume.
- Wait for the new instance to boot.
- Connect to the new instance and apt-get update, apt-get dist-upgrade, apt-get autoremove.
- Shutdown the new instance: shutdown -P 0
- Detach the sda1 volume (8GB) from the new instance and
- Attach it to the old instance as sdf (if available. any would do) where the 30GB is attached.
- Connect to the old instance
- Mount the /dev/xvdf /mnt/newvol
- Run apt-get update, apt-get dist-upgrade, apt-get autoremove to update the instance before cloning it.
- Run sudo rsync -ahPHAXx –delete –exclude={/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/lost+found} / /mnt/newvol
- Wait until rsync is finished. Time depends on the size of your volume. In my case it took about 2 minutes.
- Run blkid to get the volume UUID. You will get something like this:
/dev/xvda: LABEL="cloudimg-rootfs" UUID="8b054954-c64f-40a6-bd94-27088e2a3c04" TYPE="ext4" PTTYPE="dos" /dev/xvdf1: LABEL="cloudimg-rootfs" UUID="0a76513a-37fc-43df-9833-34f8f9598ada" TYPE="ext4"
- Now lets fix the grub:
- Run nano /boot/grub/grub.cfg
- ctrl \
- Search (to replace): 8b054954-c64f-40a6-bd94-27088e2a3c04 (copy/paste the UUID of the old volume and press enter)
- Replace with: 8b054954-c64f-40a6-bd94-27088e2a3c04 (copy/paste the UUID of the new volume and press enter)
- Replace (A)ll instances (just press A)
- Save and exit.
- Shutdown: shutdown -P 0
- Detach both volumes from the instance.
- Reattach the new volume as /sda1
- Boot the instance.