1 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
3 # PostgreSQL handler script for backupninja
6 getconf backupdir /var/backups/postgres
13 # Decide if the handler should operate on a vserver or on the host.
14 # In the former case, check that $vsname exists and is running.
17 if [ $vservers_are_available = yes ]; then
18 if [ -n "$vsname" ]; then
20 if ! vservers_exist "$vsname" ; then
21 fatal "The vserver given in vsname ($vsname) does not exist."
24 $VSERVERINFO -q $vsname RUNNING
26 fatal "The vserver $vsname is not running."
29 info "Using vserver '$vsname'."
31 vroot="$VROOTDIR/$vsname"
33 info "No vserver name specified, actions will be performed on the host."
37 # Make sure that the system to backup has the needed executables
38 if [ $usevserver = yes ]; then
39 debug "Examining vserver '$vsname'."
40 if [ "$databases" == "all" ]; then
41 [ -x "$vroot`$VSERVER $vsname exec which $PGSQLDUMPALL`" ] || \
42 fatal "Can't find $PGSQLDUMPALL in vserver $vsname."
44 [ -x "$vroot`$VSERVER $vsname exec which $PGSQLDUMP`" ] || \
45 fatal "Can't find $PGSQLDUMP in vserver $vsname."
48 if [ "$databases" == "all" ]; then
49 [ -x "`which $PGSQLDUMPALL`" ] || \
50 fatal "Can't find $PGSQLDUMPALL."
52 [ -x "`which $PGSQLDUMP`" ] || \
53 fatal "Can't find $PGSQLDUMP."
57 # create backup dir, the vroot variable will be empty if no vsname was specified
58 # and will proceed to operate on the host
59 [ -d $vroot$backupdir ] || (debug "mkdir -p $vroot$backupdir"; mkdir -p $vroot$backupdir)
60 [ -d $vroot$backupdir ] || fatal "Backup directory '$vroot$backupdir'"
62 # give backup dir the good uid and permissions
63 # (in respect to the vserver, if $usevserver = yes)
64 if [ $usevserver = yes ]; then
65 pguid=`$VSERVER $vsname exec getent passwd postgres | awk -F: '{print $3}'`
67 pguid=`getent passwd postgres | awk -F: '{print $3}'`
70 fatal "No user called postgres`[ $usevserver = no ] || echo \" on vserver $vsname\"`."
71 debug "chown $pguid $vroot$backupdir"
72 chown $pguid $vroot$backupdir
73 debug "chmod 700 $vroot$backupdir"
74 chmod 700 $vroot$backupdir
76 # if $databases = all, use pg_dumpall
77 if [ "$databases" == "all" ]; then
78 if [ $usevserver = yes ]; then
79 execstr="$VSERVER $vsname exec su - postgres -c \"$PGSQLDUMPALL > $backupdir/${vsname}.sql\""
81 execstr="su - postgres -c \"$PGSQLDUMPALL > $backupdir/${localhost}-all.sql\""
85 output=`eval $execstr 2>&1`
87 if [ "$code" == "0" ]; then
89 info "Successfully finished dump of pgsql cluster"
92 warning "Failed to dump pgsql cluster"
96 # else use pg_dump on each specified database
98 for db in $databases; do
99 if [ $usevserver = yes ]
101 execstr="$VSERVER $vsname exec su - postgres -c \"$PGSQLDUMP $db > $backupdir/${db}.sql\""
103 execstr="su - postgres -c \"$PGSQLDUMP $db > $backupdir/${db}.sql\""
107 output=`eval $execstr 2>&1`
109 if [ "$code" == "0" ]; then
111 info "Successfully finished dump of pgsql database ${db}"
114 warning "Failed to dump pgsql database ${db}"
120 if [ "$compress" == "yes" ]; then
121 output=`$GZIP -f $vroot$backupdir/*.sql 2>&1`