1 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
2 # vim: set filetype=sh sw=3 sts=3 expandtab autoindent:
4 # PostgreSQL handler script for backupninja
7 getconf backupdir /var/backups/postgres
14 # Decide if the handler should operate on a vserver or on the host.
15 # In the former case, check that $vsname exists and is running.
18 if [ $vservers_are_available = yes ]; then
19 if [ -n "$vsname" ]; then
21 if ! vservers_exist "$vsname" ; then
22 fatal "The vserver given in vsname ($vsname) does not exist."
25 vservers_running $vsname || fatal "The vserver $vsname is not running."
27 info "Using vserver '$vsname'."
29 vroot="$VROOTDIR/$vsname"
31 info "No vserver name specified, actions will be performed on the host."
35 # Make sure that the system to backup has the needed executables
36 if [ $usevserver = yes ]; then
37 debug "Examining vserver '$vsname'."
38 if [ "$databases" == "all" ]; then
39 [ -x "$vroot`$VSERVER $vsname exec which $PGSQLDUMPALL`" ] || \
40 fatal "Can't find $PGSQLDUMPALL in vserver $vsname."
42 [ -x "$vroot`$VSERVER $vsname exec which $PGSQLDUMP`" ] || \
43 fatal "Can't find $PGSQLDUMP in vserver $vsname."
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' does not exist, and could not be created."
60 # give backup dir the good uid and permissions
61 # (in respect to the vserver, if $usevserver = yes)
62 if [ $usevserver = yes ]; then
63 pguid=`$VSERVER $vsname exec getent passwd $PGSQLUSER | @AWK@ -F: '{print $3}'`
65 pguid=`getent passwd $PGSQLUSER | @AWK@ -F: '{print $3}'`
68 fatal "No user called $PGSQLUSER`[ $usevserver = no ] || echo \" on vserver $vsname\"`."
69 debug "chown $pguid $vroot$backupdir"
70 chown $pguid $vroot$backupdir
71 debug "chmod 700 $vroot$backupdir"
72 chmod 700 $vroot$backupdir
74 # if $databases = all, use pg_dumpall
75 if [ "$databases" == "all" ]; then
76 if [ $usevserver = yes ]; then
77 if [ "$compress" == "yes" ]; then
78 execstr="$VSERVER $vsname exec su - $PGSQLUSER -c \"$PGSQLDUMPALL | $GZIP --rsyncable > $backupdir/${vsname}.sql.gz\""
80 execstr="$VSERVER $vsname exec su - $PGSQLUSER -c \"$PGSQLDUMPALL > $backupdir/${vsname}.sql\""
83 if [ "$compress" == "yes" ]; then
84 execstr="su - $PGSQLUSER -c \"$PGSQLDUMPALL | $GZIP --rsyncable > $backupdir/${localhost}-all.sql.gz\""
86 execstr="su - $PGSQLUSER -c \"$PGSQLDUMPALL > $backupdir/${localhost}-all.sql\""
91 output=`eval $execstr 2>&1`
93 if [ "$code" == "0" ]; then
95 info "Successfully finished dump of pgsql cluster"
98 warning "Failed to dump pgsql cluster"
102 # else use pg_dump on each specified database
104 for db in $databases; do
105 if [ $usevserver = yes ]; then
106 if [ "$compress" == "yes" ]; then
107 execstr="$VSERVER $vsname exec su - $PGSQLUSER -c \"$PGSQLDUMP $db | $GZIP --rsyncable > $backupdir/${db}.sql.gz\""
109 execstr="$VSERVER $vsname exec su - $PGSQLUSER -c \"$PGSQLDUMP $db | > $backupdir/${db}.sql\""
112 if [ "$compress" == "yes" ]; then
113 execstr="su - $PGSQLUSER -c \"$PGSQLDUMP $db | $GZIP --rsyncable > $backupdir/${db}.sql.gz\""
115 execstr="su - $PGSQLUSER -c \"$PGSQLDUMP $db > $backupdir/${db}.sql\""
120 output=`eval $execstr 2>&1`
122 if [ "$code" == "0" ]; then
124 info "Successfully finished dump of pgsql database ${db}"
127 warning "Failed to dump pgsql database ${db}"