]>
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',
95 description
=> "Comma-separated list of email addresses that should" .
96 " receive email notifications.",
101 description
=> "Specify when to send an email",
103 enum
=> [ 'always', 'failure' ],
108 description
=> "Store temporary files to specified directory.",
113 description
=> "Store resulting files to specified directory.",
118 description
=> "Use specified hook script.",
121 storage
=> get_standard_option
('pve-storage-id', {
122 description
=> "Store resulting file to this storage.",
123 completion
=> \
&complete_backup_storage
,
128 description
=> "Stop runnig backup jobs on this host.",
134 description
=> "Unused, will be removed in a future release.",
141 description
=> "Limit I/O bandwidth (KBytes per second).",
148 description
=> "Set CFQ ionice priority.",
156 description
=> "Maximal time to wait for the global lock (minutes).",
159 default => 3*60, # 3 hours
163 description
=> "Maximal time to wait until a guest system is stopped (minutes).",
166 default => 10, # 10 minutes
170 description
=> "Maximal number of backup files per guest system.",
177 description
=> "Remove old backup files if there are more than 'maxfiles' backup files.",
183 # Load available plugins
184 my @pve_vzdump_classes = qw(PVE::VZDump::QemuServer PVE::VZDump::LXC);
185 foreach my $plug (@pve_vzdump_classes) {
186 my $filename = "/usr/share/perl5/$plug.pm";
187 $filename =~ s!::!/!g;
189 eval { require $filename; };
192 push @plugins, $plug;
208 my ($mtype, $msg, $logfd, $syslog) = @_;
214 my $pre = $debugstattxt->{$mtype} || $debugstattxt->{'err'};
216 my $timestr = strftime
("%F %H:%M:%S", CORE
::localtime);
218 syslog
($mtype eq 'info' ?
'info' : 'err', "$pre $msg") if $syslog;
220 foreach my $line (split (/\n/, $msg)) {
221 print STDERR
"$pre $line\n";
222 print $logfd "$timestr $pre $line\n" if $logfd;
227 my ($logfd, $cmdstr, %param) = @_;
231 debugmsg
('info', $line, $logfd);
234 PVE
::Tools
::run_command
($cmdstr, %param, logfunc
=> $logfunc);
240 my $cfg = PVE
::Storage
::config
();
241 my $scfg = PVE
::Storage
::storage_config
($cfg, $storage);
242 my $type = $scfg->{type
};
244 die "can't use storage type '$type' for backup\n"
245 if (!($type eq 'dir' || $type eq 'nfs' || $type eq 'glusterfs'));
246 die "can't use storage '$storage' for backups - wrong content type\n"
247 if (!$scfg->{content
}->{backup
});
249 PVE
::Storage
::activate_storage
($cfg, $storage);
252 dumpdir
=> PVE
::Storage
::get_backup_dir
($cfg, $storage),
253 maxfiles
=> $scfg->{maxfiles
},
260 my $kb = $size / 1024;
263 return int ($kb) . "KB";
266 my $mb = $size / (1024*1024);
269 return int ($mb) . "MB";
272 return sprintf ("%.2fGB", $gb);
279 my $hours = int ($seconds/3600);
280 $seconds = $seconds - $hours*3600;
281 my $min = int ($seconds/60);
282 $seconds = $seconds - $min*60;
284 return sprintf ("%02d:%02d:%02d", $hours, $min, $seconds);
290 $str =~ s/^(.{990})/$1\n/mg; # reduce line length
308 foreach my $p (split (/:/, $ENV{PATH
})) {
315 die "unable to find command '$bin'\n";
322 foreach my $vmid (@vmids) {
323 die "ERROR: strange VM ID '${vmid}'\n" if $vmid !~ m/^\d+$/;
324 $vmid = int ($vmid); # remove leading zeros
333 sub read_vzdump_defaults
{
335 my $fn = "/etc/vzdump.conf";
339 my $default = $confdesc->{$_}->{default};
340 defined($default) ?
($_ => $default) : ()
345 eval { $raw = PVE
::Tools
::file_get_contents
($fn); };
346 return $defaults if $@;
348 my $conf_schema = { type
=> 'object', properties
=> $confdesc, };
349 my $res = PVE
::JSONSchema
::parse_config
($conf_schema, $fn, $raw);
350 if (my $excludes = $res->{'exclude-path'}) {
351 $res->{'exclude-path'} = PVE
::Tools
::split_args
($excludes);
353 if (defined($res->{mailto
})) {
354 my @mailto = PVE
::Tools
::split_list
($res->{mailto
});
355 $res->{mailto
} = [ @mailto ];
358 foreach my $key (keys %$defaults) {
359 $res->{$key} = $defaults->{$key} if !defined($res->{$key});
366 my ($self, $tasklist, $totaltime, $err, $detail_pre, $detail_post) = @_;
368 my $opts = $self->{opts
};
370 my $mailto = $opts->{mailto
};
372 return if !($mailto && scalar(@$mailto));
374 my $cmdline = $self->{cmdline
};
377 foreach my $task (@$tasklist) {
378 $ecount++ if $task->{state} ne 'ok';
379 chomp $task->{msg
} if $task->{msg
};
380 $task->{backuptime
} = 0 if !$task->{backuptime
};
381 $task->{size
} = 0 if !$task->{size
};
382 $task->{tarfile
} = 'unknown' if !$task->{tarfile
};
383 $task->{hostname
} = "VM $task->{vmid}" if !$task->{hostname
};
385 if ($task->{state} eq 'todo') {
386 $task->{msg
} = 'aborted';
390 my $notify = $opts->{mailnotification
} || 'always';
391 return if (!$ecount && !$err && ($notify eq 'failure'));
393 my $stat = ($ecount || $err) ?
'backup failed' : 'backup successful';
396 $stat .= ": multiple problems";
403 my $hostname = `hostname -f` || PVE
::INotify
::nodename
();
407 my $text = $err ?
"$err\n\n" : '';
408 $text .= sprintf ("%-10s %-6s %10s %10s %s\n", qw(VMID STATUS TIME SIZE FILENAME));
409 foreach my $task (@$tasklist) {
410 my $vmid = $task->{vmid
};
411 if ($task->{state} eq 'ok') {
413 $text .= sprintf ("%-10s %-6s %10s %10s %s\n", $vmid,
415 format_time
($task->{backuptime
}),
416 format_size
($task->{size
}),
419 $text .= sprintf ("%-10s %-6s %10s %8.2fMB %s\n", $vmid,
421 format_time
($task->{backuptime
}),
426 $text .= "\nDetailed backup logs:\n\n";
427 $text .= "$cmdline\n\n";
429 $text .= $detail_pre . "\n" if defined($detail_pre);
430 foreach my $task (@$tasklist) {
431 my $vmid = $task->{vmid
};
432 my $log = $task->{tmplog
};
434 $text .= "$vmid: no log available\n\n";
438 while (my $line = <TMP
>) { $text .= encode8bit
("$vmid: $line"); }
442 $text .= $detail_post if defined($detail_post);
445 my $html = "<html><body>\n";
446 $html .= "<p>" . (escape_html
($err) =~ s/\n/<br>/gr) . "</p>\n" if $err;
447 $html .= "<table border=1 cellpadding=3>\n";
448 $html .= "<tr><td>VMID<td>NAME<td>STATUS<td>TIME<td>SIZE<td>FILENAME</tr>\n";
452 foreach my $task (@$tasklist) {
453 my $vmid = $task->{vmid
};
454 my $name = $task->{hostname
};
456 if ($task->{state} eq 'ok') {
458 $ssize += $task->{size
};
460 $html .= sprintf ("<tr><td>%s<td>%s<td>OK<td>%s<td align=right>%s<td>%s</tr>\n",
462 format_time
($task->{backuptime
}),
463 format_size
($task->{size
}),
464 escape_html
($task->{tarfile
}));
466 $html .= sprintf ("<tr><td>%s<td>%s<td><font color=red>FAILED<td>%s<td colspan=2>%s</tr>\n",
467 $vmid, $name, format_time
($task->{backuptime
}),
468 escape_html
($task->{msg
}));
472 $html .= sprintf ("<tr><td align=left colspan=3>TOTAL<td>%s<td>%s<td></tr>",
473 format_time
($totaltime), format_size
($ssize));
475 $html .= "</table><br><br>\n";
476 $html .= "Detailed backup logs:<br /><br />\n";
478 $html .= escape_html
($cmdline) . "\n\n";
480 $html .= escape_html
($detail_pre) . "\n" if defined($detail_pre);
481 foreach my $task (@$tasklist) {
482 my $vmid = $task->{vmid
};
483 my $log = $task->{tmplog
};
485 $html .= "$vmid: no log available\n\n";
489 while (my $line = <TMP
>) {
490 if ($line =~ m/^\S+\s\d+\s+\d+:\d+:\d+\s+(ERROR|WARN):/) {
491 $html .= encode8bit
("$vmid: <font color=red>".
492 escape_html
($line) . "</font>");
494 $html .= encode8bit
("$vmid: " . escape_html
($line));
500 $html .= escape_html
($detail_post) if defined($detail_post);
501 $html .= "</pre></body></html>\n";
504 my $subject = "vzdump backup status ($hostname) : $stat";
506 my $dcconf = PVE
::Cluster
::cfs_read_file
('datacenter.cfg');
507 my $mailfrom = $dcconf->{email_from
} || "root";
509 PVE
::Tools
::sendmail
($mailto, $subject, $text, $html, $mailfrom, "vzdump backup tool");
513 my ($class, $cmdline, $opts, $skiplist) = @_;
519 check_bin
('sendmail');
523 check_bin
('umount');
524 check_bin
('cstream');
525 check_bin
('ionice');
527 if ($opts->{mode
} && $opts->{mode
} eq 'snapshot') {
528 check_bin
('lvcreate');
530 check_bin
('lvremove');
533 my $defaults = read_vzdump_defaults
();
535 my $maxfiles = $opts->{maxfiles
}; # save here, because we overwrite with default
537 $opts->{remove
} = 1 if !defined($opts->{remove
});
539 foreach my $k (keys %$defaults) {
540 next if $k eq 'exclude-path'; # dealt with separately
541 if ($k eq 'dumpdir' || $k eq 'storage') {
542 $opts->{$k} = $defaults->{$k} if !defined ($opts->{dumpdir
}) &&
543 !defined ($opts->{storage
});
545 $opts->{$k} = $defaults->{$k} if !defined ($opts->{$k});
549 $opts->{dumpdir
} =~ s
|/+$|| if ($opts->{dumpdir
});
550 $opts->{tmpdir
} =~ s
|/+$|| if ($opts->{tmpdir
});
552 $skiplist = [] if !$skiplist;
553 my $self = bless { cmdline
=> $cmdline, opts
=> $opts, skiplist
=> $skiplist };
555 my $findexcl = $self->{findexcl
} = [];
556 if ($defaults->{'exclude-path'}) {
557 push @$findexcl, @{$defaults->{'exclude-path'}};
560 if ($opts->{'exclude-path'}) {
561 push @$findexcl, @{$opts->{'exclude-path'}};
564 if ($opts->{stdexcludes
}) {
565 push @$findexcl, '/tmp/?*',
570 foreach my $p (@plugins) {
572 my $pd = $p->new ($self);
574 push @{$self->{plugins
}}, $pd;
577 if (!$opts->{dumpdir
} && !$opts->{storage
}) {
578 $opts->{storage
} = 'local';
583 if ($opts->{storage
}) {
584 my $info = storage_info
($opts->{storage
});
585 $opts->{dumpdir
} = $info->{dumpdir
};
586 $maxfiles = $info->{maxfiles
} if !defined($maxfiles) && defined($info->{maxfiles
});
587 } elsif ($opts->{dumpdir
}) {
588 $errors .= "dumpdir '$opts->{dumpdir}' does not exist"
589 if ! -d
$opts->{dumpdir
};
591 die "internal error";
594 if ($opts->{tmpdir
} && ! -d
$opts->{tmpdir
}) {
595 $errors .= "\n" if $errors;
596 $errors .= "tmpdir '$opts->{tmpdir}' does not exist";
600 eval { $self->sendmail([], 0, $errors); };
601 debugmsg
('err', $@) if $@;
605 $opts->{maxfiles
} = $maxfiles if defined($maxfiles);
614 # Note: df 'available' can be negative, and percentage set to '-'
616 my $cmd = [ 'df', '-P', '-T', '-B', '1', $dir];
622 if (my ($fsid, $fstype, undef, $mp) = $line =~
623 m!(\S+.*)\s+(\S+)\s+\d+\s+\-?\d+\s+\d+\s+(\d+%|-)\s+(/.*)$!) {
632 eval { PVE
::Tools
::run_command
($cmd, errfunc
=> sub {}, outfunc
=> $parser); };
639 my ($self, $upid) = @_;
643 my $maxwait = $self->{opts
}->{lockwait
} || $self->{lockwait
};
645 die "missimg UPID" if !$upid; # should not happen
647 if (!open (SERVER_FLCK
, ">>$lockfile")) {
648 debugmsg
('err', "can't open lock on file '$lockfile' - $!", undef, 1);
649 die "can't open lock on file '$lockfile' - $!";
652 if (!flock (SERVER_FLCK
, LOCK_EX
|LOCK_NB
)) {
655 debugmsg
('err', "can't aquire lock '$lockfile' (wait = 0)", undef, 1);
656 die "can't aquire lock '$lockfile' (wait = 0)";
659 debugmsg
('info', "trying to get global lock - waiting...", undef, 1);
662 alarm ($maxwait * 60);
664 local $SIG{ALRM
} = sub { alarm (0); die "got timeout\n"; };
666 if (!flock (SERVER_FLCK
, LOCK_EX
)) {
679 debugmsg
('err', "can't aquire lock '$lockfile' - $err", undef, 1);
680 die "can't aquire lock '$lockfile' - $err";
683 debugmsg
('info', "got global lock", undef, 1);
686 PVE
::Tools
::file_set_contents
($pidfile, $upid);
689 sub run_hook_script
{
690 my ($self, $phase, $task, $logfd) = @_;
692 my $opts = $self->{opts
};
694 my $script = $opts->{script
};
698 my $cmd = "$script $phase";
700 $cmd .= " $task->{mode} $task->{vmid}" if ($task);
704 # set immutable opts directly (so they are available in all phases)
705 $ENV{STOREID
} = $opts->{storage
} if $opts->{storage
};
706 $ENV{DUMPDIR
} = $opts->{dumpdir
} if $opts->{dumpdir
};
708 foreach my $ek (qw(vmtype hostname tarfile logfile)) {
709 $ENV{uc($ek)} = $task->{$ek} if $task->{$ek};
712 run_command
($logfd, $cmd);
715 sub compressor_info
{
717 my $opt_compress = $opts->{compress
};
719 if (!$opt_compress || $opt_compress eq '0') {
721 } elsif ($opt_compress eq '1' || $opt_compress eq 'lzo') {
722 return ('lzop', 'lzo');
723 } elsif ($opt_compress eq 'gzip') {
724 if ($opts->{pigz
} > 0) {
725 # As default use int((#cores + 1)/2), we need #cores+1 for the case that #cores = 1
726 my $cores = POSIX
::sysconf
(84);
727 my $pigz_threads = ($opts->{pigz
} > 1) ?
$opts->{pigz
} : int(($cores + 1)/2);
728 return ("pigz -p ${pigz_threads}", 'gz');
730 return ('gzip', 'gz');
733 die "internal error - unknown compression option '$opt_compress'";
737 sub get_backup_file_list
{
738 my ($dir, $bkname, $exclude_fn) = @_;
741 foreach my $fn (<$dir/${bkname
}-*>) {
742 next if $exclude_fn && $fn eq $exclude_fn;
743 if ($fn =~ m!/(${bkname}-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|((tar|vma)(\.(gz|lzo))?)))$!) {
744 $fn = "$dir/$1"; # untaint
745 my $t = timelocal
($7, $6, $5, $4, $3 - 1, $2 - 1900);
746 push @$bklist, [$fn, $t];
753 sub exec_backup_task
{
754 my ($self, $task) = @_;
756 my $opts = $self->{opts
};
758 my $vmid = $task->{vmid
};
759 my $plugin = $task->{plugin
};
761 my $vmstarttime = time ();
770 die "unable to find VM '$vmid'\n" if !$plugin;
772 # for now we deny backups of a running ha managed service in *stop* mode
773 # as it interferes with the HA stack (started services should not stop).
774 if ($opts->{mode
} eq 'stop' &&
775 PVE
::HA
::Config
::vm_is_ha_managed
($vmid, 'started'))
777 die "Cannot execute a backup with stop mode on a HA managed and".
778 " enabled Service. Use snapshot mode or disable the Service.\n";
781 my $vmtype = $plugin->type();
783 my $tmplog = "$logdir/$vmtype-$vmid.log";
785 my $lt = localtime();
787 my $bkname = "vzdump-$vmtype-$vmid";
788 my $basename = sprintf "${bkname}-%04d_%02d_%02d-%02d_%02d_%02d",
789 $lt->year + 1900, $lt->mon + 1, $lt->mday,
790 $lt->hour, $lt->min, $lt->sec;
792 my $maxfiles = $opts->{maxfiles
};
794 if ($maxfiles && !$opts->{remove
}) {
795 my $bklist = get_backup_file_list
($opts->{dumpdir
}, $bkname);
796 die "only $maxfiles backup(s) allowed - please consider to remove old backup files.\n"
797 if scalar(@$bklist) >= $maxfiles;
800 my $logfile = $task->{logfile
} = "$opts->{dumpdir}/$basename.log";
802 my $ext = $vmtype eq 'qemu' ?
'.vma' : '.tar';
803 my ($comp, $comp_ext) = compressor_info
($opts);
804 if ($comp && $comp_ext) {
805 $ext .= ".${comp_ext}";
808 if ($opts->{stdout
}) {
809 $task->{tarfile
} = '-';
811 my $tarfile = $task->{tarfile
} = "$opts->{dumpdir}/$basename$ext";
812 $task->{tmptar
} = $task->{tarfile
};
813 $task->{tmptar
} =~ s/\.[^\.]+$/\.dat/;
814 unlink $task->{tmptar
};
817 $task->{vmtype
} = $vmtype;
819 if ($opts->{tmpdir
}) {
820 $task->{tmpdir
} = "$opts->{tmpdir}/vzdumptmp$$";
822 # dumpdir is posix? then use it as temporary dir
823 my $info = get_mount_info
($opts->{dumpdir
});
824 if ($vmtype eq 'qemu' ||
825 grep ($_ eq $info->{fstype
}, @posix_filesystems)) {
826 $task->{tmpdir
} = "$opts->{dumpdir}/$basename.tmp";
828 $task->{tmpdir
} = "/var/tmp/vzdumptmp$$";
829 debugmsg
('info', "filesystem type on dumpdir is '$info->{fstype}' -" .
830 "using $task->{tmpdir} for temporary files", $logfd);
834 rmtree
$task->{tmpdir
};
835 mkdir $task->{tmpdir
};
836 -d
$task->{tmpdir
} ||
837 die "unable to create temporary directory '$task->{tmpdir}'";
839 $logfd = IO
::File-
>new (">$tmplog") ||
840 die "unable to create log file '$tmplog'";
842 $task->{dumpdir
} = $opts->{dumpdir
};
843 $task->{storeid
} = $opts->{storage
};
844 $task->{tmplog
} = $tmplog;
848 debugmsg
('info', "Starting Backup of VM $vmid ($vmtype)", $logfd, 1);
850 $plugin->set_logfd ($logfd);
852 # test is VM is running
853 my ($running, $status_text) = $plugin->vm_status ($vmid);
855 debugmsg
('info', "status = ${status_text}", $logfd);
857 # lock VM (prevent config changes)
858 $plugin->lock_vm ($vmid);
860 $cleanup->{unlock
} = 1;
864 my $mode = $running ?
$task->{mode
} : 'stop';
866 if ($mode eq 'snapshot') {
867 my %saved_task = %$task;
868 eval { $plugin->prepare ($task, $vmid, $mode); };
870 die $err if $err !~ m/^mode failure/;
871 debugmsg
('info', $err, $logfd);
872 debugmsg
('info', "trying 'suspend' mode instead", $logfd);
873 $mode = 'suspend'; # so prepare is called again below
874 %$task = %saved_task;
878 $cleanup->{prepared
} = 1;
880 $task->{mode
} = $mode;
882 debugmsg
('info', "backup mode: $mode", $logfd);
884 debugmsg
('info', "bandwidth limit: $opts->{bwlimit} KB/s", $logfd)
887 debugmsg
('info', "ionice priority: $opts->{ionice}", $logfd);
889 if ($mode eq 'stop') {
891 $plugin->prepare ($task, $vmid, $mode);
893 $self->run_hook_script ('backup-start', $task, $logfd);
896 debugmsg
('info', "stopping vm", $logfd);
897 $vmstoptime = time ();
898 $self->run_hook_script ('pre-stop', $task, $logfd);
899 $plugin->stop_vm ($task, $vmid);
900 $cleanup->{restart
} = 1;
904 } elsif ($mode eq 'suspend') {
906 $plugin->prepare ($task, $vmid, $mode);
908 $self->run_hook_script ('backup-start', $task, $logfd);
910 if ($vmtype eq 'lxc') {
912 $plugin->copy_data_phase1($task, $vmid);
915 debugmsg
('info', "suspend vm", $logfd);
916 $vmstoptime = time ();
917 $self->run_hook_script ('pre-stop', $task, $logfd);
918 $plugin->suspend_vm ($task, $vmid);
919 $cleanup->{resume
} = 1;
921 if ($vmtype eq 'lxc') {
923 $plugin->copy_data_phase2($task, $vmid);
925 debugmsg
('info', "resume vm", $logfd);
926 $cleanup->{resume
} = 0;
927 $self->run_hook_script('pre-restart', $task, $logfd);
928 $plugin->resume_vm($task, $vmid);
929 $self->run_hook_script('post-restart', $task, $logfd);
930 my $delay = time () - $vmstoptime;
931 debugmsg
('info', "vm is online again after $delay seconds", $logfd);
934 } elsif ($mode eq 'snapshot') {
936 $self->run_hook_script ('backup-start', $task, $logfd);
938 my $snapshot_count = $task->{snapshot_count
} || 0;
940 $self->run_hook_script ('pre-stop', $task, $logfd);
942 if ($snapshot_count > 1) {
943 debugmsg
('info', "suspend vm to make snapshot", $logfd);
944 $vmstoptime = time ();
945 $plugin->suspend_vm ($task, $vmid);
946 $cleanup->{resume
} = 1;
949 $plugin->snapshot ($task, $vmid);
951 $self->run_hook_script ('pre-restart', $task, $logfd);
953 if ($snapshot_count > 1) {
954 debugmsg
('info', "resume vm", $logfd);
955 $cleanup->{resume
} = 0;
956 $plugin->resume_vm ($task, $vmid);
957 my $delay = time () - $vmstoptime;
958 debugmsg
('info', "vm is online again after $delay seconds", $logfd);
961 $self->run_hook_script ('post-restart', $task, $logfd);
964 die "internal error - unknown mode '$mode'\n";
967 # assemble archive image
968 $plugin->assemble ($task, $vmid);
972 if ($opts->{stdout
}) {
973 debugmsg
('info', "sending archive to stdout", $logfd);
974 $plugin->archive($task, $vmid, $task->{tmptar
}, $comp);
975 $self->run_hook_script ('backup-end', $task, $logfd);
979 debugmsg
('info', "creating archive '$task->{tarfile}'", $logfd);
980 $plugin->archive($task, $vmid, $task->{tmptar
}, $comp);
982 rename ($task->{tmptar
}, $task->{tarfile
}) ||
983 die "unable to rename '$task->{tmptar}' to '$task->{tarfile}'\n";
986 $task->{size
} = (-s
$task->{tarfile
}) || 0;
987 my $cs = format_size
($task->{size
});
988 debugmsg
('info', "archive file size: $cs", $logfd);
992 if ($maxfiles && $opts->{remove
}) {
993 my $bklist = get_backup_file_list
($opts->{dumpdir
}, $bkname, $task->{tarfile
});
994 $bklist = [ sort { $b->[1] <=> $a->[1] } @$bklist ];
996 while (scalar (@$bklist) >= $maxfiles) {
997 my $d = pop @$bklist;
998 debugmsg
('info', "delete old backup '$d->[0]'", $logfd);
1000 my $logfn = $d->[0];
1001 $logfn =~ s/\.(tgz|((tar|vma)(\.(gz|lzo))?))$/\.log/;
1006 $self->run_hook_script ('backup-end', $task, $logfd);
1013 if ($cleanup->{unlock
}) {
1014 eval { $plugin->unlock_vm ($vmid); };
1018 if ($cleanup->{prepared
}) {
1019 # only call cleanup when necessary (when prepare was executed)
1020 eval { $plugin->cleanup ($task, $vmid) };
1024 eval { $plugin->set_logfd (undef); };
1027 if ($cleanup->{resume
} || $cleanup->{restart
}) {
1029 $self->run_hook_script ('pre-restart', $task, $logfd);
1030 if ($cleanup->{resume
}) {
1031 debugmsg
('info', "resume vm", $logfd);
1032 $plugin->resume_vm ($task, $vmid);
1034 my $running = $plugin->vm_status($vmid);
1036 debugmsg
('info', "restarting vm", $logfd);
1037 $plugin->start_vm ($task, $vmid);
1040 $self->run_hook_script ('post-restart', $task, $logfd);
1046 my $delay = time () - $vmstoptime;
1047 debugmsg
('info', "vm is online again after $delay seconds", $logfd);
1052 eval { unlink $task->{tmptar
} if $task->{tmptar
} && -f
$task->{tmptar
}; };
1055 eval { rmtree
$task->{tmpdir
} if $task->{tmpdir
} && -d
$task->{tmpdir
}; };
1058 my $delay = $task->{backuptime
} = time () - $vmstarttime;
1061 $task->{state} = 'err';
1062 $task->{msg
} = $err;
1063 debugmsg
('err', "Backup of VM $vmid failed - $err", $logfd, 1);
1065 eval { $self->run_hook_script ('backup-abort', $task, $logfd); };
1068 $task->{state} = 'ok';
1069 my $tstr = format_time
($delay);
1070 debugmsg
('info', "Finished Backup of VM $vmid ($tstr)", $logfd, 1);
1073 close ($logfd) if $logfd;
1075 if ($task->{tmplog
} && $task->{logfile
}) {
1076 system {'cp'} 'cp', $task->{tmplog
}, $task->{logfile
};
1079 eval { $self->run_hook_script ('log-end', $task); };
1081 die $err if $err && $err =~ m/^interrupted by signal$/;
1085 my ($self, $rpcenv, $authuser) = @_;
1087 my $opts = $self->{opts
};
1089 debugmsg
('info', "starting new backup job: $self->{cmdline}", undef, 1);
1090 debugmsg
('info', "skip external VMs: " . join(', ', @{$self->{skiplist
}}))
1091 if scalar(@{$self->{skiplist
}});
1096 foreach my $plugin (@{$self->{plugins
}}) {
1097 my $vmlist = $plugin->vmlist();
1098 foreach my $vmid (sort @$vmlist) {
1099 next if grep { $_ eq $vmid } @{$opts->{exclude
}};
1100 next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Backup' ], 1);
1101 push @$tasklist, { vmid
=> $vmid, state => 'todo', plugin
=> $plugin, mode
=> $opts->{mode
} };
1105 foreach my $vmid (sort @{$opts->{vmids
}}) {
1107 foreach my $pg (@{$self->{plugins
}}) {
1108 my $vmlist = $pg->vmlist();
1109 if (grep { $_ eq $vmid } @$vmlist) {
1114 $rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Backup' ]);
1115 push @$tasklist, { vmid
=> $vmid, state => 'todo', plugin
=> $plugin, mode
=> $opts->{mode
} };
1119 # Use in-memory files for the outer hook logs to pass them to sendmail.
1120 my $job_start_log = '';
1121 my $job_end_log = '';
1122 open my $job_start_fd, '>', \
$job_start_log;
1123 open my $job_end_fd, '>', \
$job_end_log;
1125 my $starttime = time();
1129 $self->run_hook_script ('job-start', undef, $job_start_fd);
1131 foreach my $task (@$tasklist) {
1132 $self->exec_backup_task ($task);
1133 $errcount += 1 if $task->{state} ne 'ok';
1136 $self->run_hook_script ('job-end', undef, $job_end_fd);
1140 $self->run_hook_script ('job-abort', undef, $job_end_fd) if $err;
1143 debugmsg
('err', "Backup job failed - $err", undef, 1);
1146 debugmsg
('info', "Backup job finished with errors", undef, 1);
1148 debugmsg
('info', "Backup job finished successfully", undef, 1);
1152 close $job_start_fd;
1155 my $totaltime = time() - $starttime;
1157 eval { $self->sendmail ($tasklist, $totaltime, undef, $job_start_log, $job_end_log); };
1158 debugmsg
('err', $@) if $@;
1162 die "job errors\n" if $errcount;
1170 return defined($confdesc->{$key});
1173 # add JSON properties for create and set function
1174 sub json_config_properties
{
1177 foreach my $opt (keys %$confdesc) {
1178 $prop->{$opt} = $confdesc->{$opt};
1184 sub verify_vzdump_parameters
{
1185 my ($param, $check_missing) = @_;
1187 raise_param_exc
({ all
=> "option conflicts with option 'vmid'"})
1188 if $param->{all
} && $param->{vmid
};
1190 raise_param_exc
({ exclude
=> "option conflicts with option 'vmid'"})
1191 if $param->{exclude
} && $param->{vmid
};
1193 $param->{all
} = 1 if defined($param->{exclude
});
1195 warn "option 'size' is deprecated and will be removed in a future " .
1196 "release, please update your script/configuration!\n"
1197 if defined($param->{size
});
1199 return if !$check_missing;
1201 raise_param_exc
({ vmid
=> "property is missing"})
1202 if !($param->{all
} || $param->{stop
}) && !$param->{vmid
};
1206 sub stop_running_backups
{
1209 my $upid = PVE
::Tools
::file_read_firstline
($pidfile);
1212 my $task = PVE
::Tools
::upid_decode
($upid);
1214 if (PVE
::ProcFSTools
::check_process_running
($task->{pid
}, $task->{pstart
}) &&
1215 PVE
::ProcFSTools
::read_proc_starttime
($task->{pid
}) == $task->{pstart
}) {
1216 kill(15, $task->{pid
});
1217 # wait max 15 seconds to shut down (else, do nothing for now)
1219 for ($i = 15; $i > 0; $i--) {
1220 last if !PVE
::ProcFSTools
::check_process_running
(($task->{pid
}, $task->{pstart
}));
1223 die "stoping backup process $task->{pid} failed\n" if $i == 0;
1232 if ($param->{vmid
}) {
1233 $cmd .= " " . join(' ', PVE
::Tools
::split_list
($param->{vmid
}));
1236 foreach my $p (keys %$param) {
1237 next if $p eq 'id' || $p eq 'vmid' || $p eq 'starttime' ||
1238 $p eq 'dow' || $p eq 'stdout' || $p eq 'enabled';
1239 my $v = $param->{$p};
1240 my $pd = $confdesc->{$p} || die "no such vzdump option '$p'\n";
1241 if ($p eq 'exclude-path') {
1242 foreach my $path (split(/\0/, $v || '')) {
1243 $cmd .= " --$p " . PVE
::Tools
::shellquote
($path);
1246 $cmd .= " --$p " . PVE
::Tools
::shellquote
($v) if defined($v) && $v ne '';
1253 # bash completion helpers
1254 sub complete_backup_storage
{
1256 my $cfg = PVE
::Storage
::config
();
1257 my $ids = $cfg->{ids
};
1259 my $nodename = PVE
::INotify
::nodename
();
1262 foreach my $sid (keys %$ids) {
1263 my $scfg = $ids->{$sid};
1264 next if !PVE
::Storage
::storage_check_enabled
($cfg, $sid, $nodename, 1);
1265 next if !$scfg->{content
}->{backup
};