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
14 if [ "$vservers" == "yes" ]; then
15 if [ ! -z $vsname ]; then
16 info "using vserver '$vsname'"
19 info "no vserver name specified, actions will be performed on the host"
23 # As needed, make sure that :
24 # * the specified vserver exists and is running
25 # * the specified vserver or host has the needed executables
26 if [ $usevserver ]; then
27 info "examining vserver '$vsname'"
29 vroot="$VROOTDIR/$vsname"
30 [ -d $vroot ] || fatal "vserver '$vsname' does not exist at '$vroot'"
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)
61 pguid=`getent passwd postgres | awk -F: '{print $3}'`
63 fatal "No user called postgres`[ $usevserver != 1 ] || 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 ]; 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
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`