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