]>
git.proxmox.com Git - pve-manager.git/blob - PVE/VZDump.pm
6 use PVE
::Exception
qw(raise_param_exc);
11 use POSIX
qw(strftime);
13 use PVE
::RPCEnvironment
;
15 use PVE
::Cluster
qw(cfs_read_file);
18 use PVE
::JSONSchema
qw(get_standard_option);
19 use PVE
::HA
::Env
::PVE2
;
22 my @posix_filesystems = qw(ext3 ext4 nfs nfs4 reiserfs xfs);
24 my $lockfile = '/var/run/vzdump.lock';
26 my $pidfile = '/var/run/vzdump.pid';
28 my $logdir = '/var/log/vzdump';
34 type
=> 'string', format
=> 'pve-vmid-list',
35 description
=> "The ID of the guest system you want to backup.",
36 completion
=> \
&PVE
::Cluster
::complete_local_vmid
,
39 node
=> get_standard_option
('pve-node', {
40 description
=> "Only run if executed on this node.",
41 completion
=> \
&PVE
::Cluster
::get_nodelist
,
46 description
=> "Backup all known guest systems on this host.",
52 description
=> "Exclude temporary files and logs.",
58 description
=> "Compress dump file.",
60 enum
=> ['0', '1', 'gzip', 'lzo'],
65 description
=> "Use pigz instead of gzip when N>0.".
66 " N=1 uses half of cores, N>1 uses N as thread count.",
72 description
=> "Be quiet.",
78 description
=> "Backup mode.",
80 default => 'snapshot',
81 enum
=> [ 'snapshot', 'suspend', 'stop' ],
84 type
=> 'string', format
=> 'pve-vmid-list',
85 description
=> "Exclude specified guest systems (assumes --all)",
89 type
=> 'string', format
=> 'string-alist',
90 description
=> "Exclude certain files/directories (shell globs).",
94 type
=> 'string', format
=> 'string-list',
100 description
=> "Specify when to send an email",
102 enum
=> [ 'always', 'failure' ],
107 description
=> "Store temporary files to specified directory.",
112 description
=> "Store resulting files to specified directory.",
117 description
=> "Use specified hook script.",
120 storage
=> get_standard_option
('pve-storage-id', {
121 description
=> "Store resulting file to this storage.",
122 completion
=> \
&complete_backup_storage
,
127 description
=> "Stop runnig backup jobs on this host.",
133 description
=> "Unused, will be removed in a future release.",
140 description
=> "Limit I/O bandwidth (KBytes per second).",
147 description
=> "Set CFQ ionice priority.",
155 description
=> "Maximal time to wait for the global lock (minutes).",
158 default => 3*60, # 3 hours
162 description
=> "Maximal time to wait until a guest system is stopped (minutes).",
165 default => 10, # 10 minutes
169 description
=> "Maximal number of backup files per guest system.",
176 description
=> "Remove old backup files if there are more than 'maxfiles' backup files.",
182 # Load available plugins
183 my @pve_vzdump_classes = qw(PVE::VZDump::QemuServer PVE::VZDump::LXC);
184 foreach my $plug (@pve_vzdump_classes) {
185 my $filename = "/usr/share/perl5/$plug.pm";
186 $filename =~ s!::!/!g;
188 eval { require $filename; };
191 push @plugins, $plug;
207 my ($mtype, $msg, $logfd, $syslog) = @_;
213 my $pre = $debugstattxt->{$mtype} || $debugstattxt->{'err'};
215 my $timestr = strftime
("%b %d %H:%M:%S", CORE
::localtime);
217 syslog
($mtype eq 'info' ?
'info' : 'err', "$pre $msg") if $syslog;
219 foreach my $line (split (/\n/, $msg)) {
220 print STDERR
"$pre $line\n";
221 print $logfd "$timestr $pre $line\n" if $logfd;
226 my ($logfd, $cmdstr, %param) = @_;
230 debugmsg
('info', $line, $logfd);
233 PVE
::Tools
::run_command
($cmdstr, %param, logfunc
=> $logfunc);
239 my $cfg = PVE
::Storage
::config
();
240 my $scfg = PVE
::Storage
::storage_config
($cfg, $storage);
241 my $type = $scfg->{type
};
243 die "can't use storage type '$type' for backup\n"
244 if (!($type eq 'dir' || $type eq 'nfs' || $type eq 'glusterfs'));
245 die "can't use storage '$storage' for backups - wrong content type\n"
246 if (!$scfg->{content
}->{backup
});
248 PVE
::Storage
::activate_storage
($cfg, $storage);
251 dumpdir
=> PVE
::Storage
::get_backup_dir
($cfg, $storage),
252 maxfiles
=> $scfg->{maxfiles
},
259 my $kb = $size / 1024;
262 return int ($kb) . "KB";
265 my $mb = $size / (1024*1024);
268 return int ($mb) . "MB";
271 return sprintf ("%.2fGB", $gb);
278 my $hours = int ($seconds/3600);
279 $seconds = $seconds - $hours*3600;
280 my $min = int ($seconds/60);
281 $seconds = $seconds - $min*60;
283 return sprintf ("%02d:%02d:%02d", $hours, $min, $seconds);
289 $str =~ s/^(.{990})/$1\n/mg; # reduce line length
307 foreach my $p (split (/:/, $ENV{PATH
})) {
314 die "unable to find command '$bin'\n";
321 foreach my $vmid (@vmids) {
322 die "ERROR: strange VM ID '${vmid}'\n" if $vmid !~ m/^\d+$/;
323 $vmid = int ($vmid); # remove leading zeros
332 sub read_vzdump_defaults
{
334 my $fn = "/etc/vzdump.conf";
338 my $default = $confdesc->{$_}->{default};
339 defined($default) ?
($_ => $default) : ()
344 eval { $raw = PVE
::Tools
::file_get_contents
($fn); };
345 return $defaults if $@;
347 my $conf_schema = { type
=> 'object', properties
=> $confdesc, };
348 my $res = PVE
::JSONSchema
::parse_config
($conf_schema, $fn, $raw);
349 if (my $excludes = $res->{'exclude-path'}) {
350 $res->{'exclude-path'} = PVE
::Tools
::split_args
($excludes);
353 foreach my $key (keys %$defaults) {
354 $res->{$key} = $defaults->{$key} if !defined($res->{$key});
361 my ($self, $tasklist, $totaltime, $err) = @_;
363 my $opts = $self->{opts
};
365 my $mailto = $opts->{mailto
};
367 return if !($mailto && scalar(@$mailto));
369 my $cmdline = $self->{cmdline
};
372 foreach my $task (@$tasklist) {
373 $ecount++ if $task->{state} ne 'ok';
374 chomp $task->{msg
} if $task->{msg
};
375 $task->{backuptime
} = 0 if !$task->{backuptime
};
376 $task->{size
} = 0 if !$task->{size
};
377 $task->{tarfile
} = 'unknown' if !$task->{tarfile
};
378 $task->{hostname
} = "VM $task->{vmid}" if !$task->{hostname
};
380 if ($task->{state} eq 'todo') {
381 $task->{msg
} = 'aborted';
385 my $notify = $opts->{mailnotification
} || 'always';
386 return if (!$ecount && !$err && ($notify eq 'failure'));
388 my $stat = ($ecount || $err) ?
'backup failed' : 'backup successful';
389 $stat .= ": $err" if $err;
391 my $hostname = `hostname -f` || PVE
::INotify
::nodename
();
395 my $text = sprintf ("%-10s %-6s %10s %10s %s\n", qw(VMID STATUS TIME SIZE FILENAME));
396 foreach my $task (@$tasklist) {
397 my $vmid = $task->{vmid
};
398 if ($task->{state} eq 'ok') {
400 $text .= sprintf ("%-10s %-6s %10s %10s %s\n", $vmid,
402 format_time
($task->{backuptime
}),
403 format_size
($task->{size
}),
406 $text .= sprintf ("%-10s %-6s %10s %8.2fMB %s\n", $vmid,
408 format_time
($task->{backuptime
}),
413 $text .= "Detailed backup logs:\n\n";
414 $text .= "$cmdline\n\n";
416 foreach my $task (@$tasklist) {
417 my $vmid = $task->{vmid
};
418 my $log = $task->{tmplog
};
420 $text .= "$vmid: no log available\n\n";
424 while (my $line = <TMP
>) { $text .= encode8bit
("$vmid: $line"); }
430 my $html = "<html><body>\n";
431 $html .= "<table border=1 cellpadding=3>\n";
432 $html .= "<tr><td>VMID<td>NAME<td>STATUS<td>TIME<td>SIZE<td>FILENAME</tr>\n";
436 foreach my $task (@$tasklist) {
437 my $vmid = $task->{vmid
};
438 my $name = $task->{hostname
};
440 if ($task->{state} eq 'ok') {
442 $ssize += $task->{size
};
444 $html .= sprintf ("<tr><td>%s<td>%s<td>OK<td>%s<td align=right>%s<td>%s</tr>\n",
446 format_time
($task->{backuptime
}),
447 format_size
($task->{size
}),
448 escape_html
($task->{tarfile
}));
450 $html .= sprintf ("<tr><td>%s<td>%s<td><font color=red>FAILED<td>%s<td colspan=2>%s</tr>\n",
451 $vmid, $name, format_time
($task->{backuptime
}),
452 escape_html
($task->{msg
}));
456 $html .= sprintf ("<tr><td align=left colspan=3>TOTAL<td>%s<td>%s<td></tr>",
457 format_time
($totaltime), format_size
($ssize));
459 $html .= "</table><br><br>\n";
460 $html .= "Detailed backup logs:<br /><br />\n";
462 $html .= escape_html
($cmdline) . "\n\n";
464 foreach my $task (@$tasklist) {
465 my $vmid = $task->{vmid
};
466 my $log = $task->{tmplog
};
468 $html .= "$vmid: no log available\n\n";
472 while (my $line = <TMP
>) {
473 if ($line =~ m/^\S+\s\d+\s+\d+:\d+:\d+\s+(ERROR|WARN):/) {
474 $html .= encode8bit
("$vmid: <font color=red>".
475 escape_html
($line) . "</font>");
477 $html .= encode8bit
("$vmid: " . escape_html
($line));
483 $html .= "</pre></body></html>\n";
486 my $subject = "vzdump backup status ($hostname) : $stat";
488 my $dcconf = PVE
::Cluster
::cfs_read_file
('datacenter.cfg');
489 my $mailfrom = $dcconf->{email_from
} || "root";
491 PVE
::Tools
::sendmail
($mailto, $subject, $text, $html, $mailfrom, "vzdump backup tool");
495 my ($class, $cmdline, $opts, $skiplist) = @_;
501 check_bin
('sendmail');
505 check_bin
('umount');
506 check_bin
('cstream');
507 check_bin
('ionice');
509 if ($opts->{mode
} && $opts->{mode
} eq 'snapshot') {
510 check_bin
('lvcreate');
512 check_bin
('lvremove');
515 my $defaults = read_vzdump_defaults
();
517 my $maxfiles = $opts->{maxfiles
}; # save here, because we overwrite with default
519 $opts->{remove
} = 1 if !defined($opts->{remove
});
521 foreach my $k (keys %$defaults) {
522 next if $k eq 'exclude-path'; # dealt with separately
523 if ($k eq 'dumpdir' || $k eq 'storage') {
524 $opts->{$k} = $defaults->{$k} if !defined ($opts->{dumpdir
}) &&
525 !defined ($opts->{storage
});
527 $opts->{$k} = $defaults->{$k} if !defined ($opts->{$k});
531 $opts->{dumpdir
} =~ s
|/+$|| if ($opts->{dumpdir
});
532 $opts->{tmpdir
} =~ s
|/+$|| if ($opts->{tmpdir
});
534 $skiplist = [] if !$skiplist;
535 my $self = bless { cmdline
=> $cmdline, opts
=> $opts, skiplist
=> $skiplist };
537 my $findexcl = $self->{findexcl
} = [];
538 if ($defaults->{'exclude-path'}) {
539 push @$findexcl, @{$defaults->{'exclude-path'}};
542 if ($opts->{'exclude-path'}) {
543 push @$findexcl, @{$opts->{'exclude-path'}};
546 if ($opts->{stdexcludes
}) {
547 push @$findexcl, '/var/log/?*',
553 foreach my $p (@plugins) {
555 my $pd = $p->new ($self);
557 push @{$self->{plugins
}}, $pd;
560 if (!$opts->{dumpdir
} && !$opts->{storage
}) {
561 $opts->{storage
} = 'local';
564 if ($opts->{storage
}) {
565 my $info = storage_info
($opts->{storage
});
566 $opts->{dumpdir
} = $info->{dumpdir
};
567 $maxfiles = $info->{maxfiles
} if !defined($maxfiles) && defined($info->{maxfiles
});
568 } elsif ($opts->{dumpdir
}) {
569 die "dumpdir '$opts->{dumpdir}' does not exist\n"
570 if ! -d
$opts->{dumpdir
};
572 die "internal error";
575 if ($opts->{tmpdir
} && ! -d
$opts->{tmpdir
}) {
576 die "tmpdir '$opts->{tmpdir}' does not exist\n";
579 $opts->{maxfiles
} = $maxfiles if defined($maxfiles);
588 # Note: df 'available' can be negative, and percentage set to '-'
590 my $cmd = [ 'df', '-P', '-T', '-B', '1', $dir];
596 if (my ($fsid, $fstype, undef, $mp) = $line =~
597 m!(\S+.*)\s+(\S+)\s+\d+\s+\-?\d+\s+\d+\s+(\d+%|-)\s+(/.*)$!) {
606 eval { PVE
::Tools
::run_command
($cmd, errfunc
=> sub {}, outfunc
=> $parser); };
613 my ($self, $upid) = @_;
617 my $maxwait = $self->{opts
}->{lockwait
} || $self->{lockwait
};
619 die "missimg UPID" if !$upid; # should not happen
621 if (!open (SERVER_FLCK
, ">>$lockfile")) {
622 debugmsg
('err', "can't open lock on file '$lockfile' - $!", undef, 1);
623 die "can't open lock on file '$lockfile' - $!";
626 if (!flock (SERVER_FLCK
, LOCK_EX
|LOCK_NB
)) {
629 debugmsg
('err', "can't aquire lock '$lockfile' (wait = 0)", undef, 1);
630 die "can't aquire lock '$lockfile' (wait = 0)";
633 debugmsg
('info', "trying to get global lock - waiting...", undef, 1);
636 alarm ($maxwait * 60);
638 local $SIG{ALRM
} = sub { alarm (0); die "got timeout\n"; };
640 if (!flock (SERVER_FLCK
, LOCK_EX
)) {
653 debugmsg
('err', "can't aquire lock '$lockfile' - $err", undef, 1);
654 die "can't aquire lock '$lockfile' - $err";
657 debugmsg
('info', "got global lock", undef, 1);
660 PVE
::Tools
::file_set_contents
($pidfile, $upid);
663 sub run_hook_script
{
664 my ($self, $phase, $task, $logfd) = @_;
666 my $opts = $self->{opts
};
668 my $script = $opts->{script
};
672 my $cmd = "$script $phase";
674 $cmd .= " $task->{mode} $task->{vmid}" if ($task);
678 # set immutable opts directly (so they are available in all phases)
679 $ENV{STOREID
} = $opts->{storage
} if $opts->{storage
};
680 $ENV{DUMPDIR
} = $opts->{dumpdir
} if $opts->{dumpdir
};
682 foreach my $ek (qw(vmtype hostname tarfile logfile)) {
683 $ENV{uc($ek)} = $task->{$ek} if $task->{$ek};
686 run_command
($logfd, $cmd);
689 sub compressor_info
{
691 my $opt_compress = $opts->{compress
};
693 if (!$opt_compress || $opt_compress eq '0') {
695 } elsif ($opt_compress eq '1' || $opt_compress eq 'lzo') {
696 return ('lzop', 'lzo');
697 } elsif ($opt_compress eq 'gzip') {
698 if ($opts->{pigz
} > 0) {
699 # As default use int((#cores + 1)/2), we need #cores+1 for the case that #cores = 1
700 my $cores = POSIX
::sysconf
(84);
701 my $pigz_threads = ($opts->{pigz
} > 1) ?
$opts->{pigz
} : int(($cores + 1)/2);
702 return ("pigz -p ${pigz_threads}", 'gz');
704 return ('gzip', 'gz');
707 die "internal error - unknown compression option '$opt_compress'";
711 sub get_backup_file_list
{
712 my ($dir, $bkname, $exclude_fn) = @_;
715 foreach my $fn (<$dir/${bkname
}-*>) {
716 next if $exclude_fn && $fn eq $exclude_fn;
717 if ($fn =~ m!/(${bkname}-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|((tar|vma)(\.(gz|lzo))?)))$!) {
718 $fn = "$dir/$1"; # untaint
719 my $t = timelocal
($7, $6, $5, $4, $3 - 1, $2 - 1900);
720 push @$bklist, [$fn, $t];
727 sub exec_backup_task
{
728 my ($self, $task) = @_;
730 my $opts = $self->{opts
};
732 my $vmid = $task->{vmid
};
733 my $plugin = $task->{plugin
};
735 my $vmstarttime = time ();
744 die "unable to find VM '$vmid'\n" if !$plugin;
746 # for now we deny backups of a running ha managed service in *stop* mode
747 # as it interferes with the HA stack (enabled services should not stop).
748 if ($opts->{mode
} eq 'stop' &&
749 PVE
::HA
::Config
::vm_is_ha_managed
($vmid, 'enabled'))
751 die "Cannot execute a backup with stop mode on a HA managed and".
752 " enabled Service. Use snapshot mode or disable the Service.\n";
755 my $vmtype = $plugin->type();
757 my $tmplog = "$logdir/$vmtype-$vmid.log";
759 my $lt = localtime();
761 my $bkname = "vzdump-$vmtype-$vmid";
762 my $basename = sprintf "${bkname}-%04d_%02d_%02d-%02d_%02d_%02d",
763 $lt->year + 1900, $lt->mon + 1, $lt->mday,
764 $lt->hour, $lt->min, $lt->sec;
766 my $maxfiles = $opts->{maxfiles
};
768 if ($maxfiles && !$opts->{remove
}) {
769 my $bklist = get_backup_file_list
($opts->{dumpdir
}, $bkname);
770 die "only $maxfiles backup(s) allowed - please consider to remove old backup files.\n"
771 if scalar(@$bklist) >= $maxfiles;
774 my $logfile = $task->{logfile
} = "$opts->{dumpdir}/$basename.log";
776 my $ext = $vmtype eq 'qemu' ?
'.vma' : '.tar';
777 my ($comp, $comp_ext) = compressor_info
($opts);
778 if ($comp && $comp_ext) {
779 $ext .= ".${comp_ext}";
782 if ($opts->{stdout
}) {
783 $task->{tarfile
} = '-';
785 my $tarfile = $task->{tarfile
} = "$opts->{dumpdir}/$basename$ext";
786 $task->{tmptar
} = $task->{tarfile
};
787 $task->{tmptar
} =~ s/\.[^\.]+$/\.dat/;
788 unlink $task->{tmptar
};
791 $task->{vmtype
} = $vmtype;
793 if ($opts->{tmpdir
}) {
794 $task->{tmpdir
} = "$opts->{tmpdir}/vzdumptmp$$";
796 # dumpdir is posix? then use it as temporary dir
797 my $info = get_mount_info
($opts->{dumpdir
});
798 if ($vmtype eq 'qemu' ||
799 grep ($_ eq $info->{fstype
}, @posix_filesystems)) {
800 $task->{tmpdir
} = "$opts->{dumpdir}/$basename.tmp";
802 $task->{tmpdir
} = "/var/tmp/vzdumptmp$$";
803 debugmsg
('info', "filesystem type on dumpdir is '$info->{fstype}' -" .
804 "using $task->{tmpdir} for temporary files", $logfd);
808 rmtree
$task->{tmpdir
};
809 mkdir $task->{tmpdir
};
810 -d
$task->{tmpdir
} ||
811 die "unable to create temporary directory '$task->{tmpdir}'";
813 $logfd = IO
::File-
>new (">$tmplog") ||
814 die "unable to create log file '$tmplog'";
816 $task->{dumpdir
} = $opts->{dumpdir
};
817 $task->{storeid
} = $opts->{storage
};
818 $task->{tmplog
} = $tmplog;
822 debugmsg
('info', "Starting Backup of VM $vmid ($vmtype)", $logfd, 1);
824 $plugin->set_logfd ($logfd);
826 # test is VM is running
827 my ($running, $status_text) = $plugin->vm_status ($vmid);
829 debugmsg
('info', "status = ${status_text}", $logfd);
831 # lock VM (prevent config changes)
832 $plugin->lock_vm ($vmid);
834 $cleanup->{unlock
} = 1;
838 my $mode = $running ?
$opts->{mode
} : 'stop';
840 if ($mode eq 'snapshot') {
841 my %saved_task = %$task;
842 eval { $plugin->prepare ($task, $vmid, $mode); };
844 die $err if $err !~ m/^mode failure/;
845 debugmsg
('info', $err, $logfd);
846 debugmsg
('info', "trying 'suspend' mode instead", $logfd);
847 $mode = 'suspend'; # so prepare is called again below
848 %$task = %saved_task;
852 $task->{mode
} = $mode;
854 debugmsg
('info', "backup mode: $mode", $logfd);
856 debugmsg
('info', "bandwidth limit: $opts->{bwlimit} KB/s", $logfd)
859 debugmsg
('info', "ionice priority: $opts->{ionice}", $logfd);
861 if ($mode eq 'stop') {
863 $plugin->prepare ($task, $vmid, $mode);
865 $self->run_hook_script ('backup-start', $task, $logfd);
868 debugmsg
('info', "stopping vm", $logfd);
869 $vmstoptime = time ();
870 $self->run_hook_script ('pre-stop', $task, $logfd);
871 $plugin->stop_vm ($task, $vmid);
872 $cleanup->{restart
} = 1;
876 } elsif ($mode eq 'suspend') {
878 $plugin->prepare ($task, $vmid, $mode);
880 $self->run_hook_script ('backup-start', $task, $logfd);
882 if ($vmtype eq 'lxc') {
884 $plugin->copy_data_phase1($task, $vmid);
887 debugmsg
('info', "suspend vm", $logfd);
888 $vmstoptime = time ();
889 $self->run_hook_script ('pre-stop', $task, $logfd);
890 $plugin->suspend_vm ($task, $vmid);
891 $cleanup->{resume
} = 1;
893 if ($vmtype eq 'lxc') {
895 $plugin->copy_data_phase2($task, $vmid);
897 debugmsg
('info', "resume vm", $logfd);
898 $cleanup->{resume
} = 0;
899 $self->run_hook_script('pre-restart', $task, $logfd);
900 $plugin->resume_vm($task, $vmid);
901 my $delay = time () - $vmstoptime;
902 debugmsg
('info', "vm is online again after $delay seconds", $logfd);
905 } elsif ($mode eq 'snapshot') {
907 $self->run_hook_script ('backup-start', $task, $logfd);
909 my $snapshot_count = $task->{snapshot_count
} || 0;
911 $self->run_hook_script ('pre-stop', $task, $logfd);
913 if ($snapshot_count > 1) {
914 debugmsg
('info', "suspend vm to make snapshot", $logfd);
915 $vmstoptime = time ();
916 $plugin->suspend_vm ($task, $vmid);
917 $cleanup->{resume
} = 1;
920 $plugin->snapshot ($task, $vmid);
922 $self->run_hook_script ('pre-restart', $task, $logfd);
924 if ($snapshot_count > 1) {
925 debugmsg
('info', "resume vm", $logfd);
926 $cleanup->{resume
} = 0;
927 $plugin->resume_vm ($task, $vmid);
928 my $delay = time () - $vmstoptime;
929 debugmsg
('info', "vm is online again after $delay seconds", $logfd);
933 die "internal error - unknown mode '$mode'\n";
936 # assemble archive image
937 $plugin->assemble ($task, $vmid);
941 if ($opts->{stdout
}) {
942 debugmsg
('info', "sending archive to stdout", $logfd);
943 $plugin->archive($task, $vmid, $task->{tmptar
}, $comp);
944 $self->run_hook_script ('backup-end', $task, $logfd);
948 debugmsg
('info', "creating archive '$task->{tarfile}'", $logfd);
949 $plugin->archive($task, $vmid, $task->{tmptar
}, $comp);
951 rename ($task->{tmptar
}, $task->{tarfile
}) ||
952 die "unable to rename '$task->{tmptar}' to '$task->{tarfile}'\n";
955 $task->{size
} = (-s
$task->{tarfile
}) || 0;
956 my $cs = format_size
($task->{size
});
957 debugmsg
('info', "archive file size: $cs", $logfd);
961 if ($maxfiles && $opts->{remove
}) {
962 my $bklist = get_backup_file_list
($opts->{dumpdir
}, $bkname, $task->{tarfile
});
963 $bklist = [ sort { $b->[1] <=> $a->[1] } @$bklist ];
965 while (scalar (@$bklist) >= $maxfiles) {
966 my $d = pop @$bklist;
967 debugmsg
('info', "delete old backup '$d->[0]'", $logfd);
970 $logfn =~ s/\.(tgz|((tar|vma)(\.(gz|lzo))?))$/\.log/;
975 $self->run_hook_script ('backup-end', $task, $logfd);
982 if ($cleanup->{unlock
}) {
983 eval { $plugin->unlock_vm ($vmid); };
987 if (defined($task->{mode
})) {
988 # only call cleanup when necessary (when prepare was executed)
989 eval { $plugin->cleanup ($task, $vmid) };
993 eval { $plugin->set_logfd (undef); };
996 if ($cleanup->{resume
} || $cleanup->{restart
}) {
998 $self->run_hook_script ('pre-restart', $task, $logfd);
999 if ($cleanup->{resume
}) {
1000 debugmsg
('info', "resume vm", $logfd);
1001 $plugin->resume_vm ($task, $vmid);
1003 my $running = $plugin->vm_status($vmid);
1005 debugmsg
('info', "restarting vm", $logfd);
1006 $plugin->start_vm ($task, $vmid);
1014 my $delay = time () - $vmstoptime;
1015 debugmsg
('info', "vm is online again after $delay seconds", $logfd);
1020 eval { unlink $task->{tmptar
} if $task->{tmptar
} && -f
$task->{tmptar
}; };
1023 eval { rmtree
$task->{tmpdir
} if $task->{tmpdir
} && -d
$task->{tmpdir
}; };
1026 my $delay = $task->{backuptime
} = time () - $vmstarttime;
1029 $task->{state} = 'err';
1030 $task->{msg
} = $err;
1031 debugmsg
('err', "Backup of VM $vmid failed - $err", $logfd, 1);
1033 eval { $self->run_hook_script ('backup-abort', $task, $logfd); };
1036 $task->{state} = 'ok';
1037 my $tstr = format_time
($delay);
1038 debugmsg
('info', "Finished Backup of VM $vmid ($tstr)", $logfd, 1);
1041 close ($logfd) if $logfd;
1043 if ($task->{tmplog
} && $task->{logfile
}) {
1044 system ("cp '$task->{tmplog}' '$task->{logfile}'");
1047 eval { $self->run_hook_script ('log-end', $task); };
1049 die $err if $err && $err =~ m/^interrupted by signal$/;
1053 my ($self, $rpcenv, $authuser) = @_;
1055 my $opts = $self->{opts
};
1057 debugmsg
('info', "starting new backup job: $self->{cmdline}", undef, 1);
1058 debugmsg
('info', "skip external VMs: " . join(', ', @{$self->{skiplist
}}))
1059 if scalar(@{$self->{skiplist
}});
1064 foreach my $plugin (@{$self->{plugins
}}) {
1065 my $vmlist = $plugin->vmlist();
1066 foreach my $vmid (sort @$vmlist) {
1067 next if grep { $_ eq $vmid } @{$opts->{exclude
}};
1068 next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Backup' ], 1);
1069 push @$tasklist, { vmid
=> $vmid, state => 'todo', plugin
=> $plugin };
1073 foreach my $vmid (sort @{$opts->{vmids
}}) {
1075 foreach my $pg (@{$self->{plugins
}}) {
1076 my $vmlist = $pg->vmlist();
1077 if (grep { $_ eq $vmid } @$vmlist) {
1082 $rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Backup' ]);
1083 push @$tasklist, { vmid
=> $vmid, state => 'todo', plugin
=> $plugin };
1087 my $starttime = time();
1091 $self->run_hook_script ('job-start');
1093 foreach my $task (@$tasklist) {
1094 $self->exec_backup_task ($task);
1095 $errcount += 1 if $task->{state} ne 'ok';
1098 $self->run_hook_script ('job-end');
1102 $self->run_hook_script ('job-abort') if $err;
1105 debugmsg
('err', "Backup job failed - $err", undef, 1);
1108 debugmsg
('info', "Backup job finished with errors", undef, 1);
1110 debugmsg
('info', "Backup job finished successfully", undef, 1);
1114 my $totaltime = time() - $starttime;
1116 eval { $self->sendmail ($tasklist, $totaltime); };
1117 debugmsg
('err', $@) if $@;
1121 die "job errors\n" if $errcount;
1129 return defined($confdesc->{$key});
1132 # add JSON properties for create and set function
1133 sub json_config_properties
{
1136 foreach my $opt (keys %$confdesc) {
1137 $prop->{$opt} = $confdesc->{$opt};
1143 sub verify_vzdump_parameters
{
1144 my ($param, $check_missing) = @_;
1146 raise_param_exc
({ all
=> "option conflicts with option 'vmid'"})
1147 if $param->{all
} && $param->{vmid
};
1149 raise_param_exc
({ exclude
=> "option conflicts with option 'vmid'"})
1150 if $param->{exclude
} && $param->{vmid
};
1152 $param->{all
} = 1 if defined($param->{exclude
});
1154 warn "option 'size' is deprecated and will be removed in a future " .
1155 "release, please update your script/configuration!\n"
1156 if defined($param->{size
});
1158 return if !$check_missing;
1160 raise_param_exc
({ vmid
=> "property is missing"})
1161 if !($param->{all
} || $param->{stop
}) && !$param->{vmid
};
1165 sub stop_running_backups
{
1168 my $upid = PVE
::Tools
::file_read_firstline
($pidfile);
1171 my $task = PVE
::Tools
::upid_decode
($upid);
1173 if (PVE
::ProcFSTools
::check_process_running
($task->{pid
}, $task->{pstart
}) &&
1174 PVE
::ProcFSTools
::read_proc_starttime
($task->{pid
}) == $task->{pstart
}) {
1175 kill(15, $task->{pid
});
1176 # wait max 15 seconds to shut down (else, do nothing for now)
1178 for ($i = 15; $i > 0; $i--) {
1179 last if !PVE
::ProcFSTools
::check_process_running
(($task->{pid
}, $task->{pstart
}));
1182 die "stoping backup process $task->{pid} failed\n" if $i == 0;
1191 if ($param->{vmid
}) {
1192 $cmd .= " " . join(' ', PVE
::Tools
::split_list
($param->{vmid
}));
1195 foreach my $p (keys %$param) {
1196 next if $p eq 'id' || $p eq 'vmid' || $p eq 'starttime' ||
1197 $p eq 'dow' || $p eq 'stdout' || $p eq 'enabled';
1198 my $v = $param->{$p};
1199 my $pd = $confdesc->{$p} || die "no such vzdump option '$p'\n";
1200 if ($p eq 'exclude-path') {
1201 foreach my $path (split(/\0/, $v || '')) {
1202 $cmd .= " --$p " . PVE
::Tools
::shellquote
($path);
1205 $cmd .= " --$p " . PVE
::Tools
::shellquote
($v) if defined($v) && $v ne '';
1212 # bash completion helpers
1213 sub complete_backup_storage
{
1215 my $cfg = PVE
::Storage
::config
();
1216 my $ids = $cfg->{ids
};
1218 my $nodename = PVE
::INotify
::nodename
();
1221 foreach my $sid (keys %$ids) {
1222 my $scfg = $ids->{$sid};
1223 next if !PVE
::Storage
::storage_check_enabled
($cfg, $sid, $nodename, 1);
1224 next if !$scfg->{content
}->{backup
};