Handle hostnames with upper-case letters
[webmin.git] / postgresql / backup.cgi
1 #!/usr/local/bin/perl
2 # backup.cgi
3 # Backup a database to a local file
4
5 require './postgresql-lib.pl' ;
6
7 &ReadParse ( ) ;
8
9 &error_setup ( $text{'backup_err'} ) ;
10
11 # Validate inputs
12 if ($in{'all'}) {
13         @alldbs = &list_databases();
14         @dbs = grep { &can_edit_db($_) } @alldbs;
15         @alldbs == @dbs || &error($text{'dbase_ecannot'});
16         }
17 else {
18         &can_edit_db($in{'db'}) || &error($text{'dbase_ecannot'});
19         }
20 $access{'backup'} || &error($text{'backup_ecannot'});
21 if (!$in{'save'} || $in{'sched'}) {
22         if ($in{'all'}) {
23                 -d $in{'path'} || -d &date_subs($in{'path'}) || $in{'mkdir'} ||
24                         &error(&text('backup_pe4', $in{'path'})) ;
25                 }
26         else {
27                 $in{'path'} =~ /^\/\S+$/ ||
28                   $in{'path'} =~ /^[a-z]:[\\\/]/i ||
29                     &error(&text('backup_pe3', $in{'path'})) ;
30                 }
31         if (!$in{'all'} && !$in{'tables_def'}) {
32                 @tables = split(/\0/, $in{'tables'});
33                 @tables || &error($text{'backup_etables'});
34                 }
35         }
36 $cron = !$module_info{'usermin'} &&
37         !$access{'user'} && &foreign_installed("cron");
38 $cmode = 0;
39 if ($cron) {
40         if ($access{'cmds'}) {
41                 $config{'backup_before_'.$in{'db'}} = $in{'before'};
42                 $config{'backup_after_'.$in{'db'}} = $in{'after'};
43                 if ($in{'all'}) {
44                         $config{'backup_cmode_'.$in{'db'}} = $in{'cmode'};
45                         $cmode = $in{'cmode'};
46                         }
47                 }
48
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;
53         $oldjob = $job;
54         $job ||= { 'command' => $cmd,
55                    'user' => 'root',
56                    'active' => 1 };
57         &cron::parse_times_input($job, \%in);
58         }
59
60 if (!$in{'all'}) {
61         # Make sure the database exists
62         $db_find_f = 0 ;
63         if ( $in{'db'} ) {
64             foreach ( &list_databases() ) {
65                 if ( $_ eq $in{'db'} ) { $db_find_f = 1 ; }
66             }
67         }
68         if ( $db_find_f == 0 ) { &error ( &text ( 'backup_edb' ) ) ; }
69         }
70
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);
77         }
78 else {
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);
84         }
85
86 $desc = "<tt>$in{'db'}</tt>";
87 &ui_print_header($desc, $text{'backup_title'}, "");
88 if (!$in{'save'}) {
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";
93         if ($cmode == 1) {
94                 # Run and check before-backup command (for all DBs)
95                 $bok = &execute_before(undef, STDOUT, 1, $in{'file'}, undef);
96                 if (!$bok) {
97                         print "$main::whatfailed : ",$text{'backup_ebefore'},"<p>\n";
98                         goto donebackup;
99                         }
100                 }
101         foreach $db (@dbs) {
102                 if (!&accepting_connections($db)) {
103                         print &text('backup_notaccept', "<tt>$db</tt>"),"<p>\n";
104                         next;
105                         }
106                 if ($in{'all'}) {
107                         $dir = &date_subs($in{'path'});
108                         &make_backup_dir($dir) if ($in{'mkdir'});
109                         $path = $dir."/".$db.".".$suf;
110                         }
111                 else {
112                         $path = &date_subs($in{'path'});
113                         }
114                 if ($cron && $cmode == 0) {
115                         # Run and check before-backup command
116                         $bok = &execute_before($db, STDOUT, 1, $path, $in{'all'} ? undef : $db);
117                         if (!$bok) {
118                                 print "$main::whatfailed : ",$text{'backup_ebefore'},"<p>\n";
119                                 next;
120                                 }
121                         }
122                 $err = &backup_database($db, $path, $in{'format'}, \@tables);
123                 if ($err) {
124                         print "$main::whatfailed : ",
125                               &text('backup_ebackup',"<pre>$err</pre>"),"<p>\n";
126                         }
127                 else {
128                         @st = stat($path);
129                         print &text('backup_done', "<tt>$db</tt>",
130                                           "<tt>$path</tt>", $st[7]),"<p>\n";
131                         }
132                 &execute_after($db, STDOUT, 1, $path, $in{'all'} ? undef : $db)
133                         if ($cron && $cmode == 0);
134                 }
135         &execute_after(undef, STDOUT, 1, $in{'file'}, undef) if ($cmode == 1);
136         donebackup:
137         }
138
139 if ($cron) {
140         &lock_file($cron_cmd);
141         &cron::create_wrapper($cron_cmd, $module_name, "backup.pl");
142         &unlock_file($cron_cmd);
143
144         &lock_file(&cron::cron_file($job));
145         if ($in{'sched'} && !$oldjob) {
146                 &cron::create_cron_job($job);
147                 $what = "backup_ccron";
148                 }
149         elsif (!$in{'sched'} && $oldjob) {
150                 # Need to delete cron job
151                 &cron::delete_cron_job($job);
152                 $what = "backup_dcron";
153                 }
154         elsif ($in{'sched'} && $oldjob) {
155                 # Need to update cron job
156                 &cron::change_cron_job($job);
157                 $what = "backup_ucron";
158                 }
159         else {
160                 $what = "backup_ncron";
161                 }
162         &unlock_file(&cron::cron_file($job));
163
164         # Tell the user what was done
165         print $text{$what},"<p>\n" if ($what);
166         }
167
168 &webmin_log("backup", undef, $in{'all'} ? "" : $in{'db'}, \%in);
169 if ($in{'all'}) {
170         &ui_print_footer("", $text{'index_return'});
171         }
172 else {
173         &ui_print_footer("edit_dbase.cgi?db=$in{'db'}", $text{'dbase_return'},
174                 "", $text{'index_return'});
175         }
176