]> git.donarmstrong.com Git - dsa-puppet.git/blob - modules/postgres/files/backup_server/postgres-make-base-backups
691b13ab847b1f619333b02cd86cb70e2ce6c69a
[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 fi
43
44 if [ "$verbose" -gt 0 ]; then
45         console="--progress --verbose"
46 else
47         console=""
48 fi
49
50 if [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ]; then
51         echo "Usage: $0 [<host>:<port>]"
52         exit 0
53 fi
54
55 if [ "$#" -gt 0 ]; then
56         forcehostport="$1"
57         shift
58 else
59         forcehostport=""
60 fi
61
62 export PGSSLMODE=verify-full
63 export PGSSLROOTCERT=/etc/ssl/debian/certs/ca.crt
64
65 date=$(date "+%Y%m%d-%H%M%S")
66 thishost=$(hostname -f)
67
68 while read host port username  cluster version; do
69         [ "${host#\#}" = "$host" ] || continue
70
71         if [ -n "$forcehostport" ] && [ "$forcehostport" != "$host:$port" ]; then
72                 [ "$verbose" -gt 0 ] && echo "Skipping $host:$port $version/$cluster."
73                 continue
74         fi
75
76         label="$thishost-$date-$host-$cluster-$version-backup"
77         [ "$verbose" -gt 0 ] && echo "Doing $host:$port $version/$cluster: $label"
78
79         target="$cluster.BASE.$label.tar.gz"
80         tmp=$(tempfile -d "$ROOTDIR" -p "BASE-$host:$port-" -s ".tar.gz")
81         trap "rm -f '$tmp'" EXIT
82
83         /usr/lib/postgresql/"$version"/bin/pg_basebackup --format=tar --pgdata=- --label="$label" --host="$host" --port="$port" --username="$username" --no-password $console | pigz > "$tmp"
84         if ! [ "${PIPESTATUS[0]}" -eq 0 ]; then
85                 echo >&2 "pg_basebackup failed with exit code ${PIPESTATUS[0]}"
86                 exit 1
87         fi
88         mv "$tmp" "$ROOTDIR/${host%%.*}/$target"
89 done << EOF
90 chopin.debian.org       5432    debian-backup           dak             9.4
91 bmdb1.debian.org        5435    debian-backup           main            9.4
92 bmdb1.debian.org        5436    debian-backup           wannabuild      9.4
93 danzi.debian.org        5433    debian-backup           main            9.4
94 franck.debian.org       5433    debian-backup           dak             9.4
95 sibelius.debian.org     5433    debian-backup           snapshot        9.4
96 moszumanska.debian.org  5432    debian-backup           main            9.1
97 EOF