]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/make_catalog_backup.pl.in
Backport from BEE
[bacula/bacula] / bacula / src / cats / 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-2014 Free Software Foundation Europe e.V.
18
19    The main author of Bacula is Kern Sibbald, with contributions from many
20    others, a complete list can be found in the file AUTHORS.
21
22    You may use this file and others of this release according to the
23    license defined in the LICENSE file, which includes the Affero General
24    Public License, v3.0 ("AGPLv3") and some additional permissions and
25    terms pursuant to its AGPLv3 Section 7.
26
27    Bacula® is a registered trademark of Kern Sibbald.
28
29 =cut
30
31 my $cat = shift or die "Usage: $0 catalogname";
32 my $dir_conf='@sbindir@/dbcheck -B -c @sysconfdir@/bacula-dir.conf';
33 my $wd = "@working_dir@";
34
35 sub dump_sqlite3
36 {
37     my %args = @_;
38
39     exec("echo .dump | sqlite3 '$wd/$args{db_name}.db' > '$wd/$args{db_name}.sql'");
40     print "Error while executing sqlite dump $!\n";
41     return 1;
42 }
43
44 # TODO: use just ENV and drop the pg_service.conf file
45 sub dump_pgsql
46 {
47     my %args = @_;
48     umask(0077);
49
50     if ($args{db_address}) {
51         $ENV{PGHOST}=$args{db_address};
52     }
53     if ($args{db_socket}) {
54         $ENV{PGHOST}=$args{db_socket};
55     }
56     if ($args{db_port}) {
57         $ENV{PGPORT}=$args{db_port};
58     }
59     if ($args{db_user}) {
60         $ENV{PGUSER}=$args{db_user};
61     }
62     if ($args{db_password}) {
63         $ENV{PGPASSWORD}=$args{db_password};
64     }
65     $ENV{PGDATABASE}=$args{db_name};
66     exec("HOME='$wd' pg_dump -c > '$wd/$args{db_name}.sql'");
67     print "Error while executing postgres dump $!\n";
68     return 1;               # in case of error
69 }
70
71 sub dump_mysql
72 {
73     my %args = @_;
74     umask(0077);
75     unlink("$wd/.my.cnf");
76     open(MY, ">$wd/.my.cnf")
77         or die "Can't open $wd/.my.cnf for writing $@";
78
79     $args{db_address} = $args{db_address} || "localhost";
80     my $addr = "host=$args{db_address}";
81     if ($args{db_socket}) {     # unix socket is fastest than net socket
82         $addr = "socket=$args{db_socket}";
83     }
84
85     print MY "[client]
86 $addr
87 user=$args{db_user}
88 password=$args{db_password}
89 ";
90     if ($args{db_port}) {
91         print MY "port=$args{db_port}\n";
92     }
93
94     close(MY);
95
96     exec("HOME='$wd' mysqldump -f --opt $args{db_name} > '$wd/$args{db_name}.sql'");
97     print "Error while executing mysql dump $!\n";
98     return 1;
99 }
100
101 sub dump_catalog
102 {
103     my %args = @_;
104     if ($args{db_type} eq 'SQLite3') {
105         $ENV{PATH}="@SQLITE_BINDIR@:$ENV{PATH}";
106         dump_sqlite3(%args);
107     } elsif ($args{db_type} eq 'PostgreSQL') {
108         $ENV{PATH}="@POSTGRESQL_BINDIR@:$ENV{PATH}";
109         dump_pgsql(%args);
110     } elsif ($args{db_type} eq 'MySQL') {
111         $ENV{PATH}="@MYSQL_BINDIR@:$ENV{PATH}";
112         dump_mysql(%args);
113     } else {
114         die "This database type isn't supported";
115     }
116 }
117
118 open(FP, "$dir_conf -C '$cat'|") or die "Can't get catalog information $@";
119 # catalog=MyCatalog
120 # db_type=SQLite
121 # db_name=regress
122 # db_driver=
123 # db_user=regress
124 # db_password=
125 # db_address=
126 # db_port=0
127 # db_socket=
128 my %cfg;
129
130 while(my $l = <FP>)
131 {
132     if ($l =~ /catalog=(.+)/) {
133         if (exists $cfg{catalog} and $cfg{catalog} eq $cat) {
134             exit dump_catalog(%cfg);
135         }
136         %cfg = ();              # reset
137     }
138
139     if ($l =~ /(\w+)=(.+)/) {
140         $cfg{$1}=$2;
141     }
142 }
143
144 if (exists $cfg{catalog} and $cfg{catalog} eq $cat) {
145     exit dump_catalog(%cfg);
146 }
147
148 print "Can't find your catalog ($cat) in director configuration\n";
149 exit 1;