forked from virtualmin/virtualmin-gpl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.cgi
executable file
·362 lines (339 loc) · 10.2 KB
/
backup.cgi
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/usr/local/bin/perl
# Do an immediate virtual server backup
require './virtual-server-lib.pl';
&ReadParse();
&error_setup($text{'backup_err'});
$cbmode = &can_backup_domain();
$cbmode || &error($text{'backup_ecannot'});
if ($in{'oneoff'} || $in{'bg'}) {
($sched) = grep { $_->{'id'} eq $in{'oneoff'} &&
&can_backup_sched($_) } &list_scheduled_backups();
$sched || &error($text{'backup_egone'});
$runsched = $sched;
}
if ($in{'bg'}) {
# Background backup of previous scheduled request .. launch it
&ui_print_unbuffered_header(undef, $text{'backup_title'}, "");
@dests = &get_scheduled_backup_dests($sched);
$nice = join(", ", map { &nice_backup_url($_) } @dests);
&$first_print(&text('backup_starting', $nice));
$cmd = "$backup_cron_cmd --id $sched->{'id'} --force-email";
&clean_environment();
&execute_command("$cmd >/dev/null 2>&1 </dev/null &");
&reset_environment();
if ($sched->{'email'}) {
&$second_print(&text('backup_started', $sched->{'email'}));
}
else {
&$second_print($text{'backup_started2'});
}
&ui_print_footer("/$module_name/list_sched.cgi", $text{'sched_return'});
exit;
}
# Validate inputs
if ($sched->{'owner'}) {
$od = &get_domain($sched->{'owner'});
$od || &error($text{'backup_eownergone'});
$acluser = $od->{'user'};
}
else {
$acluser = undef;
}
if ($in{'all'} == 1) {
# All domains
@doms = grep { &can_backup_domain($_, $acluser) } &list_domains();
}
elsif ($in{'all'} == 2) {
# All except selected
%exc = map { $_, 1 } split(/\s+/, $in{'doms'});
@doms = grep { &can_backup_domain($_, $acluser) &&
!$exc{$_->{'id'}} } &list_domains();
if ($in{'parent'}) {
@doms = grep { !$_->{'parent'} || !$exc{$_->{'parent'}} } @doms;
}
}
else {
# Only selected
foreach $did (split(/\s+/, $in{'doms'})) {
local $dinfo = &get_domain($did);
if ($dinfo && &can_backup_domain($dinfo, $acluser)) {
push(@doms, $dinfo);
if (!$dinfo->{'parent'} && $in{'parent'}) {
push(@doms, &get_domain_by("parent", $did));
}
}
}
@doms = grep { !$donedom{$_->{'id'}}++ } @doms;
}
# Limit to those on some plan, if given
if ($in{'plan'} && !$in{'plan_def'} && &can_edit_plans()) {
%plandoms = map { $_->{'id'}, 1 }
grep { &indexof($_->{'plan'}, split(/\0/, $in{'plan'})) >= 0 } @doms;
@doms = grep { $plandoms{$_->{'id'}} ||
$plandoms{$_->{'parent'}} } @doms;
}
# Limit to those owned by some resellers, if given
if ($in{'reseller'} && !$in{'reseller_def'} && &can_edit_resellers()) {
%resels = map { $_, 1 } split(/\0/, $in{'reseller'});
@doms = grep { $_->{'reseller'} && $resels{$_->{'reseller'}} } @doms;
}
# Work out the current user's main domain, if needed
if ($cbmode == 2) {
$d = &get_domain_by_user($base_remote_user);
}
elsif ($cbmode == 3 && $in{'dest_mode'} == 0) {
# A reseller running a backup to a local file, created by one of
# his domains.
$sid = $in{'sched'} || $in{'oneoff'};
($sched) = grep { $_->{'id'} eq $sid &&
&can_backup_sched($_) } &list_scheduled_backups();
if ($sched && $sched->{'owner'}) {
$d = &get_domain($sched->{'owner'});
}
}
if ($in{'feature_all'}) {
@do_features = ( &get_available_backup_features(), &list_backup_plugins() );
}
else {
@do_features = split(/\0/, $in{'feature'});
}
@do_features || &error($text{'backup_efeatures'});
# Parse destinations
for($i=0; defined($in{"dest".$i."_mode"}); $i++) {
next if ($in{"dest".$i."_mode"} == 0 &&
!$in{"dest".$i."_file"});
$dest = &parse_backup_destination("dest".$i, \%in, $cbmode == 3, $d,
$in{'fmt'});
push(@dests, $dest);
$anydownload++ if ($dest eq "download:" || $dest eq "downloadlink:");
}
@dests || &error($text{'backup_edests'});
if ($anydownload) {
if (@dests == 1) {
# If just downloading, always create a single file
$in{'fmt'} = 0;
}
else {
&error($text{'backup_edownloadmany'});
}
}
@strfdests = $in{'strftime'} ? map { &backup_strftime($_) } @dests
: @dests;
# Get the backup key
$key = undef;
if (defined(&get_backup_key) && $in{'key'}) {
$key = &get_backup_key($in{'key'});
$key || &error($text{'backup_ekey'});
&can_use_backup_key($key) || &error($text{'backup_ekeycannot'});
}
# Parse option inputs
foreach $f (@do_features) {
local $ofunc = "parse_backup_$f";
if (&indexof($f, &list_backup_plugins()) < 0 &&
defined(&$ofunc)) {
$options{$f} = &$ofunc(\%in);
}
elsif (&indexof($f, &list_backup_plugins()) >= 0 &&
&plugin_defined($f, "feature_backup_parse")) {
$options{$f} = &plugin_call($f, "feature_backup_parse", \%in);
}
}
$options{'dir'}->{'exclude'} = join("\t", split(/\r?\n/, $in{'exclude'}));
$options{'dir'}->{'include'} = $in{'include'};
$options{'dir'}->{'strftime'} = $in{'strftime'};
# Parse Virtualmin feature inputs
if (&can_backup_virtualmin()) {
@vbs = split(/\0/, $in{'virtualmin'});
}
else {
@vbs = ( );
}
@doms || @vbs || &error($text{'backup_edoms'});
# Flag backup as started
if (!$runsched) {
# Fake up schedule object
$runsched = { 'id' => 'backup.cgi.'.time() };
for(my $i=0; $i<@dests; $i++) {
$runsched->{'dest'.$i} = $dests[$i];
}
if ($in{'all'}) {
$runsched->{'all'} = 1;
}
elsif (@doms) {
$runsched->{'doms'} = join(" ", map { $_->{'id'} } @doms);
}
$runsched->{'virtualmin'} = join(" ", @vbs);
}
&start_running_backup($runsched);
if ($dests[0] eq "download:" || $dests[0] eq "downloadlink:") {
# Special case .. we backup to a temp file and output in the browser
my $tempfile;
my $sfx = &compression_to_suffix($in{'compression'});
if ($dests[0] eq "download:") {
$temp = &transname().".".$sfx;
}
else {
my $filename = "virtualmin-all-domains-on-";
my $hostname = &get_system_hostname();
$hostname =~ s/\./-/g;
$filename .= $hostname;
my $time = strftime("%Y-%m-%d-%H-%M", localtime);
$filename .= "-".$time;
my $filename_dom = "$doms[0]->{'dom'}-$time";
$filename_dom =~ s/\./-/g;
$tempfile = @doms == 1 ? $filename_dom
: $filename;
$tempfile .= ".".$sfx;
$temp = &tempname($remote_user.":".$tempfile);
}
if (@doms) {
# Pre-create temp file with correct permissions
foreach $t ($temp, $temp.".info", $temp.".dom") {
&open_tempfile(TEMP, ">$t", 0, 1);
&close_tempfile(TEMP);
&set_ownership_permissions(
$doms[0]->{'uid'}, $doms[0]->{'gid'}, 0700, $t);
}
}
if ($dests[0] eq "download:") {
# Output is hidden
&set_all_null_print();
}
else {
# Show backup progress
&ui_print_unbuffered_header(undef, $text{'backup_title'}, "");
}
($ok, $size) = &backup_domains([ $temp ], \@doms, \@do_features,
$in{'fmt'}, $in{'errors'}, \%options,
$in{'fmt'} == 2, \@vbs, $in{'mkdir'},
$in{'onebyone'}, $cbmode == 2,
undef, $in{'increment'}, 0, $key, 0,
$in{'compression'});
&cleanup_backup_limits(0, 1);
unlink($temp.".info");
unlink($temp.".dom");
&run_post_actions();
if (!$ok) {
unlink($temp);
if ($dests[0] eq "download:") {
&error($text{'backup_edownloadfailed'});
}
else {
&ui_print_footer("/$module_name/list_sched.cgi",
$text{'sched_return'});
}
}
elsif ($dests[0] eq "download:") {
# Just output the file
@st = stat($temp);
print "Content-type: application/octet-stream\n";
print "Content-length: $st[7]\n";
print "\n";
open(TEMP, "<".$temp);
unlink($temp);
while(read(TEMP, $buf, &get_buffer_size()) > 0) {
print $buf;
}
close(TEMP);
}
else {
# Show page with a link to download
my $fsize = -s $temp;
my $fsizen = &nice_size($fsize);
print "<p><b>",
&ui_link("@{[&get_webprefix_safe()]}/$module_name/download_backup.cgi?file=".
&urlize($temp),
&text('backup_downloadfile', $tempfile) . " ($fsizen)" ),
"</b><p>\n";
&ui_print_footer("/$module_name/list_sched.cgi",
$text{'sched_return'});
}
}
else {
&ui_print_unbuffered_header(undef, $text{'backup_title'}, "");
# Run any before command
$start_time = time();
if ($in{'oneoff'} && $sched->{'before'}) {
&$first_print($text{'backup_brun'});
&set_backup_envs($sched, \@doms);
$out .= &backquote_logged(
"($sched->{'before'}) 2>&1 </dev/null");
&reset_backup_envs();
print "<pre>".&html_escape($out)."</pre>";
if ($?) {
&$second_print($text{'backup_brunfailed'});
goto PREFAILED;
}
else {
&$second_print($text{'setup_done'});
}
}
# Start backup and show progress
$nice = join(", ", map { &nice_backup_url($_) } @strfdests);
if (@doms) {
print &text('backup_doing', scalar(@doms), $nice),"<p>\n";
}
else {
print &text('backup_doing2', scalar(@vbs), $nice),"<p>\n";
}
&start_print_capture();
($ok, $size, $errdoms) = &backup_domains(
\@strfdests, \@doms, \@do_features,
$in{'fmt'}, $in{'errors'}, \%options,
$in{'fmt'} == 2, \@vbs, $in{'mkdir'},
$in{'onebyone'}, $cbmode == 2,
undef, $in{'increment'}, undef, $key, 0,
$in{'compression'});
$output = &stop_print_capture();
&cleanup_backup_limits(0, 1);
foreach $dest (@strfdests) {
&write_backup_log(\@doms, $dest, $in{'increment'}, $start_time,
$size, $ok, "cgi", $output, $errdoms,
undef, $key, $sched ? $sched->{'id'} : undef,
$in{'fmt'}, 0);
}
&run_post_actions();
if (!$ok) {
print "<p>",$text{'backup_failed'},"<p>\n";
}
else {
print "<p>",&text('backup_done', &nice_size($size)),"<p>\n";
}
# If purging old backups, do that now
if (($ok || $in{'errors'} == 1) && $in{'oneoff'}) {
@purges = &get_scheduled_backup_purges($sched);
$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);
}
$i++;
}
}
# Run any after command
if ($in{'oneoff'} && $sched->{'after'}) {
&$first_print($text{'backup_arun'});
&set_backup_envs($sched, \@doms, $ok);
$out = &backquote_command(
"($sched->{'after'}) 2>&1 </dev/null");
&reset_backup_envs();
print "<pre>".&html_escape($out)."</pre>";
if ($?) {
&$second_print($text{'backup_arunfailed'});
}
else {
&$second_print($text{'setup_done'});
}
}
PREFAILED:
&webmin_log("backup", $dests[0], undef,
{ 'doms' => [ map { $_->{'dom'} } @doms ],
'failed' => !$ok,
'sched' => 0, });
&ui_print_footer("/$module_name/list_sched.cgi", $text{'sched_return'});
}
# Flag backup as done
&stop_running_backup($runsched);