Handle hostnames with upper-case letters
[webmin.git] / backup-config / backup.pl
1 #!/usr/local/bin/perl
2 # Execute a backup on schedule
3
4 use strict;
5 use warnings;
6 our (%text, %config, $no_acl_check);
7 $no_acl_check++;
8 require './backup-config-lib.pl';
9 &foreign_require("mailboxes", "mailboxes-lib.pl");
10
11 # Get the backup
12 my $backup = &get_backup($ARGV[0]);
13 $backup || die "Failed to find backup $ARGV[0]";
14
15 # Run the pre-backup command, if any
16 my $err;
17 my $premsg = "";
18 if ($backup->{'pre'} =~ /\S/) {
19         my $preout = &backquote_command("($backup->{'pre'}) 2>&1 </dev/null");
20         $premsg = &text('email_pre', $backup->{'pre'})."\n".
21                   $preout."\n";
22         if ($?) {
23                 $err = $text{'email_prefailed'};
24                 }
25         }
26
27 # Do it
28 my @mods = split(/\s+/, $backup->{'mods'});
29 my $size;
30 if (!$err) {
31         $err = &execute_backup(\@mods, $backup->{'dest'}, \$size, undef,
32                                $backup->{'configfile'}, $backup->{'nofiles'},
33                                [ split(/\t+/, $backup->{'others'}) ]);
34         }
35
36 # Run the post-backup command, if any
37 my $postmsg = "";
38 if (!$err && $backup->{'post'} =~ /\S/) {
39         my $postout = &backquote_command("($backup->{'post'}) 2>&1 </dev/null");
40         $postmsg = "\n".
41                   &text('email_post', $backup->{'post'})."\n".
42                   $postout."\n";
43         }
44
45 # Send off the results
46 if (($err || $backup->{'emode'} == 0) && $backup->{'email'}) {
47         my $mlist;
48         foreach my $m (@mods) {
49                 my %minfo = &get_module_info($m);
50                 $mlist .= "    $minfo{'desc'}\n";
51                 }
52         my $host = &get_system_hostname();
53         my $nice = &nice_dest($backup->{'dest'}, 1);
54         $nice =~ s/<[^>]+>//g;
55         my $msg;
56         my $subject;
57         if ($err) {
58                 $err =~ s/<[^>]+>//g;
59                 $msg = $premsg.
60                        $text{'email_mods'}."\n".
61                        $mlist.
62                        "\n".
63                        &text('email_failed', $nice)."\n\n".
64                        "    $err\n";
65                 $subject = &text('email_sfailed', $host);
66                 }
67         else {
68                 $msg = $premsg.
69                        $text{'email_mods'}."\n".
70                        $mlist.
71                        "\n".
72                        &text('email_ok', $nice)."\n".
73                        &text('email_final', &nice_size($size))."\n".
74                        $postmsg;
75                 $subject = &text('email_sok', $host);
76                 }
77         &mailboxes::send_text_mail($config{'from_addr'} ||
78                                    &mailboxes::get_from_address(),
79                                    $backup->{'email'},
80                                    undef,
81                                    $subject,
82                                    $msg);
83         }
84