2 # This program is free software; you can redistribute it and/or modify
3 # it under the terms of the GNU General Public License as published by
4 # the Free Software Foundation; either version 2 of the License, or
5 # (at your option) any later version.
7 # This program is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 # GNU General Public License for more details.
12 # You should have received a copy of the GNU General Public License
13 # along with this program; if not, write to:
15 # Free Software Foundation, Inc.
16 # 59 Temple Place - Suite 330
17 # Boston, MA 02111-1307, USA.
19 # Rudimentary switch parsing. Must be in main package.
24 use POSIX qw(setuid setgid);
26 use File::Temp qw(tempdir);
27 use File::Path qw(rmtree);
30 # path to Apache HTTPd-style config
31 my $config = '/etc/bacula/backup-mysql.conf';
32 my $c = new BBM::Config($config);
34 # now change to user mysql after we've read config
36 my $uid = getpwnam('mysql');
37 my $gid = getgrnam('mysql');
38 die "Can't find user/group mysql\n" unless $uid or $gid;
40 # CWD could not be accessible for mysql user
49 my $backup_dir = $c->get('options', 'outdir') or die "'outdir' not defined in config\n";
50 my $tmpdir = $c->get('options', 'tmpdir') or die "'tmpdir' not defined in config\n";
52 if (!-d $backup_dir && !mkdir($backup_dir) && !-d $backup_dir) {
53 die "backup dir '$backup_dir' not present and can't be created\n";
55 if (!-d $tmpdir && !mkdir($tmpdir) && !-d $tmpdir) {
56 die "tmpdir '$tmpdir' not present and can't be created\n";
59 # process each cluster
60 for my $cluster ($c->get('clusters', 'cluster')) {
61 print ">>> $cluster\n";
63 cleanup_cluster($cluster);
65 backup_cluster($cluster);
67 print "<<< $cluster\n";
71 # Usage: mysqlhotcopy $CLUSTER $DATABASE $USERNAME $PASSWORD $SOCKET
74 my ($cluster, $database, $user, $password, $socket) = @_;
76 print ">>>> mysqlhotcopy $database\n";
78 my $dstdir = tempdir("bbm.XXXXXX", DIR => $tmpdir);
80 # make backup with mysqlhotcopy
81 my @shell = ('mysqlhotcopy');
82 push(@shell, '-u', $user) if $user;
83 push(@shell, '-p', $password) if $password;
84 push(@shell, '-S', $socket) if $socket;
85 push(@shell, $database, $dstdir);
86 system(@shell) == 0 or die "mysqlhotcopy failed: $?\n";
88 # put it to "production dir"
89 my $cluster_dir = "$backup_dir/$cluster";
90 if (!-d $cluster_dir && !mkdir($cluster_dir) && !-d $cluster_dir) {
92 die "cluster dir '$cluster_dir' not present and can't be created\n";
95 my $dirname = "$backup_dir/$cluster/$database";
100 my $srcdir = "$dstdir/$database";
101 unless (rename($srcdir, $dirname)) {
103 die "Rename '$srcdir'->'$dirname' failed: $!\n";
106 rmdir($dstdir) or warn $!;
108 print "<<<< mysqlhotcopy $database\n";
111 sub cleanup_cluster {
113 my $cluster_dir = "$backup_dir/$cluster";
114 print ">>>> cleanup $cluster_dir\n";
115 rmtree($cluster_dir);
116 print "<<<< cleanup $cluster_dir\n";
122 # get db connection info
123 my $user = $c->get($cluster, 'user') || $c->get('client', 'user');
124 my $password = $c->get($cluster, 'password') || $c->get('client', 'password');
125 my $socket = $c->get($cluster, 'socket') || $c->get('client', 'socket');
127 # get databases to backup
128 my @include = $c->get($cluster, 'include_database');
129 my @exclude = $c->get($cluster, 'exclude_database');
131 # start with include list
132 my %dbs = map { $_ => 1 } @include;
134 if (@exclude or !@include) {
135 my $dbh = new BBM::DB($user, $password, $socket);
136 my $sth = $dbh->prepare("show databases");
138 while (my($dbname) = $sth->fetchrow_array) {
139 next if lc($dbname) eq 'information_schema';
145 # remove excluded databases
146 delete @dbs{@exclude};
149 foreach my $db (keys %dbs) {
150 mysqlhotcopy($cluster, $db, $user, $password, $socket);
157 # DB class for simple Database connection
160 my $class = ref($self) || $self;
162 my ($user, $password, $socket) = @_;
164 $dsn .= "mysql_socket=$socket" if $socket;
165 my $dbh = DBI->connect("DBI:mysql:$dsn", $user, $password, { PrintError => 0, RaiseError => 1 });
175 my $class = ref($self) || $self;
178 my $config = new Config::General(-ConfigFile => $file, -LowerCaseNames => 1);
179 my $this = { $config->getall() };
180 bless($this, $class);
184 my ($self, $section, $key) = @_;
187 # descend to [cluster] if $section not present in root tree
188 unless (exists $h->{$section}) {
192 # pay attention if callee wanted arrays
193 return wantarray ? () : undef unless exists $h->{$section};
194 return wantarray ? () : undef unless exists $h->{$section}->{$key};
196 # deref if wanted array and is arrayref
197 return @{$h->{$section}->{$key}} if wantarray && ref $h->{$section}->{$key} eq 'ARRAY';
199 return $h->{$section}->{$key};
207 bacula-backup-mysql - A hook for Bacula to backup mysql databases using mysqlhotcopy.
212 Name = "example.org-mysql"
214 # This prepares the backup
215 Client Run Before Job = "/usr/sbin/bacula-backup-mysql"
216 # This deletes the copy of the catalog
217 Client Run After Job = "/usr/sbin/bacula-backup-mysql -cleanup"
222 This is a script to be setup as C<Client Run Before Job> in Bacula.
226 Copyright (C) 2009-2010, Elan RuusamE<auml>e <glen@delfi.ee>
230 http://www.bacula.org/