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 configured, decide if the handler should
14 # use them or if it should just operate on the host
16 if [ $vservers_are_available = yes ]; then
17 if ! vservers_exist "$vsname" ; then
18 fatal "At least one of the vservers listed in vsnames ($vsnames) does not exist."
20 info "using vserver '$vsname'"
25 # As needed, make sure that :
26 # * the specified vserver exists and is running
27 # * the specified vserver or host has the needed executables
28 if [ $usevserver = yes ]; then
29 info "examining vserver '$vsname'"
31 vroot="$VROOTDIR/$vsname"
32 [ -d $vroot ] || fatal "vserver '$vsname' does not exist at '$vroot'"
34 running=`$VSERVERINFO $vsname RUNNING`
35 if [ "$running" = "1" ]; then
36 if [ "$databases" == "all" ]; then
37 [ -x "$vroot`$VSERVER $vsname exec which $PGSQLDUMPALL`" ] || \
38 fatal "Can't find $PGSQLDUMPALL in vserver $vsname."
40 [ -x "$vroot`$VSERVER $vsname exec which $PGSQLDUMP`" ] || \
41 fatal "Can't find $PGSQLDUMP in vserver $vsname."
44 fatal "vserver $vsname is not running."
47 if [ "$databases" == "all" ]; then
48 [ -x "`which $PGSQLDUMPALL`" ] || \
49 fatal "Can't find $PGSQLDUMPALL."
51 [ -x "`which $PGSQLDUMP`" ] || \
52 fatal "Can't find $PGSQLDUMP."
56 # create backup dir, the vroot variable will be empty if no vsname was specified
57 # and will proceed to operate on the host
58 [ -d $vroot$backupdir ] || (debug "mkdir -p $vroot$backupdir"; mkdir -p $vroot$backupdir)
59 [ -d $vroot$backupdir ] || fatal "Backup directory '$vroot$backupdir'"
61 # give backup dir the good uid and permissions
62 # (in respect to the vserver, if $usevserver = yes)
63 pguid=`getent passwd postgres | awk -F: '{print $3}'`
65 fatal "No user called postgres`[ $usevserver = no ] || echo \" on vserver $vsname\"`."
66 debug "chown $pguid $vroot$backupdir"
67 chown $pguid $vroot$backupdir
68 debug "chmod 700 $vroot$backupdir"
69 chmod 700 $vroot$backupdir
71 # if $databases = all, use pg_dumpall
72 if [ "$databases" == "all" ]; then
73 if [ $usevserver = yes ]; then
74 execstr="$VSERVER $vsname exec su - postgres -c $PGSQLDUMPALL > $backupdir/${vsname}.sql"
76 execstr="su - postgres -c $PGSQLDUMPALL > $backupdir/${localhost}-all.sql"
80 output=`$execstr 2>&1`
82 if [ "$code" == "0" ]; then
84 info "Successfully finished dump of pgsql cluster"
87 warning "Failed to dump pgsql cluster"
91 # else use pg_dump on each specified database
93 for db in $databases; do
94 if [ $usevserver = yes ]
96 execstr="$VSERVER $vsname exec su - postgres -c $PGSQLDUMP $db > $backupdir/${db}.sql"
98 execstr="su - postgres -c $PGSQLDUMP $db > $backupdir/${db}.sql"
102 output=`$execstr 2>&1`
104 if [ "$code" == "0" ]; then
106 info "Successfully finished dump of pgsql database ${db}"
109 warning "Failed to dump pgsql database ${db}"
115 if [ "$compress" == "yes" ]; then
116 output=`$GZIP -f $vroot$backupdir/*.sql 2>&1`