diff --git a/debian/changelog b/debian/changelog index 7fa8bf9..c864526 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,4 +1,4 @@ -vn-vmware (1.0.32) stable; urgency=low +vn-vmware (1.0.33) stable; urgency=low * Initial Release. diff --git a/debian/logrotate.d b/debian/logrotate.d new file mode 100644 index 0000000..4291368 --- /dev/null +++ b/debian/logrotate.d @@ -0,0 +1,9 @@ +/var/log/vn-vmware.log { + create 0644 root root + compress + delaycompress + monthly + rotate 12 + notifempty + missingok +} diff --git a/vn-vmware.pl b/vn-vmware.pl index 023c0fd..fbd4947 100755 --- a/vn-vmware.pl +++ b/vn-vmware.pl @@ -170,8 +170,6 @@ my $snapshot_name = Opts::get_option('snapshot-name'); my $snapshot_desc = Opts::get_option('snapshot-desc'); my $vm; -my $remote_host; -my $vm_datastore; my $log_fh; my $time_pattern = '%Y-%m-%d_%H-%M'; my $local_backup_dir = $config{local_backup_dir}; @@ -388,7 +386,7 @@ sub backup_machine() { view_type => 'Datacenter', filter => {'name' => $config{datacenter}} ); - + log_message "Creating temporary backup directory: $ds_tmp_dir"; $file_manager->MakeDirectory( name => $ds_tmp_dir, @@ -406,54 +404,77 @@ sub backup_machine() { die "Local backup directory is not accessible: $local_tmp_dir"; } + my $snapshot; + eval { - my $vm_path_name = $vm->config->files->vmPathName; - log_message "Copying machine configuration file: $vm_path_name"; + log_message "Copying machine files."; - $file_manager->CopyDatastoreFile( - sourceName => $vm_path_name, - sourceDatacenter => $dc, - destinationName => "$ds_tmp_dir/$vm_name.vmx", - destinationDatacenter => $dc + my $files = $vm->layoutEx->file; + my @copy_files = ( + 'config', + 'extendedConfig', + 'nvram', + 'log' ); - my $vdm = Vim::get_view(mo_ref => $service_content->virtualDiskManager); - my $devices = $vm->config->hardware->device; + foreach my $file (@$files) { + my $file_path = $file->name; + my $file_name = basename($file_path); - log_message "Consolidating and removing existing snapshots."; - $vm->RemoveAllSnapshots(); - - log_message "Creating backup snapshot."; - $vm->CreateSnapshot( - name => "backup", - description => "Scheduled backup", - memory => 0, - quiesce => 0 - ); - - foreach my $device (@$devices) { - if ($device->isa('VirtualDisk')) { - my $disk_path = $device->backing->fileName; - my $disk_file = basename($disk_path); - log_message "Copying virtual disk file: $disk_path"; - - # XXX: Not implemented by Perl vSphere SDK - #my $disk_spec = VirtualDiskSpec->new( - # adapterType => 'busLogic', - # diskType => 'thin' - #); - $vdm->CopyVirtualDisk( - sourceName => $disk_path, + if ($file->type ~~ @copy_files) { + log_message $file_path; + $file_manager->CopyDatastoreFile( + sourceName => $file_path, sourceDatacenter => $dc, - destName => "$ds_tmp_dir/$disk_file", - destDatacenter => $dc - #destSpec => $disk_spec + destinationName => "$ds_tmp_dir/$file_name", + destinationDatacenter => $dc ); } } - log_message "Consolidating and removing snapshots."; - $vm->RemoveAllSnapshots(); + log_message "Creating backup snapshot."; + my $snapshotRef = $vm->CreateSnapshot( + name => "backup", + description => "Scheduled backup", + memory => false, + quiesce => true + ); + $snapshot = Vim::get_view(mo_ref => $snapshotRef); + + log_message "Copying virtual disk files."; + + my $devices = $snapshot->config->hardware->device; + my $vdm = Vim::get_view(mo_ref => $service_content->virtualDiskManager); + + foreach my $device (@$devices) { + if (not($device->isa('VirtualDisk'))) { + next; + } + + my $backing = $device->backing; + my $disk_path = $backing->fileName; + my $disk_file = basename($disk_path); + log_message $disk_path; + + # XXX: Not implemented by Perl vSphere SDK + #my $disk_spec = VirtualDiskSpec->new( + # adapterType => 'busLogic', + # diskType => 'thin' + #); + $vdm->CopyVirtualDisk( + sourceName => $disk_path, + sourceDatacenter => $dc, + destName => "$ds_tmp_dir/$disk_file", + destDatacenter => $dc + #destSpec => $disk_spec + ); + } + + log_message "Removing backup snapshot."; + $snapshot->RemoveSnapshot( + removeChildren => true, + consolidate => true + ); my $pigz_processes; @@ -474,13 +495,18 @@ sub backup_machine() { die "An error occurred when trying to compress '$vm_name' machine files."; } }; - my $err = $@; + my $err = $@; if ($err) { log_error "An error ocurred during '$vm_name' backup, aborting."; - log_message "Consolidating and removing snapshots."; - $vm->RemoveAllSnapshots(); + if ($snapshot) { + log_message "Removing backup snapshot."; + $snapshot->RemoveSnapshot( + removeChildren => true, + consolidate => true + ); + } } log_message "Removing temporary directory: $local_tmp_dir"; @@ -802,17 +828,8 @@ sub open_machine() { unless ($vm) { die "Machine '$vm_name' not found."; } - - my $vm_remote_host = $vm->runtime->host; - $remote_host = Vim::get_view(mo_ref => $vm_remote_host)->name; - my $datastores = eval {$vm->{datastore} || []}; - my $dsview = Vim::get_views(mo_ref_array => $datastores, properties => ['name']); - - foreach (@$dsview) { - $vm_datastore = $_->{name}; - } - log_message "Found machine '$vm_name' at host $remote_host at datastore '$vm_datastore'."; + log_message "Found machine '$vm_name'."; } sub set_power_state {