make_catalog_backup.pl 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #!/usr/bin/env perl
  2. use strict;
  3. =head1 SCRIPT
  4. This script dumps your Bacula catalog in ASCII format
  5. It works for MySQL, SQLite, and PostgreSQL
  6. =head1 USAGE
  7. make_catalog_backup.pl MyCatalog
  8. =head1 LICENSE
  9. Bacula® - The Network Backup Solution
  10. Copyright (C) 2000-2010 Free Software Foundation Europe e.V.
  11. The main author of Bacula is Kern Sibbald, with contributions from
  12. many others, a complete list can be found in the file AUTHORS.
  13. This program is Free Software; you can redistribute it and/or
  14. modify it under the terms of version three of the GNU Affero General Public
  15. License as published by the Free Software Foundation plus additions
  16. that are listed in the file LICENSE.
  17. This program is distributed in the hope that it will be useful, but
  18. WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. General Public License for more details.
  21. You should have received a copy of the GNU Affero General Public License
  22. along with this program; if not, write to the Free Software
  23. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  24. 02110-1301, USA.
  25. Bacula® is a registered trademark of Kern Sibbald.
  26. The licensor of Bacula is the Free Software Foundation Europe
  27. (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zurich,
  28. Switzerland, email:ftf@fsfeurope.org.
  29. =cut
  30. my $cat = shift or die "Usage: $0 catalogname";
  31. my $dir_conf='/usr/sbin/dbcheck -B -c /etc/bacula/bacula-dir.conf';
  32. my $wd = "/var/lib/bacula";
  33. sub dump_sqlite3
  34. {
  35. my %args = @_;
  36. exec("echo .dump | sqlite3 '$wd/$args{db_name}.db' > '$wd/$args{db_name}.sql'");
  37. print "Error while executing sqlite dump $!\n";
  38. return 1;
  39. }
  40. # TODO: use just ENV and drop the pg_service.conf file
  41. sub dump_pgsql
  42. {
  43. my %args = @_;
  44. umask(0077);
  45. if ($args{db_address}) {
  46. $ENV{PGHOST}=$args{db_address};
  47. }
  48. if ($args{db_socket}) {
  49. $ENV{PGHOST}=$args{db_socket};
  50. }
  51. if ($args{db_port}) {
  52. $ENV{PGPORT}=$args{db_port};
  53. }
  54. $ENV{PGDATABASE}=$args{db_name};
  55. $ENV{PGUSER}=$args{db_user};
  56. $ENV{PGPASSWORD}=$args{db_password};
  57. exec("HOME='$wd' pg_dump -c > '$wd/$args{db_name}.sql'");
  58. print "Error while executing postgres dump $!\n";
  59. return 1; # in case of error
  60. }
  61. sub dump_mysql
  62. {
  63. my %args = @_;
  64. umask(0077);
  65. unlink("$wd/.my.cnf");
  66. open(MY, ">$wd/.my.cnf")
  67. or die "Can't open $wd/.my.cnf for writing $@";
  68. $args{db_address} = $args{db_address} || "localhost";
  69. my $addr = "host=$args{db_address}";
  70. if ($args{db_socket}) { # unix socket is fastest than net socket
  71. $addr = "socket=$args{db_socket}";
  72. }
  73. print MY "[client]
  74. $addr
  75. user=$args{db_user}
  76. password=$args{db_password}
  77. ";
  78. if ($args{db_port}) {
  79. print MY "port=$args{db_port}\n";
  80. }
  81. close(MY);
  82. exec("HOME='$wd' mysqldump -f --opt $args{db_name} > '$wd/$args{db_name}.sql'");
  83. print "Error while executing mysql dump $!\n";
  84. return 1;
  85. }
  86. sub dump_catalog
  87. {
  88. my %args = @_;
  89. if ($args{db_type} eq 'SQLite3') {
  90. $ENV{PATH}="/usr/bin:$ENV{PATH}";
  91. dump_sqlite3(%args);
  92. } elsif ($args{db_type} eq 'PostgreSQL') {
  93. $ENV{PATH}=":$ENV{PATH}";
  94. dump_pgsql(%args);
  95. } elsif ($args{db_type} eq 'MySQL') {
  96. $ENV{PATH}=":$ENV{PATH}";
  97. dump_mysql(%args);
  98. } else {
  99. die "This database type isn't supported";
  100. }
  101. }
  102. open(FP, "$dir_conf -C '$cat'|") or die "Can't get catalog information $@";
  103. # catalog=MyCatalog
  104. # db_type=SQLite
  105. # db_name=regress
  106. # db_driver=
  107. # db_user=regress
  108. # db_password=
  109. # db_address=
  110. # db_port=0
  111. # db_socket=
  112. my %cfg;
  113. while(my $l = <FP>)
  114. {
  115. if ($l =~ /catalog=(.+)/) {
  116. if (exists $cfg{catalog} and $cfg{catalog} eq $cat) {
  117. exit dump_catalog(%cfg);
  118. }
  119. %cfg = (); # reset
  120. }
  121. if ($l =~ /(\w+)=(.+)/) {
  122. $cfg{$1}=$2;
  123. }
  124. }
  125. if (exists $cfg{catalog} and $cfg{catalog} eq $cat) {
  126. exit dump_catalog(%cfg);
  127. }
  128. print "Can't find your catalog ($cat) in director configuration\n";
  129. exit 1;