InvalidDiskFormat workaround, storage class
gitea/vn-vmware/pipeline/head There was a failure building this commit Details

This commit is contained in:
Juan Ferrer 2021-11-02 14:26:34 +01:00
parent a869b2550b
commit 65f734840e
3 changed files with 81 additions and 23 deletions

View File

@ -12,7 +12,13 @@ backup_datastore => 'backup',
# Directory where backups are stored # Directory where backups are stored
# If it's mounted via NFS you have to disable caching for the mount # If it's mounted via NFS you have to disable caching for the mount
local_backup_dir => '/mnt/backup', backup_dir => '/mnt/backup',
# The default storage class
storage_class => 'nearline',
# The archive storage class
archive_class => 'archive',
# Directory where backups are be restored # Directory where backups are be restored
restore_dir => '/mnt/backup', restore_dir => '/mnt/backup',
@ -34,7 +40,7 @@ rotations => {
lastMonth => { lastMonth => {
days => 31, days => 31,
count => 31, count => 31,
archive_regex => '^\d{4}-\d{2}-01' archive_regex => '^\d{4}-\d{2}-01',
# Function used to archive and exclude backups from rotation # Function used to archive and exclude backups from rotation
# https://perldoc.perl.org/Time/Piece.html # https://perldoc.perl.org/Time/Piece.html
@ -45,7 +51,8 @@ rotations => {
} }
}, },
lastWeek => { lastWeek => {
days => 7 days => 7,
storage_class => 'archive'
}, },
lastYear => { lastYear => {
days => 365 days => 365

2
debian/changelog vendored
View File

@ -1,4 +1,4 @@
vn-vmware (1.1.27) stable; urgency=low vn-vmware (1.1.28) stable; urgency=low
* Initial Release. * Initial Release.

View File

@ -86,6 +86,21 @@ my %opts = (
type => "=s", type => "=s",
help => "Regular expression used to archive and exclude backups from rotation" help => "Regular expression used to archive and exclude backups from rotation"
}, },
'backup-dir' => {
type => "=s",
help => "Local directory where backups are stored",
default => '.'
},
'storage-class' => {
type => "=s",
help => "The storage class",
default => 'default'
},
'archive-class' => {
type => "=s",
help => "The archive storage class",
default => 'archive'
},
'backup-file' => { 'backup-file' => {
type => "=s", type => "=s",
help => "The path to the backup file that will be restored" help => "The path to the backup file that will be restored"
@ -97,7 +112,7 @@ my %opts = (
}, },
'replicate-dir' => { 'replicate-dir' => {
type => "=s", type => "=s",
help => "Directory where local backup directory is replicated" help => "Directory where backup directory is replicated"
}, },
'dst-name' => { 'dst-name' => {
type => "=s", type => "=s",
@ -176,6 +191,9 @@ Opts::parse();
Opts::validate(); Opts::validate();
my @config_options = ( my @config_options = (
'backup-dir',
'storage-class',
'archive-class',
'passphrase-file', 'passphrase-file',
'restore-dir', 'restore-dir',
'replicate-dir' 'replicate-dir'
@ -198,6 +216,9 @@ my $passphrase_file = Opts::get_option('passphrase-file');
my $rotation_days = Opts::get_option('rotation-days'); my $rotation_days = Opts::get_option('rotation-days');
my $rotation_count = Opts::get_option('rotation-count'); my $rotation_count = Opts::get_option('rotation-count');
my $archive_regex = Opts::get_option('archive-regex'); my $archive_regex = Opts::get_option('archive-regex');
my $backup_dir = Opts::get_option('backup-dir');
my $storage_class = Opts::get_option('storage-class');
my $archive_class = Opts::get_option('archive-class');
my $backup_file = Opts::get_option('backup-file'); my $backup_file = Opts::get_option('backup-file');
my $restore_dir = Opts::get_option('restore-dir'); my $restore_dir = Opts::get_option('restore-dir');
my $replicate_dir = Opts::get_option('replicate-dir'); my $replicate_dir = Opts::get_option('replicate-dir');
@ -222,8 +243,7 @@ my $log_fh;
my $archive_fn; my $archive_fn;
my $backup_disks; my $backup_disks;
my $time_pattern = '%Y-%m-%d_%H-%M'; my $time_pattern = '%Y-%m-%d_%H-%M';
my $local_backup_dir = $config{local_backup_dir}; my $secure_file = "$backup_dir/.keepme";
my $secure_file = "$local_backup_dir/.keepme";
sub stringify_message { sub stringify_message {
my ($message) = @_; my ($message) = @_;
@ -366,8 +386,8 @@ sub backup_machine() {
my $time_mark = $time->strftime($time_pattern); my $time_mark = $time->strftime($time_pattern);
my $tmp_dir = ".$time_mark"; my $tmp_dir = ".$time_mark";
my $backup_datastore = $config{backup_datastore}; my $backup_datastore = $config{backup_datastore};
my $ds_tmp_dir = "[$backup_datastore] $vm_name/$tmp_dir"; my $ds_tmp_dir = "[$backup_datastore] $storage_class/$vm_name/$tmp_dir";
my $local_dir = "$local_backup_dir/$vm_name"; my $local_dir = "$backup_dir/$storage_class/$vm_name";
my $local_tmp_dir = "$local_dir/$tmp_dir"; my $local_tmp_dir = "$local_dir/$tmp_dir";
my $tar_file = "$local_dir/${vm_name}_$time_mark.tar.gz"; my $tar_file = "$local_dir/${vm_name}_$time_mark.tar.gz";
@ -511,6 +531,14 @@ sub backup_machine() {
# diskType => 'thin' # diskType => 'thin'
#); #);
# FIXME: Workaround for InvalidDiskFormat error
my $attempt = 0;
my $copied = false;
do {
$attempt++;
eval {
unless ($test) { unless ($test) {
$vdm->CopyVirtualDisk( $vdm->CopyVirtualDisk(
sourceName => $disk_path, sourceName => $disk_path,
@ -520,6 +548,20 @@ sub backup_machine() {
#destSpec => $disk_spec #destSpec => $disk_spec
); );
} }
$copied = true;
};
my $copy_err = $@;
if ($copy_err) {
if ($attempt >= 3) {
die $copy_err;
}
log_message $copy_err
log_message "Error while copying disk, sleeping some time and trying again.";
sleep(10);
}
} while (!$copied);
} }
log_message "Removing backup snapshot."; log_message "Removing backup snapshot.";
@ -636,6 +678,9 @@ sub backup_job() {
if (exists $rotation_cfg->{days}) { if (exists $rotation_cfg->{days}) {
$rotation_days = $rotation_cfg->{days}; $rotation_days = $rotation_cfg->{days};
} }
if (exists $rotation_cfg->{storage_class}) {
$storage_class = $rotation_cfg->{storage_class};
}
if (exists $rotation_cfg->{archive_regex}) { if (exists $rotation_cfg->{archive_regex}) {
$archive_regex = $rotation_cfg->{archive_regex}; $archive_regex = $rotation_cfg->{archive_regex};
$archive_regex = qr/$archive_regex/; $archive_regex = qr/$archive_regex/;
@ -675,7 +720,7 @@ sub rotate_backup() {
log_message "Rotating '$vm_name' backups."; log_message "Rotating '$vm_name' backups.";
my $local_dir = "$local_backup_dir/$vm_name"; my $local_dir = "$backup_dir/$storage_class/$vm_name";
my $regex = qr/^\Q$vm_name\E_(\d{4}-\d{2}-\d{2}_\d{2}-\d{2})/; my $regex = qr/^\Q$vm_name\E_(\d{4}-\d{2}-\d{2}_\d{2}-\d{2})/;
my $now = Time::Piece->new; my $now = Time::Piece->new;
@ -713,7 +758,8 @@ sub rotate_backup() {
} }
my $archive_count = scalar(@archive_files); my $archive_count = scalar(@archive_files);
my $archive_dir = "$local_dir/.archive"; my $archive_dir = "$backup_dir/$archive_class";
my $archive_vm_dir = "$archive_dir/$vm_name";
if ($archive_count > 0) { if ($archive_count > 0) {
log_message "Archiving $archive_count backups."; log_message "Archiving $archive_count backups.";
@ -724,11 +770,16 @@ sub rotate_backup() {
mkdir $archive_dir; mkdir $archive_dir;
} }
} }
unless (-e $archive_vm_dir) {
unless ($test) {
mkdir $archive_vm_dir;
}
}
foreach my $archive_file (@archive_files) { foreach my $archive_file (@archive_files) {
log_message "$archive_file"; log_message "$archive_file";
unless ($test) { unless ($test) {
move("$local_dir/$archive_file", "$archive_dir/$archive_file"); move("$local_dir/$archive_file", "$archive_vm_dir/$archive_file");
} }
} }
@ -817,7 +868,7 @@ sub replicate_backups() {
} }
my $rsync_params = '-rltmD --delete-after --include="*.tar.gz" --include="*.tar.gz.gpg" --include="*/" --exclude="*"'; my $rsync_params = '-rltmD --delete-after --include="*.tar.gz" --include="*.tar.gz.gpg" --include="*/" --exclude="*"';
my $rsync_command = "rsync $rsync_params \"$local_backup_dir/\" \"$replicate_dir\""; my $rsync_command = "rsync $rsync_params \"$backup_dir/\" \"$replicate_dir\"";
log_message $rsync_command; log_message $rsync_command;
unless ($test) { unless ($test) {
@ -831,7 +882,7 @@ sub replicate_backups() {
} }
sub init_backup_dir() { sub init_backup_dir() {
log_message "Initializing backup directory '$local_backup_dir'."; log_message "Initializing backup directory '$backup_dir'.";
if (-e $secure_file) { if (-e $secure_file) {
die "Backup directory already initialized."; die "Backup directory already initialized.";