forked from laravel-matrix/virtualmin-gpl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.pl
executable file
·341 lines (315 loc) · 8.6 KB
/
backup.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/usr/local/bin/perl
# Do a scheduled virtual server backup
package virtual_server;
$main::no_acl_check++;
require './virtual-server-lib.pl';
$host = &get_system_hostname();
if (&foreign_check("mailboxes")) {
&foreign_require("mailboxes");
$has_mailboxes++;
}
# Get the schedule being used
$id = 1;
$backup_debug = 0;
while(@ARGV > 0) {
local $a = shift(@ARGV);
if ($a eq "--id") {
$id = shift(@ARGV);
$id || &usage("Missing backup schedule ID");
}
elsif ($a eq "--debug") {
$backup_debug = 1;
}
elsif ($a eq "--force-email") {
$force_email = 1;
}
else {
&usage("Unknown parameter $a");
}
}
($sched) = grep { $_->{'id'} == $id } &list_scheduled_backups();
$sched || &usage("No scheduled backup with ID $id exists");
&start_running_backup($sched);
# Work out what will be backed up
if ($sched->{'all'} == 1) {
# All domains
@doms = &list_domains();
}
elsif ($sched->{'all'} == 2) {
# All except some domains
%exc = map { $_, 1 } split(/\s+/, $sched->{'doms'});
@doms = grep { !$exc{$_->{'id'}} } &list_domains();
if ($sched->{'parent'}) {
@doms = grep { !$_->{'parent'} || !$exc{$_->{'parent'}} } @doms;
}
}
else {
# Selected domains
foreach $d (split(/\s+/, $sched->{'doms'})) {
local $dinfo = &get_domain($d);
if ($dinfo) {
push(@doms, $dinfo);
if (!$dinfo->{'parent'} && $sched->{'parent'}) {
push(@doms, &get_domain_by("parent", $d));
}
}
}
@doms = grep { !$donedom{$_->{'id'}}++ } @doms;
}
# Limit to those on some plan, if given
if ($sched->{'plan'}) {
%plandoms = map { $_->{'id'}, 1 }
grep { &indexof($_->{'plan'}, split(/\s+/, $sched->{'plan'})) >= 0 }
@doms;
@doms = grep { $plandoms{$_->{'id'}} ||
$plandoms{$_->{'parent'}} } @doms;
}
# Work out who the schedule is being run for
if ($sched->{'owner'}) {
$asd = &get_domain($sched->{'owner'});
$owner = $asd ? $asd->{'user'} : undef;
$cbmode = &can_backup_domain(undef, $owner);
@doms = grep { &can_backup_domain($_, $owner) } @doms;
}
else {
# Master admin
$cbmode = 1;
}
# Get the backup key
if ($sched->{'key'} && defined(&get_backup_key)) {
$key = &get_backup_key($sched->{'key'});
}
# Work out features and options
if ($sched->{'feature_all'}) {
@do_features = ( &get_available_backup_features(),
&list_backup_plugins() );
}
else {
@do_features = split(/\s+/, $sched->{'features'});
}
foreach $f (@do_features) {
$options{$f} = { map { split(/=/, $_) }
split(/,/, $sched->{'backup_opts_'.$f}) };
}
$options{'dir'}->{'exclude'} = $sched->{'exclude'};
@vbs = split(/\s+/, $sched->{'virtualmin'});
# Start capturing output
$first_print = \&first_save_print;
$second_print = \&second_save_print;
$indent_print = \&indent_save_print;
$outdent_print = \&outdent_save_print;
# Run any before command
$start_time = time();
if ($sched->{'before'}) {
&$first_print("Running pre-backup command ..");
&set_backup_envs($sched, \@doms);
$out .= &backquote_command("($sched->{'before'}) 2>&1 </dev/null");
&reset_backup_envs();
print $out;
$output .= $out;
if ($?) {
&$second_print(".. failed!");
$ok = 0;
$size = 0;
goto PREFAILED;
}
else {
&$second_print(".. done");
}
}
# Execute the backup
@dests = &get_scheduled_backup_dests($sched);
@strfdests = $sched->{'strftime'} ? map { &backup_strftime($_) } @dests
: @dests;
$current_id = undef;
eval {
local $main::error_must_die = 1;
($ok, $size, $errdoms) = &backup_domains(
\@strfdests,
\@doms,
\@do_features,
$sched->{'fmt'},
$sched->{'errors'},
\%options,
$sched->{'fmt'} == 2,
\@vbs,
$sched->{'mkdir'},
$sched->{'onebyone'},
$cbmode == 2,
\&backup_cbfunc,
$sched->{'increment'},
1,
$key,
$sched->{'kill'});
};
if ($@) {
# Perl error during backup!
$ok = 0;
$output .= $@;
}
# If purging old backups, do that now
@purges = &get_scheduled_backup_purges($sched);
if ($ok || $sched->{'errors'} == 1) {
$i = 0;
$asd = $cbmode == 2 ? &get_backup_as_domain(\@doms) : undef;
foreach $dest (@dests) {
if ($purges[$i]) {
$current_id = undef;
$pok = &purge_domain_backups(
$dest, $purges[$i], $start_time, $asd);
$ok = 0 if (!$pok);
}
$i++;
}
}
# Run any after command
if ($sched->{'after'}) {
&$first_print("Running post-backup command ..");
&set_backup_envs($sched, \@doms);
$out = &backquote_command("($sched->{'after'}) 2>&1 </dev/null");
&reset_backup_envs();
print $out;
$output .= $out;
if ($?) {
&$second_print(".. failed!");
}
else {
&$second_print(".. done");
}
}
&cleanup_backup_limits(0, 1);
foreach $dest (@strfdests) {
&write_backup_log(\@doms, $dest, $sched->{'increment'}, $start_time,
$size, $ok, "sched", $output, $errdoms,
$asd ? $asd->{'user'} : undef, $key, $sched->{'id'},
$sched->{'fmt'});
}
PREFAILED:
# Send an email to the recipient, if there are any
if ($sched->{'email'} && $has_mailboxes &&
(!$ok || @$errdoms || !$sched->{'email_err'} || $force_email)) {
# Construct header for backup email
$output_header = "";
# Nice format $dest for email
$dest = &nice_backup_url($strfdests[0]);
if ($ok && !@$errdoms) {
$output_header .= &text('backup_done',
&nice_size($size))." ";
$subject = &text('backup_donesubject', $host, $dest);
}
elsif ($ok && @$errdoms) {
$output_header .= &text('backup_partial',
&nice_size($size))." ";
$subject = &text('backup_partialsubject', $host, $dest);
}
else {
$output_header .= $text{'backup_failed'}." ";
$subject = &text('backup_failedsubject', $host, $dest);
}
$total_time = time() - $start_time;
$output_header .= &text('backup_time',
&nice_hour_mins_secs($total_time))."\n";
$output_header .= "\n";
# Add list of domains that failed
if (@$errdoms) {
$output_header .= $text{'backup_partial2'}."\n";
foreach $d (@$errdoms) {
$output_header .= " ".$d->{'dom'}."\n";
}
}
$output_header .= &text('backup_fromvirt',
&get_virtualmin_url())."\n";
$output_header .= "\n";
$output = $output_header.$output;
$mail = { 'headers' => [ [ 'From', &get_global_from_address() ],
[ 'Subject', &html_tags_to_text($subject) ],
[ 'To', $sched->{'email'} ] ],
'attach' => [ { 'headers' => [ [ 'Content-type',
'text/plain' ] ],
'data' => &entities_to_ascii($output) } ]
};
&mailboxes::send_mail($mail);
}
# Send email to domain owners too, if selected
%errdoms = map { $_->{'id'}, $_ } @$errdoms;
if ($sched->{'email_doms'} && $has_mailboxes &&
(!$ok || !$sched->{'email_err'} || $force_email)) {
@emails = &unique(map { $_->{'emailto'} } @doms);
foreach $email (@emails) {
# Find the domains for this email address, and their output
@edoms = grep { $_->{'emailto'} eq $email } @doms;
$eoutput = join("", map { $domain_output{$_->{'id'}} } @edoms);
$eoutput .= "\n";
$eoutput .= &text('backup_fromvirt',
&get_virtualmin_url($edoms[0]))."\n";
# Check if any of the domains failed
@failededoms = grep { $errdoms{$_->{'id'}} } @edoms;
$dest = &nice_backup_url($strfdests[0]);
if (@failededoms) {
$subject = &text('backup_failedsubject', $host, $dest);
}
else {
$subject = &text('backup_donesubject', $host, $dest);
}
$mail = {
'headers' =>
[ [ 'From', &get_global_from_address($edoms[0]) ],
[ 'Subject', &html_tags_to_text($subject) ],
[ 'To', $email ] ],
'attach' =>
[ { 'headers' => [ [ 'Content-type', 'text/plain' ] ],
'data' => &entities_to_ascii($eoutput) } ]
};
if ($eoutput) {
&mailboxes::send_mail($mail);
}
}
}
# Backup is done
&stop_running_backup($sched);
# Override print functions to capture output
sub first_save_print
{
local @msg = map { &html_tags_to_text(&entities_to_ascii($_)) } @_;
$output .= $indent_text.join("", @msg)."\n";
$domain_output{$current_id} .= $indent_text.join("", @msg)."\n"
if ($current_id);
print $indent_text.join("", @msg)."\n" if ($backup_debug);
}
sub second_save_print
{
local @msg = map { &html_tags_to_text(&entities_to_ascii($_)) } @_;
$output .= $indent_text.join("", @msg)."\n\n";
$domain_output{$current_id} .= $indent_text.join("", @msg)."\n\n"
if ($current_id);
print $indent_text.join("", @msg)."\n" if ($backup_debug);
}
sub indent_save_print
{
$indent_text .= " ";
}
sub outdent_save_print
{
$indent_text = substr($indent_text, 4);
}
# Called during the backup process for each domain
sub backup_cbfunc
{
local ($d, $step, $info) = @_;
if ($step == 0) {
$current_id = $d->{'id'};
}
elsif ($step == 2) {
$current_id = undef;
}
}
sub usage
{
print "$_[0]\n\n" if ($_[0]);
print "Runs one scheduled Virtualmin backup. Usually called automatically from Cron.\n";
print "\n";
print "usage: backup.pl [--id number]\n";
print " [--debug]\n";
print " [--force-email]\n";
exit(1);
}