]> git.sur5r.net Git - bacula/bacula/blob - bacula/patches/testing/make_catalog_backup.pl.in
ebl Add a new catalog backup script
[bacula/bacula] / bacula / patches / testing / make_catalog_backup.pl.in
1 #!/usr/bin/env perl
2 use strict;
3
4 =head1 SCRIPT
5
6   This script dumps your Bacula catalog in ASCII format
7   It works for MySQL, SQLite, and PostgreSQL
8
9 =head1 USAGE
10
11     make_catalog_backup.pl MyCatalog
12
13 =head1 LICENSE
14
15    Bacula® - The Network Backup Solution
16
17    Copyright (C) 2000-2008 Free Software Foundation Europe e.V.
18
19    The main author of Bacula is Kern Sibbald, with contributions from
20    many others, a complete list can be found in the file AUTHORS.
21
22    This program is Free Software; you can redistribute it and/or
23    modify it under the terms of version two of the GNU General Public
24    License as published by the Free Software Foundation plus additions
25    that are listed in the file LICENSE.
26
27    This program is distributed in the hope that it will be useful, but
28    WITHOUT ANY WARRANTY; without even the implied warranty of
29    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30    General Public License for more details.
31
32    You should have received a copy of the GNU General Public License
33    along with this program; if not, write to the Free Software
34    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
35    02110-1301, USA.
36
37    Bacula® is a registered trademark of Kern Sibbald.
38    The licensor of Bacula is the Free Software Foundation Europe
39    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zurich,
40    Switzerland, email:ftf@fsfeurope.org.
41
42 =cut
43
44 my $cat = shift or die "Usage: $0 catalogname";
45 my $dir='@sbindir@/bacula-dir -B -c @sysconfdir@/bacula-dir.conf';
46 my $wd = "@working_dir@";
47
48 sub dump_sqlite
49 {
50     my %args = @_;
51
52     exec("echo .dump | sqlite '$wd/$args{db_name}.db' > '$wd/$args{db_name}.sql'");
53     print "Error while executing sqlite dump $!\n";
54     return 1;
55 }
56
57 sub dump_sqlite3
58 {
59     my %args = @_;
60
61     exec("echo .dump | sqlite3 '$wd/$args{db_name}.db' > '$wd/$args{db_name}.sql'");
62     print "Error while executing sqlite dump $!\n";
63     return 1;
64 }
65
66 # TODO: use just ENV and drop the pg_service.conf file
67 sub dump_pgsql
68 {
69     my %args = @_;
70     umask(0077);
71     unlink("$wd/pg_service.conf");
72     open(PG, ">$wd/pg_service.conf") 
73         or die "Can't open $wd/pg_service.conf for writing $@";
74     print PG "[bacula]
75 dbname=$args{db_name}
76 user=$args{db_user}
77 password=$args{db_password}
78 ";
79     if ($args{db_address}) {
80         print PG "address=$args{db_address}\n";
81         $ENV{PGHOST}=$args{db_address};
82     }
83     if ($args{db_port}) {
84         print PG "port=$args{db_port}\n";
85         $ENV{PGPORT}=$args{db_port};
86     }
87     
88     close(PG);
89     $ENV{PGDATABASE}=$args{db_name};
90     $ENV{PGUSER}=$args{db_user};
91     $ENV{PGPASSWORD}=$args{db_password};
92     exec("HOME='$wd' PGSERVICE=bacula PGSYSCONFDIR='$wd' pg_dump -c > '$wd/$args{db_name}.sql'");
93     print "Error while executing postgres dump $!\n";
94     return 1;               # in case of error
95 }
96
97 sub dump_mysql
98 {
99     my %args = @_;
100     umask(0700);
101     unlink("$wd/.my.cnf");
102     open(MY, ">$wd/.my.cnf") 
103         or die "Can't open $wd/.my.cnf for writing $@";
104     $args{db_address} = $args{db_address} || "localhost";
105     print MY "[client]
106 host=$args{db_address}
107 user=$args{db_user}
108 password=$args{db_password}
109 ";
110     if ($args{db_port}) {
111         print MY "port=%args{db_port}\n";
112     }
113     
114     close(MY);
115
116     exec("HOME='$wd' mysqldump -f --opt $args{db_name} > '$wd/$args{db_name}.sql'");
117     print "Error while executing mysql dump $!\n";
118     return 1;
119 }
120
121 sub dump_catalog
122 {
123     my %args = @_;
124     if ($args{db_type} eq 'SQLite') {
125         dump_sqlite(%args);
126     } elsif ($args{db_type} eq 'SQLite3') {
127         dump_sqlite3(%args);
128     } elsif ($args{db_type} eq 'PostgreSQL') {
129         dump_pgsql(%args);
130     } elsif ($args{db_type} eq 'MySQL') {
131         dump_mysql(%args);
132     } else {
133         die "This database type isn't supported";
134     }
135 }
136
137 open(FP, "$dir|") or die "Can't get catalog information $@";
138 # catalog=MyCatalog
139 # db_type=SQLite
140 # db_name=regress
141 # db_driver=
142 # db_user=regress
143 # db_password=
144 # db_address=
145 # db_port=0
146 # db_socket=
147 my %cfg;
148
149 while(my $l = <FP>)
150 {
151     if ($l =~ /catalog=(.+)/) {
152         if (exists $cfg{catalog} and $cfg{catalog} eq $cat) {
153             exit dump_catalog(%cfg);
154         }
155         %cfg = ();              # reset
156     }
157
158     if ($l =~ /(\w+)=(.+)/) {
159         $cfg{$1}=$2;
160     }
161 }
162
163 if (exists $cfg{catalog} and $cfg{catalog} eq $cat) {
164     exit dump_catalog(%cfg);
165 }
166
167 print "Can't find your catalog ($cat) in director configuration\n";
168 exit 1;