]> git.sur5r.net Git - bacula/bacula/blob - bacula/scripts/make_catalog_backup.pl.in
f3efec2f343d362ec0dd24a0bd0307a2838a4bb3
[bacula/bacula] / bacula / scripts / 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 $ENV{PATH}="@SQL_BINDIR@:$ENV{PATH}";
45
46 my $cat = shift or die "Usage: $0 catalogname";
47 my $dir_conf='@sbindir@/dbcheck -B -c @sysconfdir@/bacula-dir.conf';
48 my $wd = "@working_dir@";
49
50 sub dump_sqlite
51 {
52     my %args = @_;
53
54     exec("echo .dump | sqlite '$wd/$args{db_name}.db' > '$wd/$args{db_name}.sql'");
55     print "Error while executing sqlite dump $!\n";
56     return 1;
57 }
58
59 sub dump_sqlite3
60 {
61     my %args = @_;
62
63     exec("echo .dump | sqlite3 '$wd/$args{db_name}.db' > '$wd/$args{db_name}.sql'");
64     print "Error while executing sqlite dump $!\n";
65     return 1;
66 }
67
68 # TODO: use just ENV and drop the pg_service.conf file
69 sub dump_pgsql
70 {
71     my %args = @_;
72     umask(0077);
73
74     if ($args{db_address}) {
75         $ENV{PGHOST}=$args{db_address};
76     }
77     if ($args{db_port}) {
78         $ENV{PGPORT}=$args{db_port};
79     }
80
81     $ENV{PGDATABASE}=$args{db_name};
82     $ENV{PGUSER}=$args{db_user};
83     $ENV{PGPASSWORD}=$args{db_password};
84     exec("HOME='$wd' pg_dump -c > '$wd/$args{db_name}.sql'");
85     print "Error while executing postgres dump $!\n";
86     return 1;               # in case of error
87 }
88
89 sub dump_mysql
90 {
91     my %args = @_;
92     umask(0700);
93     unlink("$wd/.my.cnf");
94     open(MY, ">$wd/.my.cnf") 
95         or die "Can't open $wd/.my.cnf for writing $@";
96     $args{db_address} = $args{db_address} || "localhost";
97     print MY "[client]
98 host=$args{db_address}
99 user=$args{db_user}
100 password=$args{db_password}
101 ";
102     if ($args{db_port}) {
103         print MY "port=%args{db_port}\n";
104     }
105     
106     close(MY);
107
108     exec("HOME='$wd' mysqldump -f --opt $args{db_name} > '$wd/$args{db_name}.sql'");
109     print "Error while executing mysql dump $!\n";
110     return 1;
111 }
112
113 sub dump_catalog
114 {
115     my %args = @_;
116     if ($args{db_type} eq 'SQLite') {
117         dump_sqlite(%args);
118     } elsif ($args{db_type} eq 'SQLite3') {
119         dump_sqlite3(%args);
120     } elsif ($args{db_type} eq 'PostgreSQL') {
121         dump_pgsql(%args);
122     } elsif ($args{db_type} eq 'MySQL') {
123         dump_mysql(%args);
124     } else {
125         die "This database type isn't supported";
126     }
127 }
128
129 open(FP, "$dir_conf|") or die "Can't get catalog information $@";
130 # catalog=MyCatalog
131 # db_type=SQLite
132 # db_name=regress
133 # db_driver=
134 # db_user=regress
135 # db_password=
136 # db_address=
137 # db_port=0
138 # db_socket=
139 my %cfg;
140
141 while(my $l = <FP>)
142 {
143     if ($l =~ /catalog=(.+)/) {
144         if (exists $cfg{catalog} and $cfg{catalog} eq $cat) {
145             exit dump_catalog(%cfg);
146         }
147         %cfg = ();              # reset
148     }
149
150     if ($l =~ /(\w+)=(.+)/) {
151         $cfg{$1}=$2;
152     }
153 }
154
155 if (exists $cfg{catalog} and $cfg{catalog} eq $cat) {
156     exit dump_catalog(%cfg);
157 }
158
159 print "Can't find your catalog ($cat) in director configuration\n";
160 exit 1;