skip to Main Content

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.

  1. Make sure you have a backup first!
  2. Right-click on the instance that the 30GB volume is attached and Launch more like this
  3. Accept all default values. Just check the storage info and adjust the size according to your needs. I accepted the 8GB default volume.
  4. Wait for the new instance to boot.
  5. Connect to the new instance and apt-get update, apt-get dist-upgrade, apt-get autoremove.
  6. Shutdown the new instance: shutdown -P 0
  7. Detach the sda1 volume (8GB) from the new instance and
  8. Attach it to the old instance as sdf (if available. any would do) where the 30GB is attached.
  9. Connect to the old instance
  10. Mount the /dev/xvdf /mnt/newvol
  11. Run apt-get update, apt-get dist-upgrade, apt-get autoremove to update the instance before cloning it.
  12. Run sudo rsync -ahPHAXx –delete –exclude={/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/lost+found} / /mnt/newvol
  13. Wait until rsync is finished. Time depends on the size of your volume. In my case it took about 2 minutes.
  14. 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"
  15. Now lets fix the grub:
  16. Run nano /boot/grub/grub.cfg
  17. ctrl \
  18. Search (to replace): 8b054954-c64f-40a6-bd94-27088e2a3c04 (copy/paste the UUID of the old volume and press enter)
  19. Replace with: 8b054954-c64f-40a6-bd94-27088e2a3c04 (copy/paste the UUID of the new volume and press enter)
  20. Replace (A)ll instances (just press A)
  21. Save and exit.
  22. Shutdown: shutdown -P 0
  23. Detach both volumes from the instance.
  24. Reattach the new volume as /sda1
  25. Boot the instance.

 

This Post Has 0 Comments

Leave a Reply

Back To Top