Fix some spacing issue

tags/virt-backup-0.2.12-1 0.1.2
Daniel Berteaud 12 years ago
parent 5f4b54311b
commit 411fa045b9
  1. 71
      virt-backup

@ -31,7 +31,6 @@ use Getopt::Long;
umask(022); umask(022);
# Some constant # Some constant
our %opts = (); our %opts = ();
our @vms = (); our @vms = ();
our @excludes = (); our @excludes = ();
@ -156,19 +155,18 @@ if (($opts{state}) && ($opts{shutdown})){
print "State and shutdown flags cannot be used together\n"; print "State and shutdown flags cannot be used together\n";
exit 1; exit 1;
} }
# Backup dir needs to be created first
if (! -d $opts{backupdir} ){ if (! -d $opts{backupdir} ){
print "$opts{backupdir} is not a valid directory\n"; print "$opts{backupdir} is not a valid directory\n";
exit 1; exit 1;
} }
# Connect to libvirt # Connect to libvirt
print "\n\nConnecting to libvirt daemon using $opts{connect} as URI\n" if ($opts{debug}); print "\n\nConnecting to libvirt daemon using $opts{connect} as URI\n" if ($opts{debug});
our $libvirt = Sys::Virt->new( uri => $opts{connect} ) || our $libvirt = Sys::Virt->new( uri => $opts{connect} ) ||
die "Error connecting to libvirt on URI: $opts{connect}"; die "Error connecting to libvirt on URI: $opts{connect}";
print "\n" if ($opts{debug}); print "\n" if ($opts{debug});
foreach our $vm (@vms){ foreach our $vm (@vms){
@ -204,30 +202,31 @@ foreach our $vm (@vms){
} }
############################################################################ ############################################################################
############## FUNCTIONS #################### ############## FUNCTIONS ####################
############################################################################ ############################################################################
# Common routine before backup. Will save the XML description, try to
# create a snapshot of the disks etc...
sub prepare_backup{ sub prepare_backup{
# Create a new XML object # Create a new XML object
my $xml = new XML::Simple (); my $xml = new XML::Simple ();
my $data = $xml->XMLin( $dom->get_xml_description(), forcearray => ['disk'] ); my $data = $xml->XMLin( $dom->get_xml_description(), forcearray => ['disk'] );
# STop here if the lock file is present, another dump might be running # Stop here if the lock file is present, another dump might be running
die "Another backup is running\n" if ( -e "$backupdir.meta/$vm.lock" ); die "Another backup is running\n" if ( -e "$backupdir.meta/$vm.lock" );
# Lock VM: Create a lock file so only one dump process can run # Lock VM: Create a lock file so only one dump process can run
lock_vm(); lock_vm();
# Save the XML description # Save the XML description
save_xml(); save_xml();
# Save the VM state if it's running and --state is present # Save the VM state if it's running and --state is present
# (else, just suspend the VM) # (else, just suspend the VM)
$opts{wasrunning} = 0 unless ($dom->is_active()); $opts{wasrunning} = 0 unless ($dom->is_active());
if ($opts{wasrunning}){ if ($opts{wasrunning}){
if ($opts{state}){ if ($opts{state}){
save_vm_state(); save_vm_state();
@ -239,10 +238,9 @@ sub prepare_backup{
suspend_vm(); suspend_vm();
} }
} }
# Create a list of disks used by the VM # Create a list of disks used by the VM
foreach $disk (@{$data->{devices}->{disk}}){ foreach $disk (@{$data->{devices}->{disk}}){
my $source; my $source;
if ($disk->{type} eq 'block'){ if ($disk->{type} eq 'block'){
$source = $disk->{source}->{dev}; $source = $disk->{source}->{dev};
@ -253,25 +251,25 @@ sub prepare_backup{
else{ else{
print "\nSkiping $source for vm $vm as it's type is $disk->{type}: " . print "\nSkiping $source for vm $vm as it's type is $disk->{type}: " .
" and only block and file are supported\n" if ($opts{debug}); " and only block and file are supported\n" if ($opts{debug});
next; next;
} }
my $target = $disk->{target}->{dev}; my $target = $disk->{target}->{dev};
# Check if the current disk is not excluded # Check if the current disk is not excluded
if (grep { $_ eq "$target" } @excludes){ if (grep { $_ eq "$target" } @excludes){
print "\nSkiping $source for vm $vm as it's matching one of the excludes: " . print "\nSkiping $source for vm $vm as it's matching one of the excludes: " .
join(",",@excludes)."\n\n" if ($opts{debug}); join(",",@excludes)."\n\n" if ($opts{debug});
next; next;
} }
# If the device is a disk (and not a cdrom) and the source dev exists # If the device is a disk (and not a cdrom) and the source dev exists
if (($disk->{device} eq 'disk') && (-e $source)){ if (($disk->{device} eq 'disk') && (-e $source)){
print "\nAnalysing disk $source connected on $vm as $target\n\n" if ($opts{debug}); print "\nAnalysing disk $source connected on $vm as $target\n\n" if ($opts{debug});
# If it's a block device # If it's a block device
if ($disk->{type} eq 'block'){ if ($disk->{type} eq 'block'){
my $time = "_".time(); my $time = "_".time();
# Try to snapshot the source if snapshot is enabled # Try to snapshot the source if snapshot is enabled
if ( ($opts{snapshot}) && (create_snapshot($source,$time)) ){ if ( ($opts{snapshot}) && (create_snapshot($source,$time)) ){
@ -300,7 +298,7 @@ sub prepare_backup{
print "Adding $source to the list of disks to be backed up\n" if ($opts{debug}); print "Adding $source to the list of disks to be backed up\n" if ($opts{debug});
} }
} }
# Summarize the list of disk to be dumped # Summarize the list of disk to be dumped
if ($opts{debug}){ if ($opts{debug}){
if ($opts{action} eq 'dump'){ if ($opts{action} eq 'dump'){
@ -317,7 +315,7 @@ sub prepare_backup{
} }
} }
} }
# If livebackup is possible (every block devices can be snapshoted) # If livebackup is possible (every block devices can be snapshoted)
# We can restore the VM now, in order to minimize the downtime # We can restore the VM now, in order to minimize the downtime
if ($opts{livebackup}){ if ($opts{livebackup}){
@ -345,10 +343,10 @@ sub run_dump{
# Now, it's time to actually dump the disks # Now, it's time to actually dump the disks
foreach $disk (@disks){ foreach $disk (@disks){
my $source = $disk->{source}; my $source = $disk->{source};
my $dest = "$backupdir/$vm" . '_' . $disk->{target} . ".img$opts{compext}"; my $dest = "$backupdir/$vm" . '_' . $disk->{target} . ".img$opts{compext}";
print "\nStarting dump of $source to $dest\n\n" if ($opts{debug}); print "\nStarting dump of $source to $dest\n\n" if ($opts{debug});
my $ddcmd = "$opts{ionice} dd if=$source bs=$opts{blocksize} | $opts{nice} $opts{compcmd} > $dest 2>/dev/null"; my $ddcmd = "$opts{ionice} dd if=$source bs=$opts{blocksize} | $opts{nice} $opts{compcmd} > $dest 2>/dev/null";
unless( system("$ddcmd") == 0 ){ unless( system("$ddcmd") == 0 ){
@ -358,7 +356,7 @@ sub run_dump{
sleep(1); sleep(1);
destroy_snapshot($source) if ($disk->{type} eq 'snapshot'); destroy_snapshot($source) if ($disk->{type} eq 'snapshot');
} }
# If the VM was running before the dump, restore (or resume) it # If the VM was running before the dump, restore (or resume) it
if ($opts{wasrunning}){ if ($opts{wasrunning}){
if ($opts{state}){ if ($opts{state}){
@ -378,7 +376,7 @@ sub run_chunkmount{
# Now, lets mount guest images with chunkfs # Now, lets mount guest images with chunkfs
foreach $disk (@disks){ foreach $disk (@disks){
my $source = $disk->{source}; my $source = $disk->{source};
my $dest = "$backupdir/$vm" . '_' . $disk->{target}; my $dest = "$backupdir/$vm" . '_' . $disk->{target};
mkdir $dest || die $!; mkdir $dest || die $!;
@ -446,7 +444,7 @@ sub run_cleanup{
print "$cnt file(s) removed\n$snap LVM snapshots removed\n$meta metadata files removed\n\n" if $opts{debug}; print "$cnt file(s) removed\n$snap LVM snapshots removed\n$meta metadata files removed\n\n" if $opts{debug};
} }
# Print help
sub usage{ sub usage{
print "usage:\n$0 --action=[dump|cleanup|chunkmount|unlock] --vm=vm1[,vm2,vm3] [--debug] [--exclude=hda,hdb] [--compress] ". print "usage:\n$0 --action=[dump|cleanup|chunkmount|unlock] --vm=vm1[,vm2,vm3] [--debug] [--exclude=hda,hdb] [--compress] ".
"[--state] [--shutdown] [--shutdown-timeout] [--no-snapshot] [--snapsize=<size>] [--backupdir=/path/to/dir] [--connect=<URI>] ". "[--state] [--shutdown] [--shutdown-timeout] [--no-snapshot] [--snapsize=<size>] [--backupdir=/path/to/dir] [--connect=<URI>] ".
@ -492,7 +490,7 @@ sub usage{
"after that amount of time (in seconds), the backup will abort. The default timeout is 300 seconds\n\n" . "after that amount of time (in seconds), the backup will abort. The default timeout is 300 seconds\n\n" .
"\t--blocksize=<blocksize>: Specify block size in bytes (for dd and chunkfs). Default to 262144 (256kB).\n"; "\t--blocksize=<blocksize>: Specify block size in bytes (for dd and chunkfs). Default to 262144 (256kB).\n";
} }
# Save a running VM, if it's running # Save a running VM, if it's running
sub save_vm_state{ sub save_vm_state{
if ($dom->is_active()){ if ($dom->is_active()){
@ -504,7 +502,7 @@ sub save_vm_state{
print "$vm is not running, nothing to do\n" if ($opts{debug}); print "$vm is not running, nothing to do\n" if ($opts{debug});
} }
} }
# Restore the state of a VM # Restore the state of a VM
sub restore_vm{ sub restore_vm{
if (! $dom->is_active()){ if (! $dom->is_active()){
@ -529,7 +527,7 @@ sub restore_vm{
if ($opts{debug}); if ($opts{debug});
} }
} }
# Suspend a VM # Suspend a VM
sub suspend_vm(){ sub suspend_vm(){
if ($dom->is_active()){ if ($dom->is_active()){
@ -541,7 +539,7 @@ sub suspend_vm(){
print "$vm is not running, nothing to do\n" if ($opts{debug}); print "$vm is not running, nothing to do\n" if ($opts{debug});
} }
} }
# Resume a VM if it's paused # Resume a VM if it's paused
sub resume_vm(){ sub resume_vm(){
if ($dom->get_info->{state} == Sys::Virt::Domain::STATE_PAUSED){ if ($dom->get_info->{state} == Sys::Virt::Domain::STATE_PAUSED){
@ -554,7 +552,6 @@ sub resume_vm(){
} }
} }
# Shutdown a VM via ACPI # Shutdown a VM via ACPI
sub shutdown_vm(){ sub shutdown_vm(){
if ($dom->is_active()){ if ($dom->is_active()){
@ -585,7 +582,7 @@ sub start_vm(){
print "$vm is not in a shutdown state, nothing to do\n" if ($opts{debug}); print "$vm is not in a shutdown state, nothing to do\n" if ($opts{debug});
} }
} }
# Dump the domain description as XML # Dump the domain description as XML
sub save_xml{ sub save_xml{
print "\nSaving XML description for $vm to $backupdir/$vm.xml\n" if ($opts{debug}); print "\nSaving XML description for $vm to $backupdir/$vm.xml\n" if ($opts{debug});
@ -593,7 +590,7 @@ sub save_xml{
print XML $dom->get_xml_description(); print XML $dom->get_xml_description();
close XML; close XML;
} }
# Create an LVM snapshot # Create an LVM snapshot
# Pass the original logical volume and the suffix # Pass the original logical volume and the suffix
# to be added to the snapshot name as arguments # to be added to the snapshot name as arguments
@ -611,7 +608,7 @@ sub create_snapshot{
} }
return $ret; return $ret;
} }
# Remove an LVM snapshot # Remove an LVM snapshot
sub destroy_snapshot{ sub destroy_snapshot{
my $ret = 0; my $ret = 0;
@ -622,7 +619,7 @@ sub destroy_snapshot{
} }
return $ret; return $ret;
} }
# Lock a VM backup dir # Lock a VM backup dir
# Just creates an empty lock file # Just creates an empty lock file
sub lock_vm{ sub lock_vm{
@ -631,7 +628,7 @@ sub lock_vm{
print LOCK ""; print LOCK "";
close LOCK; close LOCK;
} }
# Unlock the VM backup dir # Unlock the VM backup dir
# Just removes the lock file # Just removes the lock file
sub unlock_vm{ sub unlock_vm{

Loading…
Cancel
Save