1 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
3 # PostgreSQL handler script for backupninja
6 getconf backupdir /var/backups/postgres
13 # If vservers are enabled, check that $vsname exists, and decide if the
14 # handler should use it or if it should just operate on the host.
16 if [ $vservers_are_available = yes ]; then
17 if ! vservers_exist "$vsname" ; then
18 fatal "The vserver given in vsname ($vsname) does not exist."
20 info "using vserver '$vsname'"
25 # As needed, make sure that :
26 # * the specified vserver is running
27 # * the specified vserver or host has the needed executables
28 if [ $usevserver = yes ]; then
29 info "examining vserver '$vsname'"
30 vroot="$VROOTDIR/$vsname"
32 running=`$VSERVERINFO $vsname RUNNING`
33 if [ "$running" = "1" ]; then
34 if [ "$databases" == "all" ]; then
35 [ -x "$vroot`$VSERVER $vsname exec which $PGSQLDUMPALL`" ] || \
36 fatal "Can't find $PGSQLDUMPALL in vserver $vsname."
38 [ -x "$vroot`$VSERVER $vsname exec which $PGSQLDUMP`" ] || \
39 fatal "Can't find $PGSQLDUMP in vserver $vsname."
42 fatal "vserver $vsname is not running."
45 if [ "$databases" == "all" ]; then
46 [ -x "`which $PGSQLDUMPALL`" ] || \
47 fatal "Can't find $PGSQLDUMPALL."
49 [ -x "`which $PGSQLDUMP`" ] || \
50 fatal "Can't find $PGSQLDUMP."
54 # create backup dir, the vroot variable will be empty if no vsname was specified
55 # and will proceed to operate on the host
56 [ -d $vroot$backupdir ] || (debug "mkdir -p $vroot$backupdir"; mkdir -p $vroot$backupdir)
57 [ -d $vroot$backupdir ] || fatal "Backup directory '$vroot$backupdir'"
59 # give backup dir the good uid and permissions
60 # (in respect to the vserver, if $usevserver = yes)
61 pguid=`getent passwd postgres | awk -F: '{print $3}'`
63 fatal "No user called postgres`[ $usevserver = no ] || echo \" on vserver $vsname\"`."
64 debug "chown $pguid $vroot$backupdir"
65 chown $pguid $vroot$backupdir
66 debug "chmod 700 $vroot$backupdir"
67 chmod 700 $vroot$backupdir
69 # if $databases = all, use pg_dumpall
70 if [ "$databases" == "all" ]; then
71 if [ $usevserver = yes ]; then
72 execstr="$VSERVER $vsname exec su - postgres -c $PGSQLDUMPALL > $backupdir/${vsname}.sql"
74 execstr="su - postgres -c $PGSQLDUMPALL > $backupdir/${localhost}-all.sql"
78 output=`$execstr 2>&1`
80 if [ "$code" == "0" ]; then
82 info "Successfully finished dump of pgsql cluster"
85 warning "Failed to dump pgsql cluster"
89 # else use pg_dump on each specified database
91 for db in $databases; do
92 if [ $usevserver = yes ]
94 execstr="$VSERVER $vsname exec su - postgres -c $PGSQLDUMP $db > $backupdir/${db}.sql"
96 execstr="su - postgres -c $PGSQLDUMP $db > $backupdir/${db}.sql"
100 output=`$execstr 2>&1`
102 if [ "$code" == "0" ]; then
104 info "Successfully finished dump of pgsql database ${db}"
107 warning "Failed to dump pgsql database ${db}"
113 if [ "$compress" == "yes" ]; then
114 output=`$GZIP -f $vroot$backupdir/*.sql 2>&1`