Handle hostnames with upper-case letters
[webmin.git] / postgresql / backup.pl
1 #!/usr/local/bin/perl
2 # backup.pl
3 # Called by cron to backup a database
4
5 $no_acl_check++;
6 require './postgresql-lib.pl';
7
8 if ($ARGV[0] eq "--all") {
9         $all = 1;
10         @dbs = &list_databases();
11         $cmode = $config{'backup_cmode_'};
12         }
13 else {
14         $ARGV[0] || die "Missing database parameter";
15         @dbs = ( $ARGV[0] );
16         $cmode = 0;
17         }
18
19 if ($cmode) {
20         # Run and check before-backup command (for all DBs)
21         $bok = &execute_before(undef, STDOUT, 0, $config{'backup_'}, undef);
22         if (!$bok) {
23                 print STDERR "Before-backup command failed!\n";
24                 exit(1);
25                 }
26         }
27 $ex = 0;
28 foreach $db (@dbs) {
29         $sf = $all ? "" : $db;
30         if (!&accepting_connections($db)) {
31                 #print STDERR "Database $db is not accepting connections.\n";
32                 next;
33                 }
34         $format = $config{'backup_format_'.$sf};
35         $mkdir = $config{'backup_mkdir_'.$sf};
36         $suf = $format eq "p" ? "sql" :
37                $format eq "t" ? "tar" : "post";
38         if ($all) {
39                 $dir = &date_subs($config{'backup_'});
40                 $file = "$dir/$db.$suf";
41                 &make_backup_dir($dir) if ($mkdir);
42                 }
43         else {
44                 $file = &date_subs($config{'backup_'.$db});
45                 }
46         @tables = split(/\s+/, $config{'backup_tables_'.$sf});
47         if (!$file) {
48                 print STDERR "No backup file set for database $db\n";
49                 exit(1);
50                 }
51
52         if (!$cmode) {
53                 # Run and check before-backup command (for one DB)
54                 $bok = &execute_before($db, STDOUT, 0, $file, $all ? undef : $db);
55                 if (!$bok) {
56                         print STDERR "Before-backup command failed!\n";
57                         $ex = 1;
58                         next;
59                         }
60                 }
61
62         unlink($file);
63         $err = &backup_database($db, $file, $format, \@tables);
64         if ($err) {
65                 print STDERR "Backup of database $db to file $file failed:\n";
66                 print STDERR $err;
67                 $ex = 1;
68                 }
69         if (!$cmode) {
70                 &execute_after($db, STDOUT, 0, $file, $all ? undef : $db);
71                 }
72         }
73 if ($cmode) {
74         &execute_after(undef, STDOUT, 0, $config{'backup_'}, undef);
75         }
76 exit($ex);
77