3 # PostgreSQL handler script for backupninja
6 getconf backupdir /var/backups/postgres
13 # If vservers are configured, decide if the handler should
14 # use them or if it should just operate on the host
15 if [ "$vservers" == "yes" ]; then
16 if [ ! -z $vsname ]; then
17 info "using vserver '$vsname'"
20 info "no vserver name specified, actions will be performed on the host"
24 # As needed, make sure that :
25 # * the specified vserver exists and is running
26 # * the specified vserver or host has the needed executables
27 if [ $usevserver ]; then
28 info "examining vserver '$vsname'"
30 vroot="$VROOTDIR/$vsname"
31 [ -d $vroot ] || fatal "vserver '$vsname' does not exist at '$vroot'"
33 running=`$VSERVERINFO $vsname RUNNING`
34 if [ $running = 1 ]; then
35 if [ "$databases" == "all" ]; then
36 [ -x "$vroot`$VSERVER $vsname exec which $PGSQLDUMPALL`" ] || \
37 fatal "Can't find $PGSQLDUMPALL in vserver $vsname."
39 [ -x "$vroot`$VSERVER $vsname exec which $PGSQLDUMP`" ] || \
40 fatal "Can't find $PGSQLDUMP in vserver $vsname."
43 fatal "vserver $vsname is not running."
46 if [ "$databases" == "all" ]; then
47 [ -x "`which $PGSQLDUMPALL`" ] || \
48 fatal "Can't find $PGSQLDUMPALL."
50 [ -x "`which $PGSQLDUMP`" ] || \
51 fatal "Can't find $PGSQLDUMP."
55 # create backup dir, the vroot variable will be empty if no vsname was specified
56 # and will proceed to operate on the host
57 [ -d $vroot$backupdir ] || (debug "mkdir -p $vroot$backupdir"; mkdir -p $vroot$backupdir)
58 [ -d $vroot$backupdir ] || fatal "Backup directory '$vroot$backupdir'"
60 # give backup dir the good uid and permissions
61 # (in respect to the vserver, if $usevserver)
62 pguid=`grep '^postgres:' $vroot/etc/passwd | awk -F: '{print $3}'`
64 fatal "No user called postgres`[ $usevserver != 1 ] || echo \" on vserver $vsname\"`."
65 debug "chown $pguid $vroot$backupdir"
66 chown $pguid $vroot$backupdir
67 debug "chmod 700 $vroot$backupdir"
68 chmod 700 $vroot$backupdir
70 # if $databases = all, use pg_dumpall
71 if [ "$databases" == "all" ]; then
72 if [ $usevserver ]; then
73 execstr="$VSERVER $vsname exec su - postgres -c $PGSQLDUMPALL > $backupdir/${vsname}.sql"
75 execstr="su - postgres -c $PGSQLDUMPALL > $backupdir/${localhost}-all.sql"
79 output=`$execstr 2>&1`
81 if [ "$code" == "0" ]; then
83 info "Successfully finished dump of pgsql cluster"
86 warning "Failed to dump pgsql cluster"
90 # else use pg_dump on each specified database
92 for db in $databases; do
95 execstr="$VSERVER $vsname exec su - postgres -c $PGSQLDUMP $db > $backupdir/${db}.sql"
97 execstr="su - postgres -c $PGSQLDUMP $db > $backupdir/${db}.sql"
101 output=`$execstr 2>&1`
103 if [ "$code" == "0" ]; then
105 info "Successfully finished dump of pgsql database ${db}"
108 warning "Failed to dump pgsql database ${db}"
114 if [ "$compress" == "yes" ]; then
115 output=`$GZIP -f $vroot$backupdir/*.sql 2>&1`