From 53bb6f9d3c0a0c41f660c4286a6ecabbf40a0628 Mon Sep 17 00:00:00 2001 From: Eric Bollengier Date: Wed, 6 Jan 2010 10:15:56 +0100 Subject: [PATCH] Add make_catalog_backup.pl script that uses env variables and disk file to pass database password for backup --- bacula/autoconf/configure.in | 3 +- bacula/configure | 5 +- bacula/scripts/make_catalog_backup.pl.in | 160 +++++++++++++++++++++++ 3 files changed, 165 insertions(+), 3 deletions(-) create mode 100644 bacula/scripts/make_catalog_backup.pl.in diff --git a/bacula/autoconf/configure.in b/bacula/autoconf/configure.in index 25a4905219..c6e331e25c 100644 --- a/bacula/autoconf/configure.in +++ b/bacula/autoconf/configure.in @@ -2978,6 +2978,7 @@ AC_OUTPUT([autoconf/Make.common \ scripts/bat.desktop.xsu \ scripts/bat.desktop.consolehelper \ scripts/bat.console_apps \ + scripts/make_catalog_backup.pl \ src/Makefile \ src/host.h \ src/console/Makefile \ @@ -3091,7 +3092,7 @@ cd ${BUILD_DIR} cd scripts chmod 755 startmysql stopmysql bacula startit stopit btraceback mtx-changer -chmod 755 dvd-handler dvd-simulator +chmod 755 dvd-handler dvd-simulator make_catalog_backup.pl chmod 755 bconsole gconsole mtx-changer devel_bacula logrotate cd .. diff --git a/bacula/configure b/bacula/configure index 089ebe6c0b..a834afc954 100755 --- a/bacula/configure +++ b/bacula/configure @@ -29124,7 +29124,7 @@ if test "x${subsysdir}" = "x${sbindir}" ; then exit 1 fi -ac_config_files="$ac_config_files autoconf/Make.common Makefile manpages/Makefile scripts/startmysql scripts/stopmysql scripts/btraceback scripts/startit scripts/stopit scripts/bconsole scripts/gconsole scripts/bacula scripts/bacula-ctl-dir scripts/bacula-ctl-fd scripts/bacula-ctl-sd scripts/devel_bacula scripts/Makefile scripts/logrotate scripts/bacula.desktop.gnome1 scripts/bacula.desktop.gnome2 scripts/bacula.desktop.gnome1.consolehelper scripts/bacula.desktop.gnome2.consolehelper scripts/bacula.desktop.gnome1.xsu scripts/bacula.desktop.gnome2.xsu scripts/bgnome-console.console_apps scripts/mtx-changer scripts/disk-changer scripts/dvd-handler scripts/dvd-simulator scripts/bacula-tray-monitor.desktop scripts/logwatch/Makefile scripts/logwatch/logfile.bacula.conf scripts/wxconsole.console_apps scripts/wxconsole.desktop.consolehelper scripts/wxconsole.desktop.xsu scripts/bat.desktop scripts/bat.desktop.xsu scripts/bat.desktop.consolehelper scripts/bat.console_apps src/Makefile src/host.h src/console/Makefile src/console/bconsole.conf src/qt-console/bat.conf src/qt-console/bat.pro src/qt-console/bat.pro.mingw32 src/qt-console/install_conf_file src/wx-console/Makefile src/wx-console/bwx-console.conf src/tray-monitor/Makefile src/tray-monitor/tray-monitor.conf src/dird/Makefile src/dird/bacula-dir.conf src/lib/Makefile src/stored/Makefile src/stored/bacula-sd.conf src/filed/Makefile src/filed/bacula-fd.conf src/cats/Makefile src/cats/make_catalog_backup src/cats/delete_catalog_backup src/cats/create_postgresql_database src/cats/update_postgresql_tables src/cats/make_postgresql_tables src/cats/grant_postgresql_privileges src/cats/drop_postgresql_tables src/cats/drop_postgresql_database src/cats/create_mysql_database src/cats/update_mysql_tables src/cats/make_mysql_tables src/cats/grant_mysql_privileges src/cats/drop_mysql_tables src/cats/drop_mysql_database src/cats/create_sqlite3_database src/cats/update_sqlite3_tables src/cats/make_sqlite3_tables src/cats/grant_sqlite3_privileges src/cats/drop_sqlite3_tables src/cats/drop_sqlite3_database src/cats/create_ingres_database src/cats/update_ingres_tables src/cats/make_ingres_tables src/cats/grant_ingres_privileges src/cats/drop_ingres_tables src/cats/drop_ingres_database src/cats/sqlite src/cats/mysql src/cats/create_bdb_database src/cats/update_bdb_tables src/cats/make_bdb_tables src/cats/grant_bdb_privileges src/cats/drop_bdb_tables src/cats/drop_bdb_database src/cats/create_bacula_database src/cats/update_bacula_tables src/cats/grant_bacula_privileges src/cats/make_bacula_tables src/cats/drop_bacula_tables src/cats/drop_bacula_database src/findlib/Makefile src/tools/Makefile src/plugins/fd/Makefile src/plugins/sd/Makefile src/plugins/dir/Makefile src/win32/Makefile.inc po/Makefile.in updatedb/update_mysql_tables_9_to_10 updatedb/update_sqlite3_tables_9_to_10 updatedb/update_postgresql_tables_9_to_10 updatedb/update_mysql_tables_10_to_11 updatedb/update_sqlite3_tables_10_to_11 updatedb/update_postgresql_tables_10_to_11 examples/nagios/check_bacula/Makefile $PFILES" +ac_config_files="$ac_config_files autoconf/Make.common Makefile manpages/Makefile scripts/startmysql scripts/stopmysql scripts/btraceback scripts/startit scripts/stopit scripts/bconsole scripts/gconsole scripts/bacula scripts/bacula-ctl-dir scripts/bacula-ctl-fd scripts/bacula-ctl-sd scripts/devel_bacula scripts/Makefile scripts/logrotate scripts/bacula.desktop.gnome1 scripts/bacula.desktop.gnome2 scripts/bacula.desktop.gnome1.consolehelper scripts/bacula.desktop.gnome2.consolehelper scripts/bacula.desktop.gnome1.xsu scripts/bacula.desktop.gnome2.xsu scripts/bgnome-console.console_apps scripts/mtx-changer scripts/disk-changer scripts/dvd-handler scripts/dvd-simulator scripts/bacula-tray-monitor.desktop scripts/logwatch/Makefile scripts/logwatch/logfile.bacula.conf scripts/wxconsole.console_apps scripts/wxconsole.desktop.consolehelper scripts/wxconsole.desktop.xsu scripts/bat.desktop scripts/bat.desktop.xsu scripts/bat.desktop.consolehelper scripts/bat.console_apps scripts/make_catalog_backup.pl src/Makefile src/host.h src/console/Makefile src/console/bconsole.conf src/qt-console/bat.conf src/qt-console/bat.pro src/qt-console/bat.pro.mingw32 src/qt-console/install_conf_file src/wx-console/Makefile src/wx-console/bwx-console.conf src/tray-monitor/Makefile src/tray-monitor/tray-monitor.conf src/dird/Makefile src/dird/bacula-dir.conf src/lib/Makefile src/stored/Makefile src/stored/bacula-sd.conf src/filed/Makefile src/filed/bacula-fd.conf src/cats/Makefile src/cats/make_catalog_backup src/cats/delete_catalog_backup src/cats/create_postgresql_database src/cats/update_postgresql_tables src/cats/make_postgresql_tables src/cats/grant_postgresql_privileges src/cats/drop_postgresql_tables src/cats/drop_postgresql_database src/cats/create_mysql_database src/cats/update_mysql_tables src/cats/make_mysql_tables src/cats/grant_mysql_privileges src/cats/drop_mysql_tables src/cats/drop_mysql_database src/cats/create_sqlite3_database src/cats/update_sqlite3_tables src/cats/make_sqlite3_tables src/cats/grant_sqlite3_privileges src/cats/drop_sqlite3_tables src/cats/drop_sqlite3_database src/cats/create_ingres_database src/cats/update_ingres_tables src/cats/make_ingres_tables src/cats/grant_ingres_privileges src/cats/drop_ingres_tables src/cats/drop_ingres_database src/cats/sqlite src/cats/mysql src/cats/create_bdb_database src/cats/update_bdb_tables src/cats/make_bdb_tables src/cats/grant_bdb_privileges src/cats/drop_bdb_tables src/cats/drop_bdb_database src/cats/create_bacula_database src/cats/update_bacula_tables src/cats/grant_bacula_privileges src/cats/make_bacula_tables src/cats/drop_bacula_tables src/cats/drop_bacula_database src/findlib/Makefile src/tools/Makefile src/plugins/fd/Makefile src/plugins/sd/Makefile src/plugins/dir/Makefile src/win32/Makefile.inc po/Makefile.in updatedb/update_mysql_tables_9_to_10 updatedb/update_sqlite3_tables_9_to_10 updatedb/update_postgresql_tables_9_to_10 updatedb/update_mysql_tables_10_to_11 updatedb/update_sqlite3_tables_10_to_11 updatedb/update_postgresql_tables_10_to_11 examples/nagios/check_bacula/Makefile $PFILES" ac_config_commands="$ac_config_commands default" @@ -30219,6 +30219,7 @@ do "scripts/bat.desktop.xsu") CONFIG_FILES="$CONFIG_FILES scripts/bat.desktop.xsu" ;; "scripts/bat.desktop.consolehelper") CONFIG_FILES="$CONFIG_FILES scripts/bat.desktop.consolehelper" ;; "scripts/bat.console_apps") CONFIG_FILES="$CONFIG_FILES scripts/bat.console_apps" ;; + "scripts/make_catalog_backup.pl") CONFIG_FILES="$CONFIG_FILES scripts/make_catalog_backup.pl" ;; "src/Makefile") CONFIG_FILES="$CONFIG_FILES src/Makefile" ;; "src/host.h") CONFIG_FILES="$CONFIG_FILES src/host.h" ;; "src/console/Makefile") CONFIG_FILES="$CONFIG_FILES src/console/Makefile" ;; @@ -31864,7 +31865,7 @@ cd ${BUILD_DIR} cd scripts chmod 755 startmysql stopmysql bacula startit stopit btraceback mtx-changer -chmod 755 dvd-handler dvd-simulator breload make_catalog_backup.pl +chmod 755 dvd-handler dvd-simulator make_catalog_backup.pl chmod 755 bconsole gconsole mtx-changer devel_bacula logrotate cd .. diff --git a/bacula/scripts/make_catalog_backup.pl.in b/bacula/scripts/make_catalog_backup.pl.in new file mode 100644 index 0000000000..f3efec2f34 --- /dev/null +++ b/bacula/scripts/make_catalog_backup.pl.in @@ -0,0 +1,160 @@ +#!/usr/bin/env perl +use strict; + +=head1 SCRIPT + + This script dumps your Bacula catalog in ASCII format + It works for MySQL, SQLite, and PostgreSQL + +=head1 USAGE + + make_catalog_backup.pl MyCatalog + +=head1 LICENSE + + Bacula® - The Network Backup Solution + + Copyright (C) 2000-2008 Free Software Foundation Europe e.V. + + The main author of Bacula is Kern Sibbald, with contributions from + many others, a complete list can be found in the file AUTHORS. + + This program is Free Software; you can redistribute it and/or + modify it under the terms of version two of the GNU General Public + License as published by the Free Software Foundation plus additions + that are listed in the file LICENSE. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. + + Bacula® is a registered trademark of Kern Sibbald. + The licensor of Bacula is the Free Software Foundation Europe + (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zurich, + Switzerland, email:ftf@fsfeurope.org. + +=cut + +$ENV{PATH}="@SQL_BINDIR@:$ENV{PATH}"; + +my $cat = shift or die "Usage: $0 catalogname"; +my $dir_conf='@sbindir@/dbcheck -B -c @sysconfdir@/bacula-dir.conf'; +my $wd = "@working_dir@"; + +sub dump_sqlite +{ + my %args = @_; + + exec("echo .dump | sqlite '$wd/$args{db_name}.db' > '$wd/$args{db_name}.sql'"); + print "Error while executing sqlite dump $!\n"; + return 1; +} + +sub dump_sqlite3 +{ + my %args = @_; + + exec("echo .dump | sqlite3 '$wd/$args{db_name}.db' > '$wd/$args{db_name}.sql'"); + print "Error while executing sqlite dump $!\n"; + return 1; +} + +# TODO: use just ENV and drop the pg_service.conf file +sub dump_pgsql +{ + my %args = @_; + umask(0077); + + if ($args{db_address}) { + $ENV{PGHOST}=$args{db_address}; + } + if ($args{db_port}) { + $ENV{PGPORT}=$args{db_port}; + } + + $ENV{PGDATABASE}=$args{db_name}; + $ENV{PGUSER}=$args{db_user}; + $ENV{PGPASSWORD}=$args{db_password}; + exec("HOME='$wd' pg_dump -c > '$wd/$args{db_name}.sql'"); + print "Error while executing postgres dump $!\n"; + return 1; # in case of error +} + +sub dump_mysql +{ + my %args = @_; + umask(0700); + unlink("$wd/.my.cnf"); + open(MY, ">$wd/.my.cnf") + or die "Can't open $wd/.my.cnf for writing $@"; + $args{db_address} = $args{db_address} || "localhost"; + print MY "[client] +host=$args{db_address} +user=$args{db_user} +password=$args{db_password} +"; + if ($args{db_port}) { + print MY "port=%args{db_port}\n"; + } + + close(MY); + + exec("HOME='$wd' mysqldump -f --opt $args{db_name} > '$wd/$args{db_name}.sql'"); + print "Error while executing mysql dump $!\n"; + return 1; +} + +sub dump_catalog +{ + my %args = @_; + if ($args{db_type} eq 'SQLite') { + dump_sqlite(%args); + } elsif ($args{db_type} eq 'SQLite3') { + dump_sqlite3(%args); + } elsif ($args{db_type} eq 'PostgreSQL') { + dump_pgsql(%args); + } elsif ($args{db_type} eq 'MySQL') { + dump_mysql(%args); + } else { + die "This database type isn't supported"; + } +} + +open(FP, "$dir_conf|") or die "Can't get catalog information $@"; +# catalog=MyCatalog +# db_type=SQLite +# db_name=regress +# db_driver= +# db_user=regress +# db_password= +# db_address= +# db_port=0 +# db_socket= +my %cfg; + +while(my $l = ) +{ + if ($l =~ /catalog=(.+)/) { + if (exists $cfg{catalog} and $cfg{catalog} eq $cat) { + exit dump_catalog(%cfg); + } + %cfg = (); # reset + } + + if ($l =~ /(\w+)=(.+)/) { + $cfg{$1}=$2; + } +} + +if (exists $cfg{catalog} and $cfg{catalog} eq $cat) { + exit dump_catalog(%cfg); +} + +print "Can't find your catalog ($cat) in director configuration\n"; +exit 1; -- 2.39.5