Backup your Xen VM disk.

Hi!

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:

#!/usr/bin/php
<?php

#Backup LV over SSH
#(c) barbe.bastien@crx.cloud
#creation:24/11/2017
#update:26/04/2018

$conf_targets=array(
        'server-fw01-disk',
        'server-web01-disk'
);

$conf_vgname='vg__data_store';
$conf_ssh_uri='ssh myBackupServer -p8762';
$conf_pathremote='/data_crxbck01/backup_lvm_vg__data_store_from_crx-hyp01-2';


function lvmGetAllLv(){
        $lvminf=array();
        $out=`lvdisplay`;
        $out=explode("\n",$out);
        $s=0;
        foreach($out as $k=>$v){
                if (preg_match("/Logical(.*)volume(.*)/",$v,$m)) {
                        $s=1;
                }
                if($s){
                        if(preg_match("/LV(.*)Name(.*)/",$v,$o)){
                                $lvkey=trim($o[2]);
                        }
                        if (preg_match("/LV(.*)Size(.*)/",$v,$m)) {
                                #var_dump($m);
                                $size=$m[2];
                                if(preg_match("/MiB/",$size,$n)){
                                        $size=(int)$size;
                                        $size=$size/1000;
                                        $size=ceil($size);
                                }else{ //GiB value:
                                        $size=str_replace(',','.',$size);
                                        $size=(float)$size;
                                        $size=ceil($size);
                                }
                                $lvminf[$lvkey]['size']=$size;
                        }

                        if (preg_match("/VG(.*)Name(.*)/",$v,$m)) {
                                $lvminf[$lvkey]['vg_name']=trim($m[2]);
                        }
                }
        }
        return $lvminf;
}

$curr_date=date('d-m-Y');
$all_lv=lvmGetAllLv();
foreach($all_lv as $lvname=>$lvinfo){
                if(in_array($lvname,$conf_targets)){
                        $backup_file_name=$curr_date.'_'.$lvname.'.gz';
                        $opt_size=$lvinfo['size'].'g';
                        $opt_snap='snap';

                        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";
                        $exec=`$create_snapshot_cmd`;
                        $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";
                        $exec=`$copy_snap_to_rmt`;
                        $remove_snapshot_cmd="lvremove -f /dev/$conf_vgname/snap";
                        echo $remove_snapshot_cmd."\n";
                        $exec=`$remove_snapshot_cmd`;
                        echo "\nEnd:".date('d-m-Y H:i:s')."\n\n";
                }
}

?>
backup_lv_to_ssh.php

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.

A+

 

 

Article: 26042018