Jul 15, 2016

Backup linux virtual machine from cloud provider

Not every cloud provider provides downloadable disk backup for your virtual machines. For those who do not provide such a feature the following instruction may be useful.


In order to backup running linux you will need another running linux with ssh access to store image.

Learn about device you are about to backup:
$ sudo fdisk -l
Disk /dev/vda: 21.5 GB, 21476933632 bytes
255 heads, 63 sectors/track, 2611 cylinders, total 41947136 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

  Device Boot      Start         End      Blocks   Id  System
/dev/vda1               1    41947135    20973567+  ee  GPT

Dump device to another server
# The operation may take long time. It is recommended to run within “screen” (https://www.gnu.org/software/screen/manual/screen.html).
# (!!!) Replace /dev/vda1 and username@hostname with your values
sudo dd if=/dev/vda1 bs=4k conv=noerror,sync | bzip2 | ssh -c blowfish username@hostname 'dd of=/root/image.dd.bz2'

Uncompress the image
bunzip2 image.dd.bz2

Shirk image
sudo umount /mnt/image
resize2fs -f -M image.dd

Mount image and check its integrity
sudo mkdir -p /mnt/image
sudo mount -o loop,rw image.dd /mnt/image
df -h
ls /mnt/image
tail /mnt/image/var/log/syslog

Compress the image
mv image.dd image.small.dd
bzip2 image.small.dd

Use backup later
bunzip2 image.small.dd.bz2
sudo mkdir -p /mnt/image
sudo mount -o loop,rw image.small.dd /mnt/image
cd /mnt/image