Backup your Xen VM disk.


Here i’m using a snapshot LVM method and compress over SSH on remote backup server.
With this method i’ve have disk image compress with gunzip, the disk image is ready for restauration
with simple dd pipe with gzip -d cmd.

I’ve develop a script for process automation here it’s:


#Backup LV over SSH


$conf_ssh_uri='ssh myBackupServer -p8762';

function lvmGetAllLv(){
        foreach($out as $k=>$v){
                if (preg_match("/Logical(.*)volume(.*)/",$v,$m)) {
                        if (preg_match("/LV(.*)Size(.*)/",$v,$m)) {
                                }else{ //GiB value:

                        if (preg_match("/VG(.*)Name(.*)/",$v,$m)) {
        return $lvminf;

foreach($all_lv as $lvname=>$lvinfo){

                        echo "\nBackup LV:$lvname";
                        echo "\nStart:".date('d-m-Y H:i:s')."\n";
                        $create_snapshot_cmd="lvcreate --size $opt_size --snapshot --name $opt_snap /dev/$conf_vgname/$lvname";
                        echo $create_snapshot_cmd."\n";
                        $copy_snap_to_rmt="dd if=/dev/$conf_vgname/snap bs=1k | gzip | $conf_ssh_uri dd of=$conf_pathremote/$backup_file_name bs=1k";
                        echo $copy_snap_to_rmt."\n";
                        $remove_snapshot_cmd="lvremove -f /dev/$conf_vgname/snap";
                        echo $remove_snapshot_cmd."\n";
                        echo "\nEnd:".date('d-m-Y H:i:s')."\n\n";


So i run it into a screen, and i’m using this on the remote to control in real time :

watch "du -sh *"
control files


To restore img simply, re create a LV with the correct size, and import IMG into it :

lvcreate -L 40G -n myserver-disk vg_target
dd if=/path/to/file/backup/myserver-disk.gz | gzip -d | dd of=/dev/vg_target/myserver-disk
restore img into lv process

Hope it’ll help you to do your backup and export vm disk.




Article: 26042018