f8baf1a927fa5517bec10f70072e2865e790abeb
[mirror/dsa-puppet.git] / modules / postgres / files / backup_server / postgres-make-one-base-backup
1 #!/bin/bash
2
3 # run a bunch of full postgresql backups
4 # vim:syn=sh:
5
6 # Copyright 2014,2018 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 usage() {
32         echo "Usage: $0 <host> <port> <username> <clustername> <version>"
33 }
34
35 CONFFILE=/etc/nagios/dsa-check-backuppg.conf
36 ROOTDIR=$(perl -MYAML -e "print YAML::LoadFile('$CONFFILE')->{'rootdir'}")
37 if [ -z "$ROOTDIR" ]; then
38         echo >&2 "Could not learn rootdir from $CONFFILE"
39         exit 1
40 fi
41
42 if [ -t 0 ]; then
43         verbose=1
44 else
45         verbose=0
46 fi
47
48 if [ "$verbose" -gt 0 ]; then
49         console="--progress --verbose"
50 else
51         console=""
52 fi
53
54 if [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ]; then
55         usage
56         exit 0
57 fi
58
59 export PGSSLMODE=verify-full
60 export PGSSLROOTCERT=/etc/ssl/debian/certs/ca.crt
61
62 date=$(date "+%Y%m%d-%H%M%S")
63 thishost=$(hostname -f)
64
65 if [ "$#" != 5 ]; then
66         usage >&2
67         exit 1
68 fi
69
70 host="$1"; shift
71 port="$1"; shift
72 username="$1"; shift
73 cluster="$1"; shift
74 version="$1"
75
76
77 label="$thishost-$date-$host-$cluster-$version-backup"
78 [ "$verbose" -gt 0 ] && echo "Doing $host:$port $version/$cluster: $label"
79
80 target="$cluster.BASE.$label.tar.gz"
81 tmp=$(tempfile -d "$ROOTDIR" -p "BASE-$host:$port-" -s ".tar.gz")
82 trap "rm -f '$tmp'" EXIT
83
84 /usr/bin/pg_basebackup \
85         --format=tar \
86         --pgdata=- \
87         -X none \
88         --label="$label" \
89         --host="$host" \
90         --port="$port" \
91         --username="$username" \
92         --no-password \
93         $console | pigz > "$tmp"
94 if ! [ "${PIPESTATUS[0]}" -eq 0 ]; then
95         echo >&2 "pg_basebackup failed with exit code ${PIPESTATUS[0]}"
96         exit 1
97 fi
98 mv "$tmp" "$ROOTDIR/${host%%.*}/$target"