use PVE::RPCEnvironment;
use PVE::Storage;
use PVE::Cluster qw(cfs_read_file);
-use PVE::VZDump::OpenVZ;
use Time::localtime;
use Time::Local;
use PVE::JSONSchema qw(get_standard_option);
+use PVE::HA::Env::PVE2;
+use PVE::HA::Config;
my @posix_filesystems = qw(ext3 ext4 nfs nfs4 reiserfs xfs);
my $lockfile = '/var/run/vzdump.lock';
+my $pidfile = '/var/run/vzdump.pid';
+
my $logdir = '/var/log/vzdump';
-my @plugins = qw (PVE::VZDump::OpenVZ);
+my @plugins = qw();
+
+my $confdesc = {
+ vmid => {
+ type => 'string', format => 'pve-vmid-list',
+ description => "The ID of the guest system you want to backup.",
+ completion => \&PVE::Cluster::complete_local_vmid,
+ optional => 1,
+ },
+ node => get_standard_option('pve-node', {
+ description => "Only run if executed on this node.",
+ completion => \&PVE::Cluster::get_nodelist,
+ optional => 1,
+ }),
+ all => {
+ type => 'boolean',
+ description => "Backup all known guest systems on this host.",
+ optional => 1,
+ default => 0,
+ },
+ stdexcludes => {
+ type => 'boolean',
+ description => "Exclude temporary files and logs.",
+ optional => 1,
+ default => 1,
+ },
+ compress => {
+ type => 'string',
+ description => "Compress dump file.",
+ optional => 1,
+ enum => ['0', '1', 'gzip', 'lzo'],
+ default => '0',
+ },
+ pigz=> {
+ type => "integer",
+ description => "Use pigz instead of gzip when N>0.".
+ " N=1 uses half of cores, N>1 uses N as thread count.",
+ optional => 1,
+ default => 0,
+ },
+ quiet => {
+ type => 'boolean',
+ description => "Be quiet.",
+ optional => 1,
+ default => 0,
+ },
+ mode => {
+ type => 'string',
+ description => "Backup mode.",
+ optional => 1,
+ default => 'snapshot',
+ enum => [ 'snapshot', 'suspend', 'stop' ],
+ },
+ exclude => {
+ type => 'string', format => 'pve-vmid-list',
+ description => "Exclude specified guest systems (assumes --all)",
+ optional => 1,
+ },
+ 'exclude-path' => {
+ type => 'string', format => 'string-alist',
+ description => "Exclude certain files/directories (shell globs).",
+ optional => 1,
+ },
+ mailto => {
+ type => 'string', format => 'string-list',
+ description => "",
+ optional => 1,
+ },
+ mailnotification => {
+ type => 'string',
+ description => "Specify when to send an email",
+ optional => 1,
+ enum => [ 'always', 'failure' ],
+ default => 'always',
+ },
+ tmpdir => {
+ type => 'string',
+ description => "Store temporary files to specified directory.",
+ optional => 1,
+ },
+ dumpdir => {
+ type => 'string',
+ description => "Store resulting files to specified directory.",
+ optional => 1,
+ },
+ script => {
+ type => 'string',
+ description => "Use specified hook script.",
+ optional => 1,
+ },
+ storage => get_standard_option('pve-storage-id', {
+ description => "Store resulting file to this storage.",
+ completion => \&complete_backup_storage,
+ optional => 1,
+ }),
+ stop => {
+ type => 'boolean',
+ description => "Stop runnig backup jobs on this host.",
+ optional => 1,
+ default => 0,
+ },
+ size => {
+ type => 'integer',
+ description => "Unused, will be removed in a future release.",
+ optional => 1,
+ minimum => 500,
+ default => 1024,
+ },
+ bwlimit => {
+ type => 'integer',
+ description => "Limit I/O bandwidth (KBytes per second).",
+ optional => 1,
+ minimum => 0,
+ default => 0,
+ },
+ ionice => {
+ type => 'integer',
+ description => "Set CFQ ionice priority.",
+ optional => 1,
+ minimum => 0,
+ maximum => 8,
+ default => 7,
+ },
+ lockwait => {
+ type => 'integer',
+ description => "Maximal time to wait for the global lock (minutes).",
+ optional => 1,
+ minimum => 0,
+ default => 3*60, # 3 hours
+ },
+ stopwait => {
+ type => 'integer',
+ description => "Maximal time to wait until a guest system is stopped (minutes).",
+ optional => 1,
+ minimum => 0,
+ default => 10, # 10 minutes
+ },
+ maxfiles => {
+ type => 'integer',
+ description => "Maximal number of backup files per guest system.",
+ optional => 1,
+ minimum => 1,
+ default => 1,
+ },
+ remove => {
+ type => 'boolean',
+ description => "Remove old backup files if there are more than 'maxfiles' backup files.",
+ optional => 1,
+ default => 1,
+ },
+};
# Load available plugins
-my $pveplug = "/usr/share/perl5/PVE/VZDump/QemuServer.pm";
-if (-f $pveplug) {
- eval { require $pveplug; };
- if (!$@) {
- PVE::VZDump::QemuServer->import ();
- push @plugins, "PVE::VZDump::QemuServer";
- } else {
- warn $@;
- }
+my @pve_vzdump_classes = qw(PVE::VZDump::QemuServer PVE::VZDump::LXC);
+foreach my $plug (@pve_vzdump_classes) {
+ my $filename = "/usr/share/perl5/$plug.pm";
+ $filename =~ s!::!/!g;
+ if (-f $filename) {
+ eval { require $filename; };
+ if (!$@) {
+ $plug->import ();
+ push @plugins, $plug;
+ } else {
+ warn $@;
+ }
+ }
}
# helper functions
sub storage_info {
my $storage = shift;
- my $cfg = cfs_read_file('storage.cfg');
+ my $cfg = PVE::Storage::config();
my $scfg = PVE::Storage::storage_config($cfg, $storage);
my $type = $scfg->{type};
die "can't use storage type '$type' for backup\n"
if (!($type eq 'dir' || $type eq 'nfs' || $type eq 'glusterfs'));
- die "can't use storage for backups - wrong content type\n"
+ die "can't use storage '$storage' for backups - wrong content type\n"
if (!$scfg->{content}->{backup});
PVE::Storage::activate_storage($cfg, $storage);
my $fn = "/etc/vzdump.conf";
- my $res = {
- bwlimit => 0,
- ionice => 7,
- size => 1024,
- lockwait => 3*60, # 3 hours
- stopwait => 10, # 10 minutes
- mode => 'snapshot',
- maxfiles => 1,
+ my $defaults = {
+ map {
+ my $default = $confdesc->{$_}->{default};
+ defined($default) ? ($_ => $default) : ()
+ } keys %$confdesc
};
- my $fh = IO::File->new ("<$fn");
- return $res if !$fh;
-
- my $line;
- while (defined ($line = <$fh>)) {
- next if $line =~ m/^\s*$/;
- next if $line =~ m/^\#/;
-
- if ($line =~ m/tmpdir:\s*(.*\S)\s*$/) {
- $res->{tmpdir} = $1;
- } elsif ($line =~ m/dumpdir:\s*(.*\S)\s*$/) {
- $res->{dumpdir} = $1;
- } elsif ($line =~ m/storage:\s*(\S+)\s*$/) {
- $res->{storage} = $1;
- } elsif ($line =~ m/script:\s*(.*\S)\s*$/) {
- $res->{script} = $1;
- } elsif ($line =~ m/bwlimit:\s*(\d+)\s*$/) {
- $res->{bwlimit} = int($1);
- } elsif ($line =~ m/ionice:\s*([0-8])\s*$/) {
- $res->{ionice} = int($1);
- } elsif ($line =~ m/lockwait:\s*(\d+)\s*$/) {
- $res->{lockwait} = int($1);
- } elsif ($line =~ m/stopwait:\s*(\d+)\s*$/) {
- $res->{stopwait} = int($1);
- } elsif ($line =~ m/size:\s*(\d+)\s*$/) {
- $res->{size} = int($1);
- } elsif ($line =~ m/maxfiles:\s*(\d+)\s*$/) {
- $res->{maxfiles} = int($1);
- } elsif ($line =~ m/exclude-path:\s*(.*)\s*$/) {
- $res->{'exclude-path'} = PVE::Tools::split_args($1);
- } elsif ($line =~ m/mode:\s*(stop|snapshot|suspend)\s*$/) {
- $res->{mode} = $1;
- } else {
- debugmsg ('warn', "unable to parse configuration file '$fn' - error at line " . $., undef, 1);
- }
+ my $raw;
+ eval { $raw = PVE::Tools::file_get_contents($fn); };
+ return $defaults if $@;
+ my $conf_schema = { type => 'object', properties => $confdesc, };
+ my $res = PVE::JSONSchema::parse_config($conf_schema, $fn, $raw);
+ if (my $excludes = $res->{'exclude-path'}) {
+ $res->{'exclude-path'} = PVE::Tools::split_args($excludes);
}
- close ($fh);
-
- return $res;
-}
-
-sub find_add_exclude {
- my ($self, $excltype, $value) = @_;
-
- if (($excltype eq '-regex') || ($excltype eq '-files')) {
- $value = "\.$value";
+ foreach my $key (keys %$defaults) {
+ $res->{$key} = $defaults->{$key} if !defined($res->{$key});
}
- if ($excltype eq '-files') {
- push @{$self->{findexcl}}, "'('", '-not', '-type', 'd', '-regex' , "'$value'", "')'", '-o';
- } else {
- push @{$self->{findexcl}}, "'('", $excltype , "'$value'", '-prune', "')'", '-o';
- }
+ return $res;
}
-my $sendmail = sub {
- my ($self, $tasklist, $totaltime) = @_;
+sub sendmail {
+ my ($self, $tasklist, $totaltime, $err) = @_;
my $opts = $self->{opts};
}
}
- my $stat = $ecount ? 'backup failed' : 'backup successful';
+ my $notify = $opts->{mailnotification} || 'always';
+ return if (!$ecount && !$err && ($notify eq 'failure'));
+
+ my $stat = ($ecount || $err) ? 'backup failed' : 'backup successful';
+ $stat .= ": $err" if $err;
my $hostname = `hostname -f` || PVE::INotify::nodename();
chomp $hostname;
- my $boundary = "----_=_NextPart_001_".int(time).$$;
-
- my $rcvrarg = '';
- foreach my $r (@$mailto) {
- $rcvrarg .= " '$r'";
- }
-
- open (MAIL,"|sendmail -B 8BITMIME $rcvrarg") ||
- die "unable to open 'sendmail' - $!";
-
- my $rcvrtxt = join (', ', @$mailto);
-
- print MAIL "Content-Type: multipart/alternative;\n";
- print MAIL "\tboundary=\"$boundary\"\n";
- print MAIL "FROM: vzdump backup tool <root>\n";
- print MAIL "TO: $rcvrtxt\n";
- print MAIL "SUBJECT: vzdump backup status ($hostname) : $stat\n";
- print MAIL "\n";
- print MAIL "This is a multi-part message in MIME format.\n\n";
- print MAIL "--$boundary\n";
-
- print MAIL "Content-Type: text/plain;\n";
- print MAIL "\tcharset=\"UTF8\"\n";
- print MAIL "Content-Transfer-Encoding: 8bit\n";
- print MAIL "\n";
-
# text part
-
- my $fill = ' '; # Avoid The Remove Extra Line Breaks Issue (MS Outlook)
-
- print MAIL sprintf ("${fill}%-10s %-6s %10s %10s %s\n", qw(VMID STATUS TIME SIZE FILENAME));
+ my $text = sprintf ("%-10s %-6s %10s %10s %s\n", qw(VMID STATUS TIME SIZE FILENAME));
foreach my $task (@$tasklist) {
my $vmid = $task->{vmid};
if ($task->{state} eq 'ok') {
- print MAIL sprintf ("${fill}%-10s %-6s %10s %10s %s\n", $vmid,
- $task->{state},
+ $text .= sprintf ("%-10s %-6s %10s %10s %s\n", $vmid,
+ $task->{state},
format_time($task->{backuptime}),
format_size ($task->{size}),
$task->{tarfile});
} else {
- print MAIL sprintf ("${fill}%-10s %-6s %10s %8.2fMB %s\n", $vmid,
- $task->{state},
+ $text .= sprintf ("%-10s %-6s %10s %8.2fMB %s\n", $vmid,
+ $task->{state},
format_time($task->{backuptime}),
0, '-');
}
}
- print MAIL "${fill}\n";
- print MAIL "${fill}Detailed backup logs:\n";
- print MAIL "${fill}\n";
- print MAIL "$fill$cmdline\n";
- print MAIL "${fill}\n";
+
+ $text .= "Detailed backup logs:\n\n";
+ $text .= "$cmdline\n\n";
foreach my $task (@$tasklist) {
my $vmid = $task->{vmid};
my $log = $task->{tmplog};
if (!$log) {
- print MAIL "${fill}$vmid: no log available\n\n";
+ $text .= "$vmid: no log available\n\n";
next;
}
open (TMP, "$log");
- while (my $line = <TMP>) { print MAIL encode8bit ("${fill}$vmid: $line"); }
+ while (my $line = <TMP>) { $text .= encode8bit ("$vmid: $line"); }
close (TMP);
- print MAIL "${fill}\n";
+ $text .= "\n";
}
- # end text part
- print MAIL "\n--$boundary\n";
-
- print MAIL "Content-Type: text/html;\n";
- print MAIL "\tcharset=\"UTF8\"\n";
- print MAIL "Content-Transfer-Encoding: 8bit\n";
- print MAIL "\n";
-
# html part
-
- print MAIL "<html><body>\n";
-
- print MAIL "<table border=1 cellpadding=3>\n";
-
- print MAIL "<tr><td>VMID<td>NAME<td>STATUS<td>TIME<td>SIZE<td>FILENAME</tr>\n";
+ my $html = "<html><body>\n";
+ $html .= "<table border=1 cellpadding=3>\n";
+ $html .= "<tr><td>VMID<td>NAME<td>STATUS<td>TIME<td>SIZE<td>FILENAME</tr>\n";
my $ssize = 0;
$ssize += $task->{size};
- print MAIL sprintf ("<tr><td>%s<td>%s<td>OK<td>%s<td align=right>%s<td>%s</tr>\n",
+ $html .= sprintf ("<tr><td>%s<td>%s<td>OK<td>%s<td align=right>%s<td>%s</tr>\n",
$vmid, $name,
format_time($task->{backuptime}),
format_size ($task->{size}),
escape_html ($task->{tarfile}));
} else {
- print MAIL sprintf ("<tr><td>%s<td>%s<td><font color=red>FAILED<td>%s<td colspan=2>%s</tr>\n",
-
- $vmid, $name, format_time($task->{backuptime}),
+ $html .= sprintf ("<tr><td>%s<td>%s<td><font color=red>FAILED<td>%s<td colspan=2>%s</tr>\n",
+ $vmid, $name, format_time($task->{backuptime}),
escape_html ($task->{msg}));
}
}
- print MAIL sprintf ("<tr><td align=left colspan=3>TOTAL<td>%s<td>%s<td></tr>",
+ $html .= sprintf ("<tr><td align=left colspan=3>TOTAL<td>%s<td>%s<td></tr>",
format_time ($totaltime), format_size ($ssize));
- print MAIL "</table><br><br>\n";
- print MAIL "Detailed backup logs:<br>\n";
- print MAIL "<br>\n";
- print MAIL "<pre>\n";
- print MAIL escape_html($cmdline) . "\n";
- print MAIL "\n";
+ $html .= "</table><br><br>\n";
+ $html .= "Detailed backup logs:<br /><br />\n";
+ $html .= "<pre>\n";
+ $html .= escape_html($cmdline) . "\n\n";
foreach my $task (@$tasklist) {
my $vmid = $task->{vmid};
my $log = $task->{tmplog};
if (!$log) {
- print MAIL "$vmid: no log available\n\n";
+ $html .= "$vmid: no log available\n\n";
next;
}
open (TMP, "$log");
while (my $line = <TMP>) {
if ($line =~ m/^\S+\s\d+\s+\d+:\d+:\d+\s+(ERROR|WARN):/) {
- print MAIL encode8bit ("$vmid: <font color=red>".
- escape_html ($line) . "</font>");
+ $html .= encode8bit ("$vmid: <font color=red>".
+ escape_html ($line) . "</font>");
} else {
- print MAIL encode8bit ("$vmid: " . escape_html ($line));
+ $html .= encode8bit ("$vmid: " . escape_html ($line));
}
}
close (TMP);
- print MAIL "\n";
+ $html .= "\n";
}
- print MAIL "</pre>\n";
+ $html .= "</pre></body></html>\n";
+ # end html part
- print MAIL "</body></html>\n";
+ my $subject = "vzdump backup status ($hostname) : $stat";
- # end html part
- print MAIL "\n--$boundary--\n";
+ my $dcconf = PVE::Cluster::cfs_read_file('datacenter.cfg');
+ my $mailfrom = $dcconf->{email_from} || "root";
- close(MAIL);
+ PVE::Tools::sendmail($mailto, $subject, $text, $html, $mailfrom, "vzdump backup tool");
};
sub new {
$opts->{remove} = 1 if !defined($opts->{remove});
foreach my $k (keys %$defaults) {
+ next if $k eq 'exclude-path'; # dealt with separately
if ($k eq 'dumpdir' || $k eq 'storage') {
$opts->{$k} = $defaults->{$k} if !defined ($opts->{dumpdir}) &&
!defined ($opts->{storage});
$skiplist = [] if !$skiplist;
my $self = bless { cmdline => $cmdline, opts => $opts, skiplist => $skiplist };
- #always skip '.'
- push @{$self->{findexcl}}, "'('", '-regex' , "'^\\.\$'", "')'", '-o';
-
- $self->find_add_exclude ('-type', 's'); # skip sockets
-
+ my $findexcl = $self->{findexcl} = [];
if ($defaults->{'exclude-path'}) {
- foreach my $path (@{$defaults->{'exclude-path'}}) {
- $self->find_add_exclude ('-regex', $path);
- }
+ push @$findexcl, @{$defaults->{'exclude-path'}};
}
if ($opts->{'exclude-path'}) {
- foreach my $path (@{$opts->{'exclude-path'}}) {
- $self->find_add_exclude ('-regex', $path);
- }
+ push @$findexcl, @{$opts->{'exclude-path'}};
}
if ($opts->{stdexcludes}) {
- $self->find_add_exclude ('-files', '/var/log/.+');
- $self->find_add_exclude ('-regex', '/tmp/.+');
- $self->find_add_exclude ('-regex', '/var/tmp/.+');
- $self->find_add_exclude ('-regex', '/var/run/.+pid');
+ push @$findexcl, '/var/log/?*',
+ '/tmp/?*',
+ '/var/tmp/?*',
+ '/var/run/?*.pid';
}
foreach my $p (@plugins) {
}
sub getlock {
- my ($self) = @_;
+ my ($self, $upid) = @_;
+ my $fh;
+
my $maxwait = $self->{opts}->{lockwait} || $self->{lockwait};
+ die "missimg UPID" if !$upid; # should not happen
+
if (!open (SERVER_FLCK, ">>$lockfile")) {
debugmsg ('err', "can't open lock on file '$lockfile' - $!", undef, 1);
- exit (-1);
+ die "can't open lock on file '$lockfile' - $!";
}
- if (flock (SERVER_FLCK, LOCK_EX|LOCK_NB)) {
- return;
- }
+ if (!flock (SERVER_FLCK, LOCK_EX|LOCK_NB)) {
- if (!$maxwait) {
- debugmsg ('err', "can't aquire lock '$lockfile' (wait = 0)", undef, 1);
- exit (-1);
- }
+ if (!$maxwait) {
+ debugmsg ('err', "can't aquire lock '$lockfile' (wait = 0)", undef, 1);
+ die "can't aquire lock '$lockfile' (wait = 0)";
+ }
- debugmsg('info', "trying to get global lock - waiting...", undef, 1);
+ debugmsg('info', "trying to get global lock - waiting...", undef, 1);
- eval {
- alarm ($maxwait * 60);
+ eval {
+ alarm ($maxwait * 60);
- local $SIG{ALRM} = sub { alarm (0); die "got timeout\n"; };
+ local $SIG{ALRM} = sub { alarm (0); die "got timeout\n"; };
- if (!flock (SERVER_FLCK, LOCK_EX)) {
- my $err = $!;
- close (SERVER_FLCK);
+ if (!flock (SERVER_FLCK, LOCK_EX)) {
+ my $err = $!;
+ close (SERVER_FLCK);
+ alarm (0);
+ die "$err\n";
+ }
alarm (0);
- die "$err\n";
- }
+ };
alarm (0);
- };
- alarm (0);
- my $err = $@;
+ my $err = $@;
+
+ if ($err) {
+ debugmsg ('err', "can't aquire lock '$lockfile' - $err", undef, 1);
+ die "can't aquire lock '$lockfile' - $err";
+ }
- if ($err) {
- debugmsg ('err', "can't aquire lock '$lockfile' - $err", undef, 1);
- exit (-1);
+ debugmsg('info', "got global lock", undef, 1);
}
- debugmsg('info', "got global lock", undef, 1);
+ PVE::Tools::file_set_contents($pidfile, $upid);
}
sub run_hook_script {
}
sub compressor_info {
- my ($opt_compress) = @_;
+ my ($opts) = @_;
+ my $opt_compress = $opts->{compress};
if (!$opt_compress || $opt_compress eq '0') {
return undef;
} elsif ($opt_compress eq '1' || $opt_compress eq 'lzo') {
return ('lzop', 'lzo');
} elsif ($opt_compress eq 'gzip') {
- return ('gzip', 'gz');
+ if ($opts->{pigz} > 0) {
+ # As default use int((#cores + 1)/2), we need #cores+1 for the case that #cores = 1
+ my $cores = POSIX::sysconf(84);
+ my $pigz_threads = ($opts->{pigz} > 1) ? $opts->{pigz} : int(($cores + 1)/2);
+ return ("pigz -p ${pigz_threads}", 'gz');
+ } else {
+ return ('gzip', 'gz');
+ }
} else {
die "internal error - unknown compression option '$opt_compress'";
}
eval {
die "unable to find VM '$vmid'\n" if !$plugin;
+ # for now we deny backups of a running ha managed service in *stop* mode
+ # as it interferes with the HA stack (enabled services should not stop).
+ if ($opts->{mode} eq 'stop' &&
+ PVE::HA::Config::vm_is_ha_managed($vmid, 'enabled'))
+ {
+ die "Cannot execute a backup with stop mode on a HA managed and".
+ " enabled Service. Use snapshot mode or disable the Service.\n";
+ }
+
my $vmtype = $plugin->type();
my $tmplog = "$logdir/$vmtype-$vmid.log";
my $logfile = $task->{logfile} = "$opts->{dumpdir}/$basename.log";
my $ext = $vmtype eq 'qemu' ? '.vma' : '.tar';
- my ($comp, $comp_ext) = compressor_info($opts->{compress});
+ my ($comp, $comp_ext) = compressor_info($opts);
if ($comp && $comp_ext) {
$ext .= ".${comp_ext}";
}
$self->run_hook_script ('backup-start', $task, $logfd);
- if ($vmtype eq 'openvz') {
+ if ($vmtype eq 'lxc') {
# pre-suspend rsync
- $plugin->copy_data_phase1 ($task, $vmid);
+ $plugin->copy_data_phase1($task, $vmid);
}
debugmsg ('info', "suspend vm", $logfd);
$plugin->suspend_vm ($task, $vmid);
$cleanup->{resume} = 1;
- if ($vmtype eq 'openvz') {
+ if ($vmtype eq 'lxc') {
# post-suspend rsync
- $plugin->copy_data_phase2 ($task, $vmid);
+ $plugin->copy_data_phase2($task, $vmid);
debugmsg ('info', "resume vm", $logfd);
$cleanup->{resume} = 0;
- $self->run_hook_script ('pre-restart', $task, $logfd);
- $plugin->resume_vm ($task, $vmid);
+ $self->run_hook_script('pre-restart', $task, $logfd);
+ $plugin->resume_vm($task, $vmid);
my $delay = time () - $vmstoptime;
- debugmsg ('info', "vm is online again after $delay seconds", $logfd);
+ debugmsg('info', "vm is online again after $delay seconds", $logfd);
}
-
+
} elsif ($mode eq 'snapshot') {
$self->run_hook_script ('backup-start', $task, $logfd);
warn $@ if $@;
}
- eval { $plugin->cleanup ($task, $vmid) };
- warn $@ if $@;
+ if (defined($task->{mode})) {
+ # only call cleanup when necessary (when prepare was executed)
+ eval { $plugin->cleanup ($task, $vmid) };
+ warn $@ if $@;
+ }
eval { $plugin->set_logfd (undef); };
warn $@ if $@;
eval { unlink $task->{tmptar} if $task->{tmptar} && -f $task->{tmptar}; };
warn $@ if $@;
-# eval { rmtree $task->{tmpdir} if $task->{tmpdir} && -d $task->{tmpdir}; };
+ eval { rmtree $task->{tmpdir} if $task->{tmpdir} && -d $task->{tmpdir}; };
warn $@ if $@;
my $delay = $task->{backuptime} = time () - $vmstarttime;
my $totaltime = time() - $starttime;
- eval { $self->$sendmail ($tasklist, $totaltime); };
+ eval { $self->sendmail ($tasklist, $totaltime); };
debugmsg ('err', $@) if $@;
die $err if $err;
die "job errors\n" if $errcount;
+
+ unlink $pidfile;
}
-my $confdesc = {
- vmid => {
- type => 'string', format => 'pve-vmid-list',
- description => "The ID of the VM you want to backup.",
- optional => 1,
- },
- node => get_standard_option('pve-node', {
- description => "Only run if executed on this node.",
- optional => 1,
- }),
- all => {
- type => 'boolean',
- description => "Backup all known VMs on this host.",
- optional => 1,
- default => 0,
- },
- stdexcludes => {
- type => 'boolean',
- description => "Exclude temorary files and logs.",
- optional => 1,
- default => 1,
- },
- compress => {
- type => 'string',
- description => "Compress dump file.",
- optional => 1,
- enum => ['0', '1', 'gzip', 'lzo'],
- default => 'lzo',
- },
- quiet => {
- type => 'boolean',
- description => "Be quiet.",
- optional => 1,
- default => 0,
- },
- mode => {
- type => 'string',
- description => "Backup mode.",
- optional => 1,
- default => 'stop',
- enum => [ 'snapshot', 'suspend', 'stop' ],
- },
- exclude => {
- type => 'string', format => 'pve-vmid-list',
- description => "exclude specified VMs (assumes --all)",
- optional => 1,
- },
- 'exclude-path' => {
- type => 'string', format => 'string-alist',
- description => "exclude certain files/directories (regex).",
- optional => 1,
- },
- mailto => {
- type => 'string', format => 'string-list',
- description => "",
- optional => 1,
- },
- tmpdir => {
- type => 'string',
- description => "Store temporary files to specified directory.",
- optional => 1,
- },
- dumpdir => {
- type => 'string',
- description => "Store resulting files to specified directory.",
- optional => 1,
- },
- script => {
- type => 'string',
- description => "Use specified hook script.",
- optional => 1,
- },
- storage => get_standard_option('pve-storage-id', {
- description => "Store resulting file to this storage.",
- optional => 1,
- }),
- size => {
- type => 'integer',
- description => "LVM snapshot size im MB.",
- optional => 1,
- minimum => 500,
- },
- bwlimit => {
- type => 'integer',
- description => "Limit I/O bandwidth (KBytes per second).",
- optional => 1,
- minimum => 0,
- },
- ionice => {
- type => 'integer',
- description => "Set CFQ ionice priority.",
- optional => 1,
- minimum => 0,
- maximum => 8,
- },
- lockwait => {
- type => 'integer',
- description => "Maximal time to wait for the global lock (minutes).",
- optional => 1,
- minimum => 0,
- },
- stopwait => {
- type => 'integer',
- description => "Maximal time to wait until a VM is stopped (minutes).",
- optional => 1,
- minimum => 0,
- },
- maxfiles => {
- type => 'integer',
- description => "Maximal number of backup files per VM.",
- optional => 1,
- minimum => 1,
- },
- remove => {
- type => 'boolean',
- description => "Remove old backup files if there are more than 'maxfiles' backup files.",
- optional => 1,
- default => 1,
- },
-};
sub option_exists {
my $key = shift;
$param->{all} = 1 if defined($param->{exclude});
+ warn "option 'size' is deprecated and will be removed in a future " .
+ "release, please update your script/configuration!\n"
+ if defined($param->{size});
+
return if !$check_missing;
raise_param_exc({ vmid => "property is missing"})
- if !$param->{all} && !$param->{vmid};
+ if !($param->{all} || $param->{stop}) && !$param->{vmid};
+
+}
+sub stop_running_backups {
+ my($self) = @_;
+
+ my $upid = PVE::Tools::file_read_firstline($pidfile);
+ return if !$upid;
+
+ my $task = PVE::Tools::upid_decode($upid);
+
+ if (PVE::ProcFSTools::check_process_running($task->{pid}, $task->{pstart}) &&
+ PVE::ProcFSTools::read_proc_starttime($task->{pid}) == $task->{pstart}) {
+ kill(15, $task->{pid});
+ # wait max 15 seconds to shut down (else, do nothing for now)
+ my $i;
+ for ($i = 15; $i > 0; $i--) {
+ last if !PVE::ProcFSTools::check_process_running(($task->{pid}, $task->{pstart}));
+ sleep (1);
+ }
+ die "stoping backup process $task->{pid} failed\n" if $i == 0;
+ }
}
sub command_line {
}
foreach my $p (keys %$param) {
- next if $p eq 'id' || $p eq 'vmid' || $p eq 'starttime' || $p eq 'dow' || $p eq 'stdout';
+ next if $p eq 'id' || $p eq 'vmid' || $p eq 'starttime' ||
+ $p eq 'dow' || $p eq 'stdout' || $p eq 'enabled';
my $v = $param->{$p};
my $pd = $confdesc->{$p} || die "no such vzdump option '$p'\n";
if ($p eq 'exclude-path') {
return $cmd;
}
+# bash completion helpers
+sub complete_backup_storage {
+
+ my $cfg = PVE::Storage::config();
+ my $ids = $cfg->{ids};
+
+ my $nodename = PVE::INotify::nodename();
+
+ my $res = [];
+ foreach my $sid (keys %$ids) {
+ my $scfg = $ids->{$sid};
+ next if !PVE::Storage::storage_check_enabled($cfg, $sid, $nodename, 1);
+ next if !$scfg->{content}->{backup};
+ push @$res, $sid;
+ }
+
+ return $res;
+}
+
1;