]> git.donarmstrong.com Git - dsa-puppet.git/blob - modules/postgres/files/backup_server/postgres-make-base-backups
Check pipe status
[dsa-puppet.git] / modules / postgres / files / backup_server / postgres-make-base-backups
1 #!/bin/bash
2
3 # run a bunch of full postgresql backups
4
5
6 # Copyright 2014 Peter Palfrader
7 #
8 # Permission is hereby granted, free of charge, to any person obtaining
9 # a copy of this software and associated documentation files (the
10 # "Software"), to deal in the Software without restriction, including
11 # without limitation the rights to use, copy, modify, merge, publish,
12 # distribute, sublicense, and/or sell copies of the Software, and to
13 # permit persons to whom the Software is furnished to do so, subject to
14 # the following conditions:
15 #
16 # The above copyright notice and this permission notice shall be
17 # included in all copies or substantial portions of the Software.
18 #
19 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
27
28 set -e
29 set -u
30
31 CONFFILE=/etc/nagios/dsa-check-backuppg.conf
32 ROOTDIR=$(perl -MYAML -e "print YAML::LoadFile('$CONFFILE')->{'rootdir'}")
33 if [ -z "$ROOTDIR" ]; then
34         echo >&2 "Could not learn rootdir from $CONFFILE"
35         exit 1
36 fi
37
38 if [ -t 0 ]; then
39         verbose=1
40 else
41         verbose=0
42
43 if [ "$verbose" -gt 0 ]; then
44         console="--progress --verbose"
45 else
46         console=""
47 fi
48
49 export PGSSLMODE=verify-full
50 export PGSSLROOTCERT=/etc/ssl/debian/certs/ca.crt
51
52 date=$(date "+%Y%m%d-%H%M%S")
53
54 while read host port username  cluster version; do
55         [ "${host#\#}" = "$host" ] || continue
56
57         label="$date-$host-$cluster-$version-backup"
58         [ "$verbose" -gt 0 ] && echo "Doing $host:$port $version/$cluster: $label"
59
60         target="$cluster.BASE.$label.tar.gz"
61         tmp=$(tempfile -d "$ROOTDIR" -p "BASE-$host:$port-" -s ".tar.gz")
62         trap "rm -f '$tmp'" EXIT
63
64         /usr/lib/postgresql/"$version"/bin/pg_basebackup --format=tar --pgdata=- --label="$label" --host="$host" --port="$port" --username="$username" --no-password $console | pigz > "$tmp"
65         if ! [ "${PIPESTATUS[0]}" -eq 0 ]; then
66                 echo >&2 "pg_basebackup failed with exit code ${PIPESTATUS[0]}"
67                 exit 1
68         fi
69         mv "$tmp" "$ROOTDIR/${host%%.*}/$target"
70 done << EOF
71 chopin.debian.org       5432    debian-backup           dak             9.1
72 bmdb1.debian.org        5435    debian-backup           main            9.1
73 bmdb1.debian.org        5436    debian-backup           wanna-build     9.1
74 danzi.debian.org        5433    debian-backup           main            9.1
75 franck.debian.org       5433    debian-backup           dak             9.1
76 sibelius.debian.org     5433    debian-backup           snapshot        9.1
77 EOF