From 0997e8cf80f81e7e55655d011e55a07873b75f90 Mon Sep 17 00:00:00 2001 From: Juan Date: Wed, 15 Aug 2018 21:35:13 +0200 Subject: [PATCH] Bug fixes --- config.pl | 4 ++-- debian/cron.d | 12 ++++++------ vn-vmware.pl | 40 +++++++++++++++++++++++++++++----------- 3 files changed, 37 insertions(+), 19 deletions(-) diff --git a/config.pl b/config.pl index 12520b4..6499fff 100644 --- a/config.pl +++ b/config.pl @@ -26,13 +26,13 @@ local_backup_dir => '/mnt/vm-backups', backup_jobs => { schedule1 => { machines => ['test'], - rotation => 'daily' + rotation => 'lastMonth' } }, # Backup rotation configuration rotations => { - daily => { + lastMonth => { days => 31, count => 31 } diff --git a/debian/cron.d b/debian/cron.d index 3a03b25..7d5361e 100755 --- a/debian/cron.d +++ b/debian/cron.d @@ -3,10 +3,10 @@ MAILFROM="bacula@verdnatura.es" MAILTO="sysadmin@verdnatura.es" -30 21 * * * root vn-vmware.pl --operation clone-job --job testDb -01 01 02 * * root vn-vmware.pl --operation clone-job --job monthlyDb +00 03 * * tue-fri root vn-vmware.pl --operation clone-job --job testDb +00 02 01 * * root vn-vmware.pl --operation clone-job --job monthlyDb -00 04 * * * root vn-vmware.pl --operation backup-job --job db -00 01 * * sat root vn-vmware.pl --operation backup-job --job friday -00 01 * * sun root vn-vmware.pl --operation backup-job --job saturday -00 01 * * mon root vn-vmware.pl --operation backup-job --job sunday +00 04 * * * root vn-vmware.pl --operation backup-job --job db +00 01 * * sat root vn-vmware.pl --operation backup-job --job friday +00 01 * * sun root vn-vmware.pl --operation backup-job --job saturday +00 01 * * mon root vn-vmware.pl --operation backup-job --job sunday diff --git a/vn-vmware.pl b/vn-vmware.pl index cf95fe7..51b75a8 100755 --- a/vn-vmware.pl +++ b/vn-vmware.pl @@ -42,8 +42,8 @@ my %opts = ( }, 'rotation-count' => { type => "=i", - help => "Number of backups to keep despite the rotation", - default => 5 + help => "Number of backups to keep despite the rotation days", + default => 0 }, 'dst-name' => { type => "=s", @@ -303,16 +303,29 @@ sub backup_job() { my $backup_job = $config{backup_jobs}{$job}; - my $rotation_cfg = $config{rotations}{$backup_job->{rotation}}; - $rotation_count = $rotation_cfg->{count}; - $rotation_days = $rotation_cfg->{days}; + if (exists $backup_job->{rotation}) { + my $rotation_name = $backup_job->{rotation}; + + unless (exists $config{rotations}{$rotation_name}) { + die "Rotation '$rotation_name' not defined."; + } + + my $rotation_cfg = $config{rotations}{$rotation_name}; + + if (exists $rotation_cfg->{count}) { + $rotation_count = $rotation_cfg->{count}; + } + if (exists $rotation_cfg->{days}) { + $rotation_days = $rotation_cfg->{days}; + } + } my @machines = @{$backup_job->{machines}}; foreach my $machine (@machines) { eval { $vm_name = $machine; - &open_machine(); + open_machine(); backup_machine(); rotate_backup(); }; @@ -422,12 +435,17 @@ sub backup_machine() { $vm->RemoveAllSnapshots(); log_message "Compressing to TAR file: $tar_file"; - system("tar -I pigz -cf $tar_file -C $local_tmp_dir ."); + my $tar_status = system("tar -I pigz -cf $tar_file -C $local_tmp_dir ."); + + unless ($tar_status == 0) { + die "An error occurred when trying to compress the file." + } }; my $err = $@; if ($err) { - log_error "An error ocurred during '$vm_name' backup, aborting." + log_message "An error ocurred during '$vm_name' backup, aborting."; + $vm->RemoveAllSnapshots(); } log_message "Removing temporary directory: $local_tmp_dir"; @@ -538,15 +556,15 @@ sub clone_machine { } my $relocate_spec; - my $mac_type = "Generated"; + my $mac_type = 'Generated'; my $host_view = $vm->summary->runtime->host; if (defined($mac)) { - $mac_type = "Manual" + $mac_type = 'Manual'; } if (defined ($dst_host)) { $host_view = Vim::find_entity_view( - view_type => "HostSystem", + view_type => 'HostSystem', filter => {'name' => $dst_host} ); }