Naming conventions vzdump backups (1.4)

  • Thread starter Thread starter Myatu
  • Start date Start date
M

Myatu

Guest
Dietmar, et al.

Where can I change the default naming convention of automated backups generated through the web admin? Ie., "vzdump-openvz-101-2009_11_02-02_00_01.tgz" is not ideal in my situation and would prefer the simpler "vzdump-101.tgz" or even "vzdump-openvz-101.tgz" as before.
 
"Hardcoded" in a script or binary? I don't mind changing either, just point me in the right direction (source) :)

vzdump is a perl script. Anyway, new vzdump has a ne option to run hook scripts (--script). You can simply write such script and rename the file there - see 'man vzdump' - an example script is included.
 
Actually appending the date and time should be optional and an option in the GUI for appending the host name or container name will be welcome - the backup naming hack for v1.3 was nice. Must the script name be included in come conf file or provided in the GUI? Will that hack still work if configured into the files in v1.4?
 
# pveversion -v
Code:
pve-manager: 1.4-9 (pve-manager/1.4/4390)
qemu-server: 1.1-8
pve-kernel: 2.6.24-16
pve-qemu-kvm: 0.11.0-2
pve-firmware: 1
vncterm: 0.9-2
vzctl: 3.0.23-1pve3
vzdump: 1.2-5
vzprocps: 2.0.11-1dso2
vzquota: 3.0.11-1
# vzdump --help
Code:
Unknown option: help
usage: /usr/sbin/vzdump OPTIONS [--all | VMID]
        --exclude VMID          exclude VMID (assumes --all)
        --exclude-path REGEX    exclude certain files/directories
        --stdexcludes           exclude temorary files and logs
        --compress              compress dump file (gzip)
        --dumpdir DIR           store resulting files in DIR
        --maxfiles N            maximal number of backup files per VM
        --script FILENAME       execute hook script
        --storage STORAGE_ID    store resulting files to STORAGE_ID (PVE only)
        --tmpdir DIR            store temporary files in DIR
        --mailto EMAIL          send notification mail to EMAIL.
        --quiet                 be quiet.
        --stop                  stop/start VM if running
        --suspend               suspend/resume VM when running
        --snapshot              use LVM snapshot when running
        --size MB               LVM snapshot size
        --node CID              only run on pve cluster node CID
        --lockwait MINUTES      maximal time to wait for the global lock
        --stopwait MINUTES      maximal time to wait until a VM is stopped
        --bwlimit KBPS          limit I/O bandwidth; KBytes per second

Where in the example script file do we append the host name to the backup file? An example for appending the hosname / container or machine name would be nice.
 
# man vzdump
Code:
CONFIGURATION
       Global configuration is stored in /etc/vzdump.conf.
        tmpdir: DIR
        dumpdir: DIR
        storage: STORAGE_ID
        mode: snapshot|suspend|stop
        bwlimit: KBPS
        lockwait: MINUTES
        stopwait: MINUTES
        size: MB
        maxfiles: N
        script: FILENAME
The Global configuration file /etc/vzdump.conf is missing in a fresh install of v1.4 (even after apt updates).
 
The example script /usr/share/doc/vzdump/examples/hook-script.pl is quite cryptic. Please provide an example where we can append the host name / container name / machine name to the backed-up output file name.
# cat /usr/share/doc/vzdump/examples/hook-script.pl
Code:
#!/usr/bin/perl -w
# example hook script for vzdump (--script option)
use strict;
print "HOOK: " . join (' ', @ARGV) . "\n";
my $phase = shift;
if ($phase eq 'job-start' || 
    $phase eq 'job-end'  || 
    $phase eq 'job-abort') { 
    # do what you want 
} elsif ($phase eq 'backup-start' || 
  $phase eq 'backup-end' ||
  $phase eq 'backup-abort' || 
  $phase eq 'pre-stop' ||
  $phase eq 'pre-restart') {
    print "HOOK: " . join (' ', @ARGV) . "\n";
    my $mode = shift; # stop/suspend/snapshot
    my $vmid = shift;
    my $vmtype = $ENV{VMTYPE}; # openvz/qemu
    my $dumpdir = $ENV{DUMPDIR};
    my $hostname = $ENV{HOSTNAME};
    my $tarfile = $ENV{TARFILE};
    my $logfile = $ENV{LOGFILE};
    print "HOOK-ENV: vmtype=$vmtype;dumpdir=$dumpdir;hostname=$hostname;tarfile=$tarfile;logfile=$logfile\n";
    # example: copy resulting files to another host using scp
    #if ($phase eq 'backup-end') {
    # system ("scp $tarfile $logfile backup-host:/backup-dir") == 0 ||
    #     die "copy to backup-host failed";
    # unlink $tarfile;
    # unlink $logfile;
    #}
    
} else {
    die "got unknown phase '$phase'";
}
exit (0);
 
Appending hostname to backup filename

Trying mihait's patches from v1.3 to v1.4 will not work as the /usr/share/perl5/PVE/VZDump.pm file is used inside the /usr/sbin/vzdump file where his $opt_appendhostname variable would have gone.

Delving into the /usr/share/perl5/PVE/VZDump.pm file reveals that the hard coding is done at lines 771 to 774 and the --maxfiles parameter is checking files of the same naming convention at lines 946 to 955. The $hostname variable is determined at lines 376 to 377.

Hopefully a replacement of line 771
Code:
    my $bkname = "vzdump-$vmtype-$vmid";
with
Code:
    my $bkname = "vzdump-$vmtype-$vmid-$hostname";
or with
Code:
    my $bkname = "vzdump-$vmtype-$hostname-$vmid";
should do the trick!
 
Delving into the /usr/share/perl5/PVE/VZDump.pm file reveals that the hard coding is done at lines 771 to 774 and the --maxfiles parameter is checking files of the same naming convention at lines 946 to 955. The $hostname variable is determined at lines 376 to 377.

This variable $hostname is not available to the earlier lines. Hence it has to be copied just before the eval statement at line 762.

Replace Line 771
Code:
    my $bkname = "vzdump-$vmtype-$vmid";
with
Code:
    my $bkname = "vzdump-$vmtype-$vmid-$hostname";

Insert at Line 761
Code:
    my $hostname = `hostname -f` || hostname();
    chomp $hostname;

And viola! It Works!

This does not seem to break the --maxfiles feature as the variable $hostname is recomputed for this section. Please let me know if any other files are affected by this change.
 

Attachments

I like... Both the fact that vzdump has a script hook, and that apmuthu has taken the time to dig it all up :D
 
The method suggested by me in this thread above provides the hostname of the HN (hardware node) and not that of the VM that we are backing up. Whilst the email notification references the hostname of the VM being backed up, such a variable is not available for the backup file naming / reading code. I tried $task->{hostname} and $task->hostname() to no avail. The $self variable was also equally unsuccessful. What / how do we reference the hostname of the VM we are backing up? It appears that the tasklist is being cycled thru at around lines 1078 to 1081:
Code:
 foreach my $task (@$tasklist) {
     $self->exec_backup_task ($task);
     $errcount += 1 if $task->{state} ne 'ok';
 }
At this point, $task->{hostname} should have the hostname of the VM we are backing up. Unfortunately, it doesn't get into the subroutine!
 
Last edited:
We are in this situation because the $plugin>prepare is called for each $mode (snapshot, suspend, stop) only after the file name has been cobbled together. The $plugin->prepare gets the value of $task->{hostname} from the VZ Container or QEMU Machine config file.