5 require './mysql-lib.pl';
8 @alldbs = grep { &supports_backup_db($_) } &list_databases();
9 @dbs = grep { &can_edit_db($_) } @alldbs;
10 @alldbs == @dbs || &error($text{'dbase_ecannot'});
13 &can_edit_db($in{'db'}) || &error($text{'dbase_ecannot'});
15 $access{'edonly'} && &error($text{'dbase_ecannot'});
16 $access{'buser'} || &error($text{'dbase_ecannot'});
17 &error_setup($text{'backup_err'});
19 if (!$in{'save'} || $in{'sched'}) {
21 -d $in{'file'} || -d &date_subs($in{'file'}) || $in{'mkdir'} ||
22 &error($text{'backup_efile2'});
25 $in{'file'} =~ /^\/\S+$/ || &error($text{'backup_efile'});
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>"));
38 if (!$in{'all'} && !$in{'tables_def'}) {
39 @tables = split(/\0/, $in{'tables'});
40 @tables || &error($text{'backup_etables'});
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");
49 $config{'backup_before_'.$in{'db'}} = $in{'before'};
50 $config{'backup_after_'.$in{'db'}} = $in{'after'};
52 $config{'backup_cmode_'.$in{'db'}} = $in{'cmode'};
53 $cmode = $in{'cmode'};
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;
61 $job ||= { 'command' => $cmd,
64 &cron::parse_times_input($job, \%in);
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);
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);
102 &ui_print_header(undef, $text{'backup_title'}, "");
104 # Actually execute the backup now
105 @dbs = $in{'all'} ? @alldbs : ( $in{'db'} );
107 # Run and check before-backup command (for all DBs)
108 $bok = &execute_before(undef, STDOUT, 1, $in{'file'}, undef);
110 print "$main::whatfailed : ",
111 $text{'backup_ebefore'},"<p>\n";
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" : "");
124 $file = &date_subs($in{'file'});
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);
131 print "$main::whatfailed : ",
132 $text{'backup_ebefore'},"<p>\n";
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'});
142 print "$main::whatfailed : ",
143 &text('backup_ebackup',"<pre>$err</pre>"),"<p>\n";
147 print &text('backup_done', "<tt>$db</tt>",
148 "<tt>$file</tt>", int($st[7])),"<p>\n";
150 &execute_after($db, STDOUT, 1, $file, $in{'all'} ? undef : $db)
151 if ($cron && $cmode == 0);
153 &execute_after(undef, STDOUT, 1, $in{'file'}, undef) if ($cmode == 1);
158 &lock_file($cron_cmd);
159 &cron::create_wrapper($cron_cmd, $module_name, "backup.pl");
160 &unlock_file($cron_cmd);
162 &lock_file(&cron::cron_file($job));
163 if ($in{'sched'} && !$oldjob) {
164 &cron::create_cron_job($job);
165 $what = "backup_ccron";
167 elsif (!$in{'sched'} && $oldjob) {
168 # Need to delete cron job
169 &cron::delete_cron_job($job);
170 $what = "backup_dcron";
172 elsif ($in{'sched'} && $oldjob) {
173 # Need to update cron job
174 &cron::change_cron_job($job);
175 $what = "backup_ucron";
178 $what = "backup_ncron";
180 &unlock_file(&cron::cron_file($job));
182 # Tell the user what was done
183 print $text{$what},"<p>\n" if ($what);
186 &webmin_log("backup", undef, $in{'all'} ? "" : $in{'db'}, \%in);
188 &ui_print_footer("", $text{'index_return'});
191 &ui_print_footer("edit_dbase.cgi?db=$in{'db'}", $text{'dbase_return'},
192 "", $text{'index_return'});