Package: backup-manager
Version: 0.7.9-3
Severity: wishlist
Tags: upstream patch

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Attached you will find a patch (backup-method-postgresql.diff) to add a 
"postgresql" method
to backup manager. AFAIK PostgreSQL is the default DBMS installed by Debian 
Installer, thus
supplying such a backup method would be useful.

A second, optional, patch (rename-mysql-databases.diff) renames the dump 
created by mysql 
backup method for individual database(s) so that it contains the DBMS 
identifier, just 
like the postgres method supplied here does. It is not essential, but I thought 
it makes
sense, especially in view of possible future addition of other DBMS dumping 
methods.

The code uses the pg_dump and pg_dumpall utilities of postgresql to provide the 
ability to
dump specific database(s) or the entire postgres cluster. A new section was 
added to the
configuration template. Configuration directives were chosen in a way to be as 
much as
possible compatible with the existing ones of the MySQL backup method.

I have tested it with my (very basic at the moment) postgres setup and it seems 
to work ok:

- -- both the entire cluster and individual databases dumps are generated 
   (note: I have used only the local unix sockets method to connect in my tests)

- -- no errors from pd_dump, pg_dumpall appear in logs

I have also verified that the package builds if the patches are appended to the 
existing
debian/patches/series in the order given here (ie all patches succeed).

I release the code with the same license as backup-manager does.

regards
George Zarkadas

- -- System Information:
Debian Release: 6.0.2
  APT prefers stable-updates
  APT policy: (500, 'stable-updates'), (500, 'proposed-updates'), (500, 
'stable'), (450, 'testing-proposed-updates'), (450, 'testing'), (400, 
'unstable')
Architecture: amd64 (x86_64)

Kernel: Linux 2.6.32-5-amd64 (SMP w/4 CPU cores)
Locale: LANG=el_GR.utf8, LC_CTYPE=el_GR.utf8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/dash

Versions of packages backup-manager depends on:
ii  debconf [debconf-2.0]        1.5.36.1    Debian configuration management sy
ii  findutils                    4.4.2-1+b1  utilities for finding files--find,
ii  ucf                          3.0025+nmu1 Update Configuration File: preserv

backup-manager recommends no packages.

Versions of packages backup-manager suggests:
ii  anacron               2.3-14             cron-like program that doesn't go 
ii  backup-manager-doc    0.7.9-3            documentation package for Backup M
ii  dar                   2.3.10-1+b1        Disk ARchive: Backup directory tre
ii  dvd+rw-tools          7.1-6              DVD+-RW/R tools
ii  genisoimage           9:1.1.11-1         Creates ISO-9660 CD-ROM filesystem
ii  gettext-base          0.18.1.1-3         GNU Internationalization utilities
ii  libfile-slurp-perl    9999.13-1          single call read & write file rout
pn  libnet-amazon-s3-perl <none>             (no description available)
ii  openssh-client        1:5.5p1-6+squeeze1 secure shell (SSH) client, for sec
ii  perl                  5.10.1-17squeeze2  Larry Wall's Practical Extraction 
ii  wodim                 9:1.1.11-1         command line CD/DVD writing tool
ii  zip                   3.0-3              Archiver for .zip files

- -- debconf information excluded


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)

iQEcBAEBAgAGBQJORxrpAAoJEJWXIVmJ5BwW6XAH/1Qaw4agOvpycstJFcxB0ysh
Rco1a5AoAaCboXv2+eumssg72XxSTfJEoVp+Ub6dgti6jtXYrVd9y8VSbrnloiuC
KNpj8s5UuZ0m0UXI4RUbNQkojp9PbUlM8iWM6f7w+53jXMo9g11Er89/xIshtkyd
3o+WJfpyReKCVXLJFVfQj3slpGLGwuTA22h5vHsHcpUGGrzfaKFu/81RhILPgwxa
0gRCtFCckRp4VCjDW0E5gOFb+tPaZb60UEi786sM5rp8rd872CJkWwEnp5mBw5fL
GKc5VQr97/lBC1hECBS5j93DRnWhQtthVghfy/6d7Iy/XNnRSTohjlUj92bMu6g=
=n41A
-----END PGP SIGNATURE-----
--- a/backup-manager.conf.tpl
+++ b/backup-manager.conf.tpl
@@ -72,6 +72,7 @@
 # - tarball
 # - tarball-incremental
 # - mysql
+# - postgresql
 # - svn
 # - pipe
 # - none
@@ -225,6 +226,73 @@
 export BM_MYSQL_EXTRA_OPTIONS=""
 
 ##############################################################
+# Backup method: PostgreSQL
+#############################################################
+
+# This method is dedicated to PostgreSQL databases.
+# You should not use the tarball method for backing up database
+# directories or you may have corrupted archives (unless, -and
+# this is specific for PostgreSQL- you also continuously backup
+# the Write Ahead Log; see the PostgreSQL documentation for 
+# details).
+# Enter here the list of databases to backup.
+# Wildcard: __ALL__ (will dump all the databases in one archive)
+export BM_POSTGRESQL_DATABASES="__ALL__"
+
+# The PostgreSQL user who is allowed to read every database of those
+# filled in BM_POSTGRESQL_DATABASES.
+export BM_POSTGRESQL_ADMINLOGIN=""
+
+# The above user's password.
+# WARNING! Be sure to protect this file with strict permissions.
+export BM_POSTGRESQL_ADMINPASS=""
+
+# The host where the database is.
+# Leave it empty for local access through unix sockets (the default).
+# Set to "localhost" for local access through the loopback interface.
+export BM_POSTGRESQL_HOST=""
+
+# The port where PostgreSQL listens to on the host.
+export BM_POSTGRESQL_PORT="5433"
+
+# Select the format of the output. It can be one of the following:
+# -- plain  : Plain-text SQL script file (the default).
+# -- custom : Custom-format archive suitable for input into pg_restore.
+# -- tar    : Tar-format archive suitable for input into pg_restore.
+# 
+# If you want to be able to transfer the database(s) to database platforms
+# other than PostgreSQL, use the plain format. Else it is perhaps better
+# to use the custom format, which offers the most flexibility as well as
+# builtin compression.
+# Note that the dump of __ALL__ target is always in plain format, since
+# pg_dumpall does not accept any option to set it.
+# See 'man pg_dump' and 'man pg_dumpall' for more information.
+export BM_POSTGRESQL_DUMPFORMAT="plain"
+
+# Which compression format to use? Relevant only if output format is 'plain'.
+# Available: all that are available for tarball method (gzip, bzip2, etc).
+export BM_POSTGRESQL_FILETYPE="bzip2"
+
+# Extra options to prepend to the database dump generation. 
+# Take care to what you do; this will be silently added to the 
+# command line.
+# Note that the basic options (hostname, port, etc) come after this on
+# the dump program's command line; thus they cannot be overriden.
+#
+# Example: add commands to drop objects before creating them (valid for
+# the "plain" output format).
+#
+#     export BM_PGDUMP_EXTRA_OPTIONS="--clean"
+
+# Extra options when dumping individual database(s).
+# Passed to pg_dump. See the man page for reference.
+export BM_PGDUMP_EXTRA_OPTIONS=""
+
+# Extra options when dumping the entire database cluster.
+# Passed to pg_dumpall. See the man page for reference.
+export BM_PGDUMPALL_EXTRA_OPTIONS=""
+
+##############################################################
 # Backup method: svn
 #############################################################
 
--- a/lib/actions.sh
+++ b/lib/actions.sh
@@ -31,6 +31,9 @@
         mysql)
             backup_method_mysql "$method"
         ;;
+        postgresql)
+            backup_method_postgresql "$method"
+        ;;
         tarball|tarball-incremental)
             backup_method_tarball "$method"
         ;;
--- a/lib/backup-methods.sh
+++ b/lib/backup-methods.sh
@@ -930,6 +930,74 @@
     fi
 }
 
+function backup_method_postgresql()
+{
+    method="$1"
+    pgpass_file="$HOME/.pgpass"
+
+    debug "backup_method_postgresql ($method)"
+
+    info "Using method \"\$method\"."
+    if [[ -x $pgdump ]] && [[ -x $pgdumpall ]]; then
+        :
+    else
+        error "The \"postgresql\" method is chosen, but \$pgdump and/or 
\$pgdumpall are not found."
+    fi
+
+    # if a PostgreSQL password file exists, the password must be inside
+    if [[ -f $pgpass_file ]]; then
+        info "Using existing PostgreSQL password file: \$pgpass_file"
+        BM_SHOULD_PURGE_PGPASS="false"
+    # we create a default one, just with the password
+    else
+        warning "Creating a default PostgreSQL password file: \$pgpass_file"
+        : > $pgpass_file
+        chmod 600 $pgpass_file
+        echo 
"$BM_POSTGRESQL_HOST:$BM_POSTGRESQL_PORT:*:$BM_POSTGRESQL_ADMINLOGIN:$BM_POSTGRESQL_ADMINPASS"
 >> $pgpass_file
+        BM_SHOULD_PURGE_PGPASS="true"
+    fi
+
+    # Account for empty host when unix sockets are used to connect
+    # to a local postgres server.
+
+    if [[ "X$BM_POSTGRESQL_HOST" = "X" ]]; then
+        BM_POSTGRESQL_HOSTFLAGS=""
+    else
+        BM_POSTGRESQL_HOSTFLAGS="-h$BM_POSTGRESQL_HOST"
+    fi
+
+    # We omit the binary from base_command, because backing up all databases
+    # requires a different binary than backing up specific database(s).
+    # It will be added later, during scanning of databases-to-backup phase.
+
+    base_command="--no-password -U$BM_POSTGRESQL_ADMINLOGIN 
$BM_POSTGRESQL_HOSTFLAGS -p$BM_POSTGRESQL_PORT"
+
+    for database in $BM_POSTGRESQL_DATABASES
+    do
+        if [[ "$database" = "__ALL__" ]]; then
+            
file_to_create="$BM_REPOSITORY_ROOT/${BM_ARCHIVE_PREFIX}-all-postgresql-databases.$TODAY.sql"
+            command="$pgdumpall $BM_PGDUMPALL_EXTRA_OPTIONS $base_command"
+            compress=$BM_POSTGRESQL_FILETYPE
+        else
+            
file_to_create="$BM_REPOSITORY_ROOT/${BM_ARCHIVE_PREFIX}-postgresql-${database}.$TODAY.sql"
+            if [[ "$BM_POSTGRESQL_DUMPFORMAT" = "plain" ]]; then
+                command="$pgdump $BM_PGDUMP_EXTRA_OPTIONS $base_command 
$database"
+                compress=$BM_POSTGRESQL_FILETYPE
+            else
+                command="$pgdump $BM_PGDUMP_EXTRA_OPTIONS 
-F$BM_POSTGRESQL_DUMPFORMAT $base_command $database"
+                compress="none"   
+            fi
+        fi
+        __create_file_with_meta_command
+    done 
+
+    # purge the pgpass file, if created by Backup Manager
+    if [[ "$BM_SHOULD_PURGE_PGPASS" == "true" ]]; then
+        info "Removing default PostgreSQL password file: \$pgpass_file"
+        rm -f $pgpass_file
+    fi
+}
+
 function backup_method_svn()
 {
     method="$1"
--- a/lib/externals.sh
+++ b/lib/externals.sh
@@ -14,6 +14,8 @@
 md5sum=$(which md5sum 2> /dev/null) || true
 bc=$(which bc 2> /dev/null) || true
 mysqldump=$(which mysqldump 2> /dev/null) || true
+pgdump=$(which pg_dump 2> /dev/null) || true
+pgdumpall=$(which pg_dumpall 2> /dev/null) || true
 svnadmin=$(which svnadmin 2> /dev/null) || true
 logger=$(which logger 2> /dev/null) || true
 nice_bin=$(which nice 2> /dev/null) || true
--- a/lib/sanitize.sh
+++ b/lib/sanitize.sh
@@ -231,6 +231,13 @@
     confkey_require "BM_MYSQL_HOST" "localhost"
     confkey_require "BM_MYSQL_PORT" "3306"
     confkey_require "BM_MYSQL_FILETYPE" "tar.gz"
+fi
+
+if [[ "$BM_ARCHIVE_METHOD" = "postgresql" ]]; then
+    confkey_require "BM_POSTGRESQL_ADMINLOGIN" "postgres"
+    confkey_require "BM_POSTGRESQL_PORT" "5433"
+    confkey_require "BM_POSTGRESQL_DUMPFORMAT" "plain"
+    confkey_require "BM_POSTGRESQL_FILETYPE" "gzip"
 fi
 
 # Burning system
--- a/man/backup-manager.8
+++ b/man/backup-manager.8
@@ -10,8 +10,9 @@
 for those who don't want an obfuscated tool for making tarballs. 
 .P
 There are different methods for building archives: tarballs, incremental 
-tarballs, mysql dumps, svn dump. You can also write your own method with a 
-generic method that will execute a command and will archive its output.
+tarballs, mysql dumps, postgresql dumps, svn dump. You can also write 
+your own method with a generic method that will execute a command and 
+will archive its output.
 .P
 Every times you run Backup Manager, you can upload archives thanks to a couple
 of upload methods: through FTP, SSH or even with RSYNC.
--- a/lib/backup-methods.sh
+++ b/lib/backup-methods.sh
@@ -917,7 +917,7 @@
             
file_to_create="$BM_REPOSITORY_ROOT/${BM_ARCHIVE_PREFIX}-all-mysql-databases.$TODAY.sql"
             command="$base_command --all-databases"
         else
-            
file_to_create="$BM_REPOSITORY_ROOT/${BM_ARCHIVE_PREFIX}-${database}.$TODAY.sql"
+            
file_to_create="$BM_REPOSITORY_ROOT/${BM_ARCHIVE_PREFIX}-mysql-${database}.$TODAY.sql"
             command="$base_command $database"
         fi
         __create_file_with_meta_command

Reply via email to