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
#creation:24/11/2017
#update:24/05/2018
$conf_targets=array(
        'crx-fw01-disk',
        'crx-web01-disk',
        'crx-mail01-disk',
        'crx-vmtemplate01-disk',
        'crx-dxspiderham-disk',
        'crx-dxspider11m-disk',
        'crx-sche02-disk',
        'crx-web02-disk',
        'crx-sup01-disk'
);

$conf_vgname='vg__data_store';
$conf_ssh_uri='ssh crx-bck01.ext.crx.cloud -p9822';
$conf_pathremote='/data_crxbck01/backup_lvm_vg__data_store_from_crx-hyp01-5';

function lvmGetAllLv(){
        $lvminf=array();
        $out=`/sbin/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)){
                        $opt_size=$lvinfo['size'].'g';
                        $opt_snap='snap';

                        $backup_file_name=$curr_date.'_'.$lvname.'_'.$opt_size.'.gz';

                        echo "\nBackup LV:$lvname";
                        echo "\nStart:".date('d-m-Y H:i:s')."\n";
                        $create_snapshot_cmd="/sbin/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="/bin/dd if=/dev/$conf_vgname/snap bs=1k | /bin/gzip | $conf_ssh_uri /bin/dd of=$conf_pathremote/$backup_file_name bs=1k";
                        echo $copy_snap_to_rmt."\n";
                        $exec=`$copy_snap_to_rmt`;
                        $remove_snapshot_cmd="/sbin/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

Démarrez une conversation

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *