Created
January 3, 2011 19:47
-
-
Save sampowers/763848 to your computer and use it in GitHub Desktop.
XenServer backup script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use POSIX qw(strftime); | |
use Data::Dumper; | |
sub timestamp { return strftime("%H:%M:%S", localtime(time)); } | |
sub datestamp { return strftime("%b %d", localtime(time)); } | |
sub l { | |
my ($msg) = shift; | |
printf("$0: %s %s $msg\n", datestamp(), timestamp()); | |
#printf("$msg\n"); | |
} | |
sub p { printf("%s", @_); } | |
sub vm_name { | |
$uuid = shift; | |
$cmd = "xe vm-list uuid='$uuid' params=name-label --minimal"; | |
chomp($vmname=`$cmd`); | |
return $vmname; | |
} | |
sub BackupVM { | |
my ($vmUUID, $backupdir) = @_; | |
$vmname = vm_name($vmUUID); | |
# todo: support snapshot with quiesce (sometimes this fails, fallback to normal) | |
chomp($snapshotUUID=`xe vm-snapshot uuid=$vmUUID new-name-label='backup_vm ($vmname)'`); | |
$status = `xe template-param-set is-a-template=false ha-always-run=false uuid=$snapshotUUID`; | |
$exportstring = sprintf("%s-%s.xva", | |
$backupdir.$vmname, | |
strftime("%Y%m%d",localtime(time))); | |
chomp($status=`xe vm-export vm=$snapshotUUID filename=$exportstring`); | |
chomp($status=`xe vm-uninstall uuid=$snapshotUUID force=true`); | |
} | |
sub find_backup_target { | |
my $target = shift; | |
# search SRs for one with our target name in custom field 'backup-opts' | |
$cmd = "xe sr-list " . | |
"other-config:XenCenter.CustomFields.backup-opts=$target --minimal"; | |
chomp($uuid=`$cmd`); | |
$uuid or return; | |
# get the name for that SR | |
$cmd = "xe sr-param-get uuid=$uuid param-name=name-label"; | |
chomp( $name = `$cmd` ); | |
$name or return; | |
# maybe check to see if it's accessible before returning it | |
return{ uuid => $uuid, | |
name => $name, | |
path => "/var/run/sr-mount/$uuid/", | |
targnum => $target, | |
status => "Online" | |
}; | |
} | |
############################################################################### | |
# grab vmlist {{ | |
chomp($vm_uuids = `xe vm-list is-control-domain=false params=uuid --minimal`); | |
@vmlist = split(/,/, $vm_uuids); | |
#@vmlist = ('f700ea60-db7a-80b4-c0e4-a7824da74c55','44505578-3cae-429b-68fe-9a3616b5eab2'); | |
#@vmlist = ('b5d7afd3-43f3-a180-9148-c460635f98c5', 'a7bf2f78-85d8-1feb-80bd-f24ae917cf76'); | |
#}} | |
l "scanning for backup targets... "; | |
for (1..10) { | |
if ($bt = find_backup_target("target$_")) { | |
push(@btargets, $bt); | |
} | |
} | |
p "found " . scalar(@btargets) . " targets: \n"; | |
for $t(@btargets) { | |
print " $t->{targnum}: $t->{name} ($t->{status})\n"; | |
} | |
l "backup target scan complete"; | |
for $bt(@btargets) { | |
for $vu (@vmlist) { | |
unless ($yestoall =~ /[Yy]/) { | |
print "Next VM: " . vm_name($vu) . "\n"; | |
printf "\nEnter Y to continue from this point, N to skip. [N/y] "; | |
chomp($result=<STDIN>); | |
} | |
if ( ($result =~ /[Yy]/) || ($yestoall =~ /[Yy]/) ) { | |
printf timestamp() . " beginning backup of vm '%s' to target '$bt->{name}'\n", vm_name($vu); | |
BackupVM($vu, $bt->{path}); | |
$yestoall = 'Y'; | |
} else { | |
printf "Skipping backup for %s\n", vm_name($vu); | |
next; | |
} | |
} | |
} | |
# }}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment