Handle hostnames with upper-case letters
[webmin.git] / backup-config / save.cgi
1 #!/usr/local/bin/perl
2 # Create, update or delete a scheduled backup
3
4 use strict;
5 use warnings;
6 require './backup-config-lib.pl';
7 our (%in, %text, $cron_cmd, $module_name);
8 &ReadParse();
9
10 # Find the backup job
11 my ($job, $backup);
12 if (!$in{'new'}) {
13         $backup = &get_backup($in{'id'});
14         $job = &find_cron_job($backup);
15         }
16 else {
17         $backup = { };
18         }
19
20 if ($in{'delete'}) {
21         # Delete the backup
22         &delete_backup($backup);
23         if ($job) {
24                 &lock_file(&cron::cron_file($job));
25                 &cron::delete_cron_job($job);
26                 &unlock_file(&cron::cron_file($job));
27                 }
28         }
29 else {
30         # Validate inputs
31         &error_setup($text{'save_err'});
32         my @mods = split(/\0/, $in{'mods'});
33         $backup->{'mods'} = join(" ", @mods);
34         $backup->{'dest'} = &parse_backup_destination("dest", \%in);
35         &cron::parse_times_input($backup, \%in);
36         $backup->{'emode'} = $in{'emode'};
37         $backup->{'email'} = $in{'email'};
38         $backup->{'pre'} = $in{'pre'};
39         $backup->{'post'} = $in{'post'};
40         $backup->{'sched'} = $in{'sched'};
41         ($backup->{'configfile'}, $backup->{'nofiles'}, $backup->{'others'}) =
42                 &parse_backup_what("what", \%in);
43         @mods || ($backup->{'nofiles'} && !$backup->{'configfile'}) ||
44                 &error($text{'save_emods'});
45
46         # Save or create
47         &save_backup($backup);
48         if ($job) {
49                 &lock_file(&cron::cron_file($job));
50                 &cron::delete_cron_job($job);
51                 }
52         if ($in{'sched'}) {
53                 &cron::create_wrapper($cron_cmd, $module_name, "backup.pl");
54                 $job = { 'user' => 'root',
55                          'command' => "$cron_cmd $backup->{'id'}",
56                          'active' => 1,
57                          'mins' => $backup->{'mins'},
58                          'hours' => $backup->{'hours'},
59                          'days' => $backup->{'days'},
60                          'months' => $backup->{'months'},
61                          'weekdays' => $backup->{'weekdays'},
62                          'special' => $backup->{'special'} };
63                 &lock_file(&cron::cron_file($job));
64                 &cron::create_cron_job($job);
65                 }
66         &unlock_file(&cron::cron_file($job)) if ($job);
67         }
68 &webmin_log($in{'new'} ? 'create' : $in{'delete'} ? 'delete' : 'modify',
69             'backup', $backup->{'dest'}, $backup);
70
71 if ($in{'run'}) {
72         # Execute the backup now
73         &ui_print_unbuffered_header(undef, $text{'run_title'}, "");
74
75         # Run the pre-backup command, if any
76         my $err;
77         if ($backup->{'pre'} =~ /\S/) {
78                 my $preout = &backquote_command(
79                         "($backup->{'pre'}) 2>&1 </dev/null");
80                 print &text('email_pre',
81                         "<tt>".&html_escape($backup->{'pre'})."</tt>")."<br>\n".
82                         "<pre>".&html_escape($preout)."</pre>\n";
83                 if ($?) {
84                         $err = $text{'email_prefailed'};
85                         }
86                 }
87
88         my @mods = split(/\s+/, $backup->{'mods'});
89         my $nice = &nice_dest($backup->{'dest'}, 1);
90         if (!$err) {
91                 print &text('run_doing', scalar(@mods),
92                             "<tt>$nice</tt>"),"<br>\n";
93                 my $size;
94                 $err = &execute_backup(
95                         \@mods, $backup->{'dest'}, \$size, undef,
96                         $backup->{'configfile'}, $backup->{'nofiles'},
97                         [ split(/\t+/, $backup->{'others'}) ]);
98                 }
99         if ($err) {
100                 print "<pre>$err</pre>";
101                 print "$text{'run_failed'}<p>\n";
102                 }
103         else {
104                 print "$text{'run_ok'}<p>\n";
105                 }
106
107         # Run the post-backup command, if any
108         if (!$err && $backup->{'post'} =~ /\S/) {
109                 my $postout = &backquote_command(
110                         "($backup->{'post'}) 2>&1 </dev/null");
111                 print &text('email_post',
112                       "<tt>".&html_escape($backup->{'post'})."</tt>")."<br>\n".
113                       "<pre>".&html_escape($postout)."</pre>\n";
114                 }
115
116         &webmin_log("run", "backup", $backup->{'dest'}, $backup);
117         &ui_print_footer("edit.cgi?id=$in{'id'}", $text{'edit_return'},
118                          "index.cgi?mode=sched", $text{'index_return'});
119         }
120 else {
121         &redirect("index.cgi?mode=sched");
122         }
123
124