From 02ba5c0f1af62bab44a21847e89dc5a7e518ee9a Mon Sep 17 00:00:00 2001 From: Daniel Berteaud Date: Mon, 22 Oct 2018 18:31:03 +0200 Subject: [PATCH] Add an exclusive lock to be sure we correctly count the number of running backups This also ensure only one LVM snapshot is created at a time, which is a good thing to prevent overloading the system --- README | 5 +++-- virt-backup | 15 ++++++++++++++- virt-backup.spec | 1 + 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/README b/README index 6806719..5e3803a 100644 --- a/README +++ b/README @@ -47,9 +47,10 @@ Keep the lock file present after the dump virt-backup --dump --vm=devsrv --snapsize=10G --keep-lock -Backup devsrv, and disable LVM snapshots +Backup devsrv, and disable LVM snapshots. Also fails if +there's already 2 backups running -virt-backup --dump --vm=devsrv --no-snapshot +virt-backup --dump --max-backups=2 --vm=devsrv --no-snapshot Backup mail01, and enable debug (verbose output) diff --git a/virt-backup b/virt-backup index 22594ef..a94c0d2 100644 --- a/virt-backup +++ b/virt-backup @@ -28,6 +28,7 @@ use Sys::Virt; use Getopt::Long; use File::Copy; use File::Spec; +use LockFile::Simple; # Set umask umask(022); @@ -230,7 +231,16 @@ if (defined $connect[1]){ our $libvirt = $libvirt1; print "\n" if ($opts{debug}); - + +# Acquire an exclusive lock so we can ensure only one copy +# of virt-backup can run at a time +my $locker = LockFile::Simple->make( + -max => 20, + -delay => 1 +); +print "Trying to acquire an exclusive lock\n" if ($opts{debug}); +die "Can't acquire exclusive lock" unless $locker->lock($opts{lockdir} . '/virt-backup'); + foreach our $vm (@vms){ # Create a new object representing the VM print "Checking $vm status\n\n" if ($opts{debug}); @@ -280,6 +290,9 @@ foreach our $vm (@vms){ } } +print "Releasing exclusive lock\n" if ($opts{debug}); +$locker->unlock($opts{lockdir} . "/virt-backup"); +exit 0; ############################################################################ ############## FUNCTIONS #################### diff --git a/virt-backup.spec b/virt-backup.spec index e9ff7dd..b1eb3f9 100644 --- a/virt-backup.spec +++ b/virt-backup.spec @@ -15,6 +15,7 @@ Requires: util-linux Requires: lzop, bzip2, gzip, xz Requires: perl(Sys::Virt) => 0.2.3 Requires: perl(XML::Simple) +Requires: perl(LockFile::Simple) Requires: fuse-chunkfs AutoReq: no