Merge pull request #2220 from pyhalov/desktop-cache
[unleashed-userland.git] / components / bacula / files / make_catalog_backup.pl
blob153e2b2f51a4759a19500b2fb4fa7a064cc48e76
1 #!/usr/bin/perl
2 use strict;
4 =head1 SCRIPT
6 This script dumps your Bacula catalog in ASCII format
7 It works for MySQL, SQLite, and PostgreSQL
9 =head1 USAGE
11 make_catalog_backup.pl MyCatalog
13 =head1 LICENSE
15 Bacula® - The Network Backup Solution
17 Copyright (C) 2000-2010 Free Software Foundation Europe e.V.
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.
22 This program is Free Software; you can redistribute it and/or
23 modify it under the terms of version three of the GNU Affero General Public
24 License as published by the Free Software Foundation plus additions
25 that are listed in the file LICENSE.
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.
32 You should have received a copy of the GNU Affero 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.
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.
42 =cut
44 my $cat = shift or die "Usage: $0 catalogname";
45 my $dir_conf='/usr/sbin/dbcheck -B -c /etc/bacula/bacula-dir.conf';
46 my $wd = "/var/bacula";
48 sub dump_sqlite3
50 my %args = @_;
52 exec("echo .dump | sqlite3 '$wd/$args{db_name}.db' > '$wd/$args{db_name}.sql'");
53 print "Error while executing sqlite dump $!\n";
54 return 1;
57 # TODO: use just ENV and drop the pg_service.conf file
58 sub dump_pgsql
60 my %args = @_;
61 umask(0077);
63 if ($args{db_address}) {
64 $ENV{PGHOST}=$args{db_address};
66 if ($args{db_socket}) {
67 $ENV{PGHOST}=$args{db_socket};
69 if ($args{db_port}) {
70 $ENV{PGPORT}=$args{db_port};
72 if ($args{db_user}) {
73 $ENV{PGUSER}=$args{db_user};
75 if ($args{db_password}) {
76 $ENV{PGPASSWORD}=$args{db_password};
78 $ENV{PGDATABASE}=$args{db_name};
79 exec("HOME='$wd' pg_dump -c > '$wd/$args{db_name}.sql'");
80 print "Error while executing postgres dump $!\n";
81 return 1; # in case of error
84 sub dump_mysql
86 my %args = @_;
87 umask(0077);
88 unlink("$wd/.my.cnf");
89 open(MY, ">$wd/.my.cnf")
90 or die "Can't open $wd/.my.cnf for writing $@";
92 $args{db_address} = $args{db_address} || "localhost";
93 my $addr = "host=$args{db_address}";
94 if ($args{db_socket}) { # unix socket is fastest than net socket
95 $addr = "socket=$args{db_socket}";
98 print MY "[client]
99 $addr
100 user=$args{db_user}
101 password=$args{db_password}
103 if ($args{db_port}) {
104 print MY "port=$args{db_port}\n";
107 close(MY);
109 exec("HOME='$wd' mysqldump -f --opt $args{db_name} > '$wd/$args{db_name}.sql'");
110 print "Error while executing mysql dump $!\n";
111 return 1;
114 sub dump_catalog
116 my %args = @_;
117 if ($args{db_type} eq 'SQLite3') {
118 $ENV{PATH}="/usr/bin:$ENV{PATH}";
119 dump_sqlite3(%args);
120 } elsif ($args{db_type} eq 'PostgreSQL') {
121 $ENV{PATH}="/usr/postgres/9.3/bin:$ENV{PATH}";
122 dump_pgsql(%args);
123 } elsif ($args{db_type} eq 'MySQL') {
124 $ENV{PATH}="/usr/mariadb/5.5/bin:$ENV{PATH}";
125 dump_mysql(%args);
126 } else {
127 die "This database type isn't supported";
131 open(FP, "$dir_conf -C '$cat'|") or die "Can't get catalog information $@";
132 # catalog=MyCatalog
133 # db_type=SQLite
134 # db_name=regress
135 # db_driver=
136 # db_user=regress
137 # db_password=
138 # db_address=
139 # db_port=0
140 # db_socket=
141 my %cfg;
143 while(my $l = <FP>)
145 if ($l =~ /catalog=(.+)/) {
146 if (exists $cfg{catalog} and $cfg{catalog} eq $cat) {
147 exit dump_catalog(%cfg);
149 %cfg = (); # reset
152 if ($l =~ /(\w+)=(.+)/) {
153 $cfg{$1}=$2;
157 if (exists $cfg{catalog} and $cfg{catalog} eq $cat) {
158 exit dump_catalog(%cfg);
161 print "Can't find your catalog ($cat) in director configuration\n";
162 exit 1;