]> git.sur5r.net Git - bacula/bacula/blob - bacula/patches/testing/make_catalog_backup.pl.in
606890f9c0d39bb46d157d4b4e6a3c25cffbe971
[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_conf='@sbindir@/dbcheck -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
72     if ($args{db_address}) {
73         $ENV{PGHOST}=$args{db_address};
74     }
75     if ($args{db_port}) {
76         $ENV{PGPORT}=$args{db_port};
77     }
78
79     $ENV{PGDATABASE}=$args{db_name};
80     $ENV{PGUSER}=$args{db_user};
81     $ENV{PGPASSWORD}=$args{db_password};
82     exec("HOME='$wd' pg_dump -c > '$wd/$args{db_name}.sql'");
83     print "Error while executing postgres dump $!\n";
84     return 1;               # in case of error
85 }
86
87 sub dump_mysql
88 {
89     my %args = @_;
90     umask(0700);
91     unlink("$wd/.my.cnf");
92     open(MY, ">$wd/.my.cnf") 
93         or die "Can't open $wd/.my.cnf for writing $@";
94     $args{db_address} = $args{db_address} || "localhost";
95     print MY "[client]
96 host=$args{db_address}
97 user=$args{db_user}
98 password=$args{db_password}
99 ";
100     if ($args{db_port}) {
101         print MY "port=%args{db_port}\n";
102     }
103     
104     close(MY);
105
106     exec("HOME='$wd' mysqldump -f --opt $args{db_name} > '$wd/$args{db_name}.sql'");
107     print "Error while executing mysql dump $!\n";
108     return 1;
109 }
110
111 sub dump_catalog
112 {
113     my %args = @_;
114     if ($args{db_type} eq 'SQLite') {
115         dump_sqlite(%args);
116     } elsif ($args{db_type} eq 'SQLite3') {
117         dump_sqlite3(%args);
118     } elsif ($args{db_type} eq 'PostgreSQL') {
119         dump_pgsql(%args);
120     } elsif ($args{db_type} eq 'MySQL') {
121         dump_mysql(%args);
122     } else {
123         die "This database type isn't supported";
124     }
125 }
126
127 open(FP, "$dir_conf|") or die "Can't get catalog information $@";
128 # catalog=MyCatalog
129 # db_type=SQLite
130 # db_name=regress
131 # db_driver=
132 # db_user=regress
133 # db_password=
134 # db_address=
135 # db_port=0
136 # db_socket=
137 my %cfg;
138
139 while(my $l = <FP>)
140 {
141     if ($l =~ /catalog=(.+)/) {
142         if (exists $cfg{catalog} and $cfg{catalog} eq $cat) {
143             exit dump_catalog(%cfg);
144         }
145         %cfg = ();              # reset
146     }
147
148     if ($l =~ /(\w+)=(.+)/) {
149         $cfg{$1}=$2;
150     }
151 }
152
153 if (exists $cfg{catalog} and $cfg{catalog} eq $cat) {
154     exit dump_catalog(%cfg);
155 }
156
157 print "Can't find your catalog ($cat) in director configuration\n";
158 exit 1;