Handle hostnames with upper-case letters
[webmin.git] / mysql / backup_db.cgi
1 #!/usr/local/bin/perl
2 # backup_db.cgi
3 # Do the actual backup
4
5 require './mysql-lib.pl';
6 &ReadParse();
7 if ($in{'all'}) {
8         @alldbs = grep { &supports_backup_db($_) } &list_databases();
9         @dbs = grep { &can_edit_db($_) } @alldbs;
10         @alldbs == @dbs || &error($text{'dbase_ecannot'});
11         }
12 else {
13         &can_edit_db($in{'db'}) || &error($text{'dbase_ecannot'});
14         }
15 $access{'edonly'} && &error($text{'dbase_ecannot'});
16 $access{'buser'} || &error($text{'dbase_ecannot'});
17 &error_setup($text{'backup_err'});
18
19 if (!$in{'save'} || $in{'sched'}) {
20         if ($in{'all'}) {
21                 -d $in{'file'} || -d &date_subs($in{'file'}) || $in{'mkdir'} ||
22                         &error($text{'backup_efile2'});
23                 }
24         else {
25                 $in{'file'} =~ /^\/\S+$/ || &error($text{'backup_efile'});
26                 }
27         $in{'where_def'} || $in{'where'} || &error($text{'backup_ewhere'});
28         $in{'charset_def'} || $in{'charset'} =~ /^\S+$/ ||
29                 &error($text{'backup_echarset'});
30         $ccmd = $in{'compress'} == 1 ? "gzip" :
31                 $in{'compress'} == 2 ? "bzip2" : undef;
32         !$ccmd || &has_command($ccmd) ||
33                 &error(&text('backup_eccmd', "<tt>$ccmd</tt>"));
34         if (!&is_under_directory($access{'bpath'}, $in{'file'})) {
35                 &error($text{'backup_epath'}."<br>".
36                        &text('backup_eunder', "<tt>$access{'bpath'}</tt>"));
37                 }
38         if (!$in{'all'} && !$in{'tables_def'}) {
39                 @tables = split(/\0/, $in{'tables'});
40                 @tables || &error($text{'backup_etables'});
41                 }
42         }
43 @compat = $in{'compatible_def'} ? ( ) : ( $in{'compatible'} );
44 push(@compat, split(/\0/, $in{'options'}));
45 $cron = !$module_info{'usermin'} && $access{'buser'} eq 'root' &&
46         !$access{'user'} && &foreign_installed("cron");
47 $cmode = 0;
48 if ($cron) {
49         $config{'backup_before_'.$in{'db'}} = $in{'before'};
50         $config{'backup_after_'.$in{'db'}} = $in{'after'};
51         if ($in{'all'}) {
52                 $config{'backup_cmode_'.$in{'db'}} = $in{'cmode'};
53                 $cmode = $in{'cmode'};
54                 }
55
56         &foreign_require("cron", "cron-lib.pl");
57         @jobs = &cron::list_cron_jobs();
58         $cmd = $in{'all'} ? "$cron_cmd --all" : "$cron_cmd $in{'db'}";
59         ($job) = grep { $_->{'command'} eq $cmd } @jobs;
60         $oldjob = $job;
61         $job ||= { 'command' => $cmd,
62                    'user' => 'root',
63                    'active' => 1 };
64         &cron::parse_times_input($job, \%in);
65         }
66
67 # Save choices for next time the form is visited (and for the cron job)
68 if ($module_info{'usermin'}) {
69         $userconfig{'backup_'.$in{'db'}} = $in{'file'};
70         $userconfig{'backup_where_'.$in{'db'}} =
71                 $in{'where_def'} ? undef : $in{'where'};
72         $userconfig{'backup_charset_'.$in{'db'}} =
73                 $in{'charset_def'} ? undef : $in{'charset'};
74         $userconfig{'backup_compatible_'.$in{'db'}} =
75                 $in{'compatible_def'} ? undef : $in{'compatible'};
76         $userconfig{'backup_options_'.$in{'db'}} =
77                 join(" ", split(/\0/, $in{'options'}));
78         $userconfig{'backup_compress_'.$in{'db'}} = $in{'compress'};
79         $userconfig{'backup_drop_'.$in{'db'}} = $in{'drop'};
80         $userconfig{'backup_single_'.$in{'db'}} = $in{'single'};
81         $userconfig{'backup_tables_'.$in{'db'}} = join(" ", @tables);
82         &write_file("$user_module_config_directory/config", \%userconfig);
83         }
84 else {
85         $config{'backup_'.$in{'db'}} = $in{'file'};
86         $config{'backup_mkdir_'.$in{'db'}} = $in{'mkdir'};
87         $config{'backup_where_'.$in{'db'}} =
88                 $in{'where_def'} ? undef : $in{'where'};
89         $config{'backup_charset_'.$in{'db'}} =
90                 $in{'charset_def'} ? undef : $in{'charset'};
91         $config{'backup_compatible_'.$in{'db'}} =
92                 $in{'compatible_def'} ? undef : $in{'compatible'};
93         $config{'backup_options_'.$in{'db'}} =
94                 join(" ", split(/\0/, $in{'options'}));
95         $config{'backup_compress_'.$in{'db'}} = $in{'compress'};
96         $config{'backup_drop_'.$in{'db'}} = $in{'drop'};
97         $config{'backup_single_'.$in{'db'}} = $in{'single'};
98         $config{'backup_tables_'.$in{'db'}} = join(" ", @tables);
99         &write_file("$module_config_directory/config", \%config);
100         }
101
102 &ui_print_header(undef, $text{'backup_title'}, "");
103 if (!$in{'save'}) {
104         # Actually execute the backup now
105         @dbs = $in{'all'} ? @alldbs : ( $in{'db'} );
106         if ($cmode == 1) {
107                 # Run and check before-backup command (for all DBs)
108                 $bok = &execute_before(undef, STDOUT, 1, $in{'file'}, undef);
109                 if (!$bok) {
110                         print "$main::whatfailed : ",
111                               $text{'backup_ebefore'},"<p>\n";
112                         goto donebackup;
113                         }
114                 }
115         foreach $db (@dbs) {
116                 if ($in{'all'}) {
117                         $dir = &date_subs($in{'file'});
118                         &make_dir($dir, 0755) if ($in{'mkdir'});
119                         $file = $dir."/".$db.".sql".
120                                 ($in{'compress'} == 1 ? ".gz" :
121                                  $in{'compress'} == 2 ? ".bz2" : "");
122                         }
123                 else {
124                         $file = &date_subs($in{'file'});
125                         }
126                 if ($cron && $cmode == 0) {
127                         # Run and check before-backup command (for one DB)
128                         $bok = &execute_before($db, STDOUT, 1, $file,
129                                                $in{'all'} ? undef : $db);
130                         if (!$bok) {
131                                 print "$main::whatfailed : ",
132                                       $text{'backup_ebefore'},"<p>\n";
133                                 next;
134                                 }
135                         }
136                 unlink($file);
137                 local $err = &backup_database($db, $file, $in{'compress'},
138                         $in{'drop'}, $in{'where_def'} ? undef : $in{'where'},
139                         $in{'charset_def'} ? undef : $in{'charset'},
140                         \@compat, \@tables, $access{'buser'}, $in{'single'});
141                 if ($err) {
142                         print "$main::whatfailed : ",
143                               &text('backup_ebackup',"<pre>$err</pre>"),"<p>\n";
144                         }
145                 else {
146                         @st = stat($file);
147                         print &text('backup_done', "<tt>$db</tt>",
148                                     "<tt>$file</tt>", int($st[7])),"<p>\n";
149                         }
150                 &execute_after($db, STDOUT, 1, $file, $in{'all'} ? undef : $db)
151                         if ($cron && $cmode == 0);
152                 }
153         &execute_after(undef, STDOUT, 1, $in{'file'}, undef) if ($cmode == 1);
154         donebackup:
155         }
156
157 if ($cron) {
158         &lock_file($cron_cmd);
159         &cron::create_wrapper($cron_cmd, $module_name, "backup.pl");
160         &unlock_file($cron_cmd);
161
162         &lock_file(&cron::cron_file($job));
163         if ($in{'sched'} && !$oldjob) {
164                 &cron::create_cron_job($job);
165                 $what = "backup_ccron";
166                 }
167         elsif (!$in{'sched'} && $oldjob) {
168                 # Need to delete cron job
169                 &cron::delete_cron_job($job);
170                 $what = "backup_dcron";
171                 }
172         elsif ($in{'sched'} && $oldjob) {
173                 # Need to update cron job
174                 &cron::change_cron_job($job);
175                 $what = "backup_ucron";
176                 }
177         else {
178                 $what = "backup_ncron";
179                 }
180         &unlock_file(&cron::cron_file($job));
181
182         # Tell the user what was done
183         print $text{$what},"<p>\n" if ($what);
184         }
185
186 &webmin_log("backup", undef, $in{'all'} ? "" : $in{'db'}, \%in);
187 if ($in{'all'}) {
188         &ui_print_footer("", $text{'index_return'});
189         }
190 else {
191         &ui_print_footer("edit_dbase.cgi?db=$in{'db'}", $text{'dbase_return'},
192                 "", $text{'index_return'});
193         }
194