Handle hostnames with upper-case letters
[webmin.git] / mysql / backup.pl
1 #!/usr/local/bin/perl
2 # backup.pl
3 # Called by cron to backup a database, or all databases
4
5 $no_acl_check++;
6 require './mysql-lib.pl';
7
8 if ($ARGV[0] eq "--all") {
9         $all = 1;
10         @dbs = grep { &supports_backup_db($_) } &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 "Before-backup command failed!\n";
24                 exit(1);
25                 }
26         }
27
28 # Check if MySQL is running
29 if (!$config{'host'}) {
30         ($r, $out) = &is_mysql_running();
31         if (!$r) {
32                 print "MySQL does not appear to be running : $out\n";
33                 print "Backups cannot be performed.\n";
34                 exit(1);
35                 }
36         }
37
38 $ex = 0;
39 foreach $db (@dbs) {
40         $sf = $all ? "" : $db;
41         if ($all) {
42                 $dir = &date_subs($config{'backup_'});
43                 &make_dir($dir, 0755) if ($config{'backup_mkdir_'});
44                 $file = $dir."/".$db.".sql".
45                         ($config{'backup_compress_'.$sf} == 1 ? ".gz" :
46                          $config{'backup_compress_'.$sf} == 2 ? ".bz2" : "");
47                 }
48         else {
49                 $file = &date_subs($config{'backup_'.$db});
50                 }
51         if (!$file) {
52                 print STDERR "No backup file set for database $db\n";
53                 exit(1);
54                 }
55         @compat = $config{'backup_compatible_'.$sf} ?
56                         ( $config{'backup_compatible_'.$sf} ) : ( );
57         push(@compat, split(/\0/, $in{'backup_options_'.$sf}));
58         @tables = split(/\s+/, $config{'backup_tables_'.$sf});
59
60         if (!$cmode) {
61                 # Run and check before-backup command (for one DB)
62                 $bok = &execute_before($db, STDOUT, 0, $file, $all ? undef : $db);
63                 if (!$bok) {
64                         print "Before-backup command failed!\n";
65                         $ex = 1;
66                         next;
67                         }
68                 }
69
70         # Do the backup
71         $err = &backup_database($db, $file,
72                                 $config{'backup_compress_'.$sf},
73                                 $config{'backup_drop_'.$sf},
74                                 $config{'backup_where_'.$sf},
75                                 $config{'backup_charset_'.$sf},
76                                 \@compat,
77                                 \@tables,
78                                 "root",
79                                 $config{'backup_single_'.$sf});
80         if ($err) {
81                 print "Backup of database $db to file $file failed:\n";
82                 print $out;
83                 $ex = 1;
84                 }
85         if (!$cmode) {
86                 &execute_after($db, STDOUT, 0, $file, $all ? undef : $db);
87                 }
88         }
89 if ($cmode) {
90         &execute_after(undef, STDOUT, 0, $config{'backup_'}, undef);
91         }
92 exit($ex);
93