ff5f7ba1352527be8481732344b78f23df6d2789
[matthijs/upstream/backupninja.git] / handlers / pgsql.in
1 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
2 # vim: set filetype=sh sw=3 sts=3 expandtab autoindent:
3 #
4 # PostgreSQL handler script for backupninja
5 #
6
7 getconf backupdir /var/backups/postgres
8 getconf databases all
9 getconf compress yes
10 getconf vsname
11
12 localhost=`hostname`
13
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.
16 local usevserver=no
17 local vroot
18 if [ $vservers_are_available = yes ]; then
19    if [ -n "$vsname" ]; then
20       # does it exist ?
21       if ! vservers_exist "$vsname" ; then
22          fatal "The vserver given in vsname ($vsname) does not exist."
23       fi
24       # is it running ?
25       vservers_running $vsname || fatal "The vserver $vsname is not running."
26       # everything ok
27       info "Using vserver '$vsname'."
28       usevserver=yes
29       vroot="$VROOTDIR/$vsname"
30    else
31       info "No vserver name specified, actions will be performed on the host."
32    fi
33 fi
34
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."
41    else
42       [ -x "$vroot`$VSERVER $vsname exec which $PGSQLDUMP`" ] || \
43          fatal "Can't find $PGSQLDUMP in vserver $vsname."
44    fi
45 else
46    if [ "$databases" == "all" ]; then
47       [ -x "`which $PGSQLDUMPALL`" ] || \
48          fatal "Can't find $PGSQLDUMPALL."
49    else
50       [ -x "`which $PGSQLDUMP`" ] || \
51          fatal "Can't find $PGSQLDUMP."
52    fi
53 fi
54
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."
59
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}'`
64 else
65    pguid=`getent passwd $PGSQLUSER | @AWK@ -F: '{print $3}'`
66 fi
67 [ -n "$pguid" ] || \
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
73
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 > $backupdir/${vsname}.sql.gz\""
79       else
80          execstr="$VSERVER $vsname exec su - $PGSQLUSER -c \"$PGSQLDUMPALL > $backupdir/${vsname}.sql\""
81       fi
82    else
83       if [ "$compress" == "yes" ]; then
84          execstr="su - $PGSQLUSER -c \"$PGSQLDUMPALL | $GZIP > $backupdir/${localhost}-all.sql.gz\""
85       else
86          execstr="su - $PGSQLUSER -c \"$PGSQLDUMPALL > $backupdir/${localhost}-all.sql\""
87       fi
88    fi
89    debug "$execstr"
90    if [ ! $test ]; then
91       output=`eval $execstr 2>&1`
92       code=$?
93       if [ "$code" == "0" ]; then
94          debug $output
95          info "Successfully finished dump of pgsql cluster"
96       else
97          warning $output
98          warning "Failed to dump pgsql cluster"
99       fi
100    fi
101
102 # else use pg_dump on each specified database
103 else
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 > $backupdir/${db}.sql.gz\""
108          else
109             execstr="$VSERVER $vsname exec su - $PGSQLUSER -c \"$PGSQLDUMP $db | > $backupdir/${db}.sql\""
110          fi
111       else
112          if [ "$compress" == "yes" ]; then
113             execstr="su - $PGSQLUSER -c \"$PGSQLDUMP $db | $GZIP > $backupdir/${db}.sql.gz\""
114          else
115             execstr="su - $PGSQLUSER -c \"$PGSQLDUMP $db > $backupdir/${db}.sql\""
116          fi
117       fi
118       debug "$execstr"
119       if [ ! $test ]; then
120          output=`eval $execstr 2>&1`
121          code=$?
122          if [ "$code" == "0" ]; then
123             debug $output
124             info "Successfully finished dump of pgsql database ${db}"
125          else
126             warning $output
127             warning "Failed to dump pgsql database ${db}"
128          fi
129       fi
130    done
131 fi
132
133 return 0
134