3 # Backup a database to a local file
5 require './postgresql-lib.pl' ;
9 &error_setup ( $text{'backup_err'} ) ;
13 @alldbs = &list_databases();
14 @dbs = grep { &can_edit_db($_) } @alldbs;
15 @alldbs == @dbs || &error($text{'dbase_ecannot'});
18 &can_edit_db($in{'db'}) || &error($text{'dbase_ecannot'});
20 $access{'backup'} || &error($text{'backup_ecannot'});
21 if (!$in{'save'} || $in{'sched'}) {
23 -d $in{'path'} || -d &date_subs($in{'path'}) || $in{'mkdir'} ||
24 &error(&text('backup_pe4', $in{'path'})) ;
27 $in{'path'} =~ /^\/\S+$/ ||
28 $in{'path'} =~ /^[a-z]:[\\\/]/i ||
29 &error(&text('backup_pe3', $in{'path'})) ;
31 if (!$in{'all'} && !$in{'tables_def'}) {
32 @tables = split(/\0/, $in{'tables'});
33 @tables || &error($text{'backup_etables'});
36 $cron = !$module_info{'usermin'} &&
37 !$access{'user'} && &foreign_installed("cron");
40 if ($access{'cmds'}) {
41 $config{'backup_before_'.$in{'db'}} = $in{'before'};
42 $config{'backup_after_'.$in{'db'}} = $in{'after'};
44 $config{'backup_cmode_'.$in{'db'}} = $in{'cmode'};
45 $cmode = $in{'cmode'};
49 &foreign_require("cron", "cron-lib.pl");
50 @jobs = &cron::list_cron_jobs();
51 $cmd = $in{'all'} ? "$cron_cmd --all" : "$cron_cmd $in{'db'}";
52 ($job) = grep { $_->{'command'} eq $cmd } @jobs;
54 $job ||= { 'command' => $cmd,
57 &cron::parse_times_input($job, \%in);
61 # Make sure the database exists
64 foreach ( &list_databases() ) {
65 if ( $_ eq $in{'db'} ) { $db_find_f = 1 ; }
68 if ( $db_find_f == 0 ) { &error ( &text ( 'backup_edb' ) ) ; }
71 # Save choices for next time the form is visited (and for the cron job)
72 if ($module_info{'usermin'}) {
73 $userconfig{'backup_'.$in{'db'}} = $in{'path'};
74 $userconfig{'backup_format_'.$in{'db'}} = $in{'format'};
75 $userconfig{'backup_tables_'.$in{'db'}} = join(" ", @tables);
76 &write_file("$user_module_config_directory/config", \%userconfig);
79 $config{'backup_'.$in{'db'}} = $in{'path'};
80 $config{'backup_mkdir_'.$in{'db'}} = $in{'mkdir'};
81 $config{'backup_format_'.$in{'db'}} = $in{'format'};
82 $config{'backup_tables_'.$in{'db'}} = join(" ", @tables);
83 &write_file("$module_config_directory/config", \%config);
86 $desc = "<tt>$in{'db'}</tt>";
87 &ui_print_header($desc, $text{'backup_title'}, "");
89 # Construct and run the backup command
90 @dbs = $in{'all'} ? @alldbs : ( $in{'db'} );
91 $suf = $in{'format'} eq "p" ? "sql" :
92 $in{'format'} eq "t" ? "tar" : "post";
94 # Run and check before-backup command (for all DBs)
95 $bok = &execute_before(undef, STDOUT, 1, $in{'file'}, undef);
97 print "$main::whatfailed : ",$text{'backup_ebefore'},"<p>\n";
102 if (!&accepting_connections($db)) {
103 print &text('backup_notaccept', "<tt>$db</tt>"),"<p>\n";
107 $dir = &date_subs($in{'path'});
108 &make_backup_dir($dir) if ($in{'mkdir'});
109 $path = $dir."/".$db.".".$suf;
112 $path = &date_subs($in{'path'});
114 if ($cron && $cmode == 0) {
115 # Run and check before-backup command
116 $bok = &execute_before($db, STDOUT, 1, $path, $in{'all'} ? undef : $db);
118 print "$main::whatfailed : ",$text{'backup_ebefore'},"<p>\n";
122 $err = &backup_database($db, $path, $in{'format'}, \@tables);
124 print "$main::whatfailed : ",
125 &text('backup_ebackup',"<pre>$err</pre>"),"<p>\n";
129 print &text('backup_done', "<tt>$db</tt>",
130 "<tt>$path</tt>", $st[7]),"<p>\n";
132 &execute_after($db, STDOUT, 1, $path, $in{'all'} ? undef : $db)
133 if ($cron && $cmode == 0);
135 &execute_after(undef, STDOUT, 1, $in{'file'}, undef) if ($cmode == 1);
140 &lock_file($cron_cmd);
141 &cron::create_wrapper($cron_cmd, $module_name, "backup.pl");
142 &unlock_file($cron_cmd);
144 &lock_file(&cron::cron_file($job));
145 if ($in{'sched'} && !$oldjob) {
146 &cron::create_cron_job($job);
147 $what = "backup_ccron";
149 elsif (!$in{'sched'} && $oldjob) {
150 # Need to delete cron job
151 &cron::delete_cron_job($job);
152 $what = "backup_dcron";
154 elsif ($in{'sched'} && $oldjob) {
155 # Need to update cron job
156 &cron::change_cron_job($job);
157 $what = "backup_ucron";
160 $what = "backup_ncron";
162 &unlock_file(&cron::cron_file($job));
164 # Tell the user what was done
165 print $text{$what},"<p>\n" if ($what);
168 &webmin_log("backup", undef, $in{'all'} ? "" : $in{'db'}, \%in);
170 &ui_print_footer("", $text{'index_return'});
173 &ui_print_footer("edit_dbase.cgi?db=$in{'db'}", $text{'dbase_return'},
174 "", $text{'index_return'});