Handle hostnames with upper-case letters
[webmin.git] / fsdump / backup.pl
1 #!/usr/local/bin/perl
2 # backup.pl
3 # Perform a backup and send the results to someone
4
5 $no_acl_check++;
6 require './fsdump-lib.pl';
7 $dump = &get_dump($ARGV[0]);
8 $dump->{'id'} || die "Dump $ARGV[0] does not exist!";
9
10 # Check if this backup is already running
11 &foreign_require("proc", "proc-lib.pl");
12 @procs = &proc::list_processes();
13 @running = &running_dumps(\@procs);
14 ($running) = grep { $_->{'id'} eq $dump->{'id'} &&
15                     $_->{'pid'} != $$ } @running;
16
17 $sfile = "$module_config_directory/$dump->{'id'}.$$.status";
18 if ($running) {
19         # Already running! Do nothing ..
20         $ok = 0;
21         $out = &text('email_already', $running->{'pid'})."\n";
22         }
23 else {
24         # Update status file
25         %status = ( 'status' => 'running',
26                     'pid' => $$,
27                     'start' => time() );
28         &write_file($sfile, \%status);
29
30         if ($dump->{'email'}) {
31                 # Save output for mailing
32                 $temp = &transname();
33                 open(OUT, ">$temp");
34                 }
35         else {
36                 # Throw output away
37                 open(OUT, ">/dev/null");
38                 }
39
40         # Create tape change wrapper
41         &create_wrappers();
42
43         $bok = &execute_before($dump, OUT, 0);
44         if (!$bok && !$dump->{'beforefok'}) {
45                 # Before command failed!
46                 print OUT "\n$text{'email_ebefore'}\n";
47                 $status{'status'} = 'failed';
48                 }
49         else {
50                 # Do the backup
51                 $now = time();
52                 $ok = &execute_dump($dump, OUT, 0, 1, $now);
53
54                 # Re-update the status file
55                 if ($ok) {
56                         # Worked .. but verify if asked
57                         if ($dump->{'reverify'}) {
58                                 print OUT "\n$text{'email_verify'}\n";
59                                 $ok = &verify_dump($dump, OUT, 0, 1, $now);
60                                 }
61                         if ($ok) {
62                                 $status{'status'} = 'complete';
63                                 }
64                         else {
65                                 $status{'status'} = 'verifyfailed';
66                                 }
67                         }
68                 else {
69                         $status{'status'} = 'failed';
70                         }
71                 }
72         $status{'end'} = time();
73         &write_file($sfile, \%status);
74
75         if ($status{'status'} eq 'complete') {
76                 # Execute the post-backup script
77                 $bok = &execute_after($dump, OUT, 0);
78                 if (!$bok && !$dump->{'afterfok'}) {
79                         print OUT "\n$text{'email_eafter'}\n";
80                         $status{'status'} = 'failed';
81                         $ok = 0;
82                         }
83                 }
84         close(OUT);
85
86         if ($temp) {
87                 # Read output
88                 open(OUT, $temp);
89                 while(<OUT>) {
90                         s/\r//g;
91                         $out .= $_;
92                         }
93                 close(OUT);
94                 unlink($temp);
95                 }
96         }
97
98 if ($out && $dump->{'email'} && &foreign_check("mailboxes")) {
99         # Construct the email
100         &foreign_require("mailboxes", "mailboxes-lib.pl");
101         $host = &get_system_hostname();
102         @dirs = &dump_directories($dump);
103         $dirs = join(", ", @dirs);
104         %hash = ( %$dirs, 'dirs' => $dirs );
105         local $subject = &substitute_template($dump->{'subject'}, \%hash) ||
106                          &text('email_subject', $dirs, $host);
107         local $data = &text('email_subject', $dirs, $host)."\n\n";
108         $data .= $out;
109         $data .= "\n";
110         if ($ok) {
111                 $data .= $text{'email_ok'}."\n";
112                 }
113         else {
114                 $data .= $text{'email_failed'}."\n";
115                 }
116
117         # Send the email
118         if (!$ok || !$config{'error_email'}) {
119                 # Only send email upon failure, or it requested always
120                 &mailboxes::send_text_mail(&mailboxes::get_from_address(),
121                                            $dump->{'email'},
122                                            undef,
123                                            $subject,
124                                            $data,
125                                            $config{'smtp_server'});
126                 }
127         }
128
129 # Check for any dumps scheduled to run after this one
130 foreach $follow (&list_dumps()) {
131         if ($follow->{'follow'} eq $dump->{'id'} && $follow->{'enabled'} == 2) {
132                 system("$cron_cmd $follow->{'id'}");
133                 }
134         }
135