2 # PostgreSQL handler script for backupninja
5 getconf backupdir /var/backups/postgres
12 # If vservers are configured, decide if the handler should
13 # use them or if it should just operate on the host
15 if [ "$vservers" == "yes" ]
19 info "Using vserver '$vsname'"
22 info "No vserver name specified, actions will be performed on the host"
26 # Check to make sure that the specified vserver exists
29 vroot="$VROOTDIR/$vsname"
30 [ -d $vroot ] || fatal "vserver '$vsname' does not exist at '$vroot'"
33 # create backup dir, the vroot variable will be empty if no vsname was specified
34 # and will proceed to operate on the host
35 [ -d $vroot$backupdir ] || mkdir -p $vroot$backupdir
36 [ -d $vroot$backupdir ] || fatal "Backup directory '$vroot$backupdir'"
38 # give backup dir the good uid and permissions
39 # (in respect to the vserver, if $usevserver)
40 pguid=`grep '^postgres:' $vroot/etc/passwd | awk -F: '{print $3}'`
41 debug "chown $pguid $vroot$backupdir"
42 chown $pguid $vroot$backupdir
43 debug "chmod 700 $vroot$backupdir"
44 chmod 700 $vroot$backupdir
46 # if $databases = all, use pg_dumpall
47 if [ "$databases" == "all" ]; then
48 if [ $usevserver ]; then
49 execstr="$VSERVER $vsname exec su - postgres -c $PGSQLDUMPALL > $vroot$backupdir/${vsname}.sql"
51 execstr="su - postgres -c $PGSQLDUMPALL > $backupdir/${localhost}-all.sql"
55 output=`$execstr 2>&1`
57 if [ "$code" == "0" ]; then
59 info "Successfully finished dump of pgsql cluster"
62 warning "Failed to dump pgsql cluster"
66 # else use pg_dump on each specified database
68 for db in $databases; do
71 execstr="$VSERVER $vsname exec su - postgres -c $PGSQLDUMP $db > $vroot$backupdir/${db}.sql"
73 execstr="su - postgres -c $PGSQLDUMP $db > $backupdir/${db}.sql"
77 output=`$execstr 2>&1`
79 if [ "$code" == "0" ]; then
81 info "Successfully finished dump of pgsql database ${db}"
84 warning "Failed to dump pgsql database ${db}"
90 if [ "$compress" == "yes" ]; then
91 output=`$GZIP -f $vroot$backupdir/*.sql 2>&1`