switch seger to modern pg backup config fu
[mirror/dsa-puppet.git] / modules / postgres / templates / backup_server / postgres-make-base-backups.erb
1 #!/bin/bash
2
3 # vim:syn=sh:
4 # vim:ts=4:
5 # vim:et:
6
7
8 # run a bunch of full postgresql backups
9 #  if given a host:port, run this backup,
10 #  else run all defined once if they have not run recently
11 # vim:syn=sh:
12
13
14 # Copyright 2014 Peter Palfrader
15 #
16 # Permission is hereby granted, free of charge, to any person obtaining
17 # a copy of this software and associated documentation files (the
18 # "Software"), to deal in the Software without restriction, including
19 # without limitation the rights to use, copy, modify, merge, publish,
20 # distribute, sublicense, and/or sell copies of the Software, and to
21 # permit persons to whom the Software is furnished to do so, subject to
22 # the following conditions:
23 #
24 # The above copyright notice and this permission notice shall be
25 # included in all copies or substantial portions of the Software.
26 #
27 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34
35
36 MIN_WAIT=$(( 60*60*4 ))
37 MIN_WAIT_SUCCESS=$(( 60*60*24*7 ))
38 MAX_WAIT_SUCCESS=$(( 60*60*24*10 ))
39 STATEDIR=/var/lib/dsa/postgres-make-base-backups
40
41 ####
42 set -u
43
44 if [ "$(id -u)" = 0 ]; then
45     echo >&2 "Do not run me as root.  Probably you want sudo -u debbackup."
46     exit 1
47 fi
48
49 SELF="`basename "$0"`[$$]"
50 DELTA_WAIT_SUCCESS=$(( MAX_WAIT_SUCCESS - MIN_WAIT_SUCCESS ))
51 MYHOSTNAME=$(hostname -f)
52
53 if [ -t 0 ]; then
54     verbose=1
55 else
56     verbose=0
57 fi
58
59 log() {
60     [ "$verbose" -gt 0 ] && echo "$*"
61     logger -p daemon.info -t "$SELF" "$*"
62 }
63 format_timedelta() {
64     local secs="$1"; shift
65     if [ "$secs" -ge 86400 ]; then
66         printf '%d+%02d:%02d:%02d\n' $(($secs/3600/24)) $(($secs/3600%24)) $(($secs/60%60)) $(($secs%60))
67     else
68         printf '%02d:%02d:%02d\n' $(($secs/3600)) $(($secs/60%60)) $(($secs%60))
69     fi
70 }
71
72
73 if [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ]; then
74     echo "Usage: $0 [<host>:<port>]"
75     exit 0
76 fi
77
78 if [ "$#" -gt 0 ]; then
79     forcehostport="$1"
80     shift
81 else
82     forcehostport=""
83 fi
84
85 mkdir -p "$STATEDIR"
86
87 # get a lock, but only if we did not force the run
88 if [ -z "$forcehostport" ]; then
89     exec 200< "$STATEDIR"
90     if ! flock -w 0 -e 200; then
91         log "Cannot acquire lock on $STATEDIR."
92         exit 0
93     fi
94 fi
95
96 while read host port username  cluster version; do
97     [ "${host#\#}" = "$host" ] || continue
98     [ -z "$host" ] && continue
99
100     flagfile="$STATEDIR/$host-$port.last-attempt"
101     flagfilesuccess="$STATEDIR/$host-$port.last-success"
102     if [ -n "$forcehostport" ]; then
103         if [ "$forcehostport" != "$host:$port" ]; then
104             log "Skipping $host:$port $version/$cluster because this run is limited to $forcehostport."
105             runme=0
106         else
107             log "Forcing $host:$port $version/$cluster run."
108             runme=1
109         fi
110     else
111         if ! [ -e "$flagfile" ]; then
112             runme=1
113             log "Planning to run $host:$port $version/$cluster because no flag file exists."
114         else
115             now=$(date +%s)
116             mtime="$(stat --printf "%Y" "$flagfile")"
117             delta=$(( now - mtime ))
118             if [ "$delta" -lt "$MIN_WAIT" ]; then
119                 runme=0
120                 log "Skipping $host:$port $version/$cluster because last attempt was only $(format_timedelta "${delta}") (< $(format_timedelta "${MIN_WAIT}")) ago."
121             else
122                 if ! [ -e "$flagfilesuccess" ]; then
123                     runme=1
124                     log "Planning to run $host:$port $version/$cluster because no success flag exists."
125                 else
126                     mtime="$(stat --printf "%Y" "$flagfilesuccess")"
127                     delta=$(( now - mtime ))
128                     if [ "$delta" -lt "$MIN_WAIT_SUCCESS" ]; then
129                         runme=0
130                         log "Skipping $host:$port $version/$cluster because last success was only $(format_timedelta "${delta}") (< $(format_timedelta "${MIN_WAIT_SUCCESS}")) ago."
131                     elif [ "$delta" -gt "$MAX_WAIT_SUCCESS" ]; then
132                         runme=1
133                         log "Planning to run $host:$port $version/$cluster because last success was $(format_timedelta "${delta}") (>= $(format_timedelta "${MAX_WAIT_SUCCESS}")) ago."
134                     else
135                         # get a "randomish" but stable value for this backup run
136                         val=$(echo "$MYHOSTNAME-$host-$port-$mtime" | sha256sum | head -c 8)
137                         val=$((16#$val))
138                         rnd_cuttoff=$(($val % $DELTA_WAIT_SUCCESS))
139                         age_after_min=$((delta - MIN_WAIT_SUCCESS))
140                         if [ "$age_after_min" -lt "$rnd_cuttoff" ]; then
141                             runme=0
142                             log "Skipping $host:$port $version/$cluster because random computer says wait ([$(format_timedelta "${age_after_min}") < $(format_timedelta "${rnd_cuttoff}") (< $(format_timedelta "${DELTA_WAIT_SUCCESS}"))] + $(format_timedelta "${MIN_WAIT_SUCCESS}"))."
143                         else
144                             runme=1
145                             log "Planning to run $host:$port $version/$cluster because random computer says so ($(format_timedelta "${age_after_min}") >= $(format_timedelta "${rnd_cuttoff}"))."
146                         fi
147                     fi
148                 fi
149             fi
150         fi
151     fi
152
153     if [ "$runme" -gt 0 ]; then
154         touch "$flagfile"
155         exec 201< "$flagfile"
156         if flock -w 0 -e 201; then
157             log "Running $host:$port $version/$cluster."
158             /usr/local/bin/postgres-make-one-base-backup "$host" "$port" "$username" "$cluster" "$version"
159             rc=$?
160             log "Base backup for $host:$port $version/$cluster exited with rc $rc."
161             [ "$rc" = 0 ] && touch "$flagfilesuccess"
162             flock -u 201
163         else
164             log "Cannot acquire lock on $flagfile, skipping $host:$port $version/$cluster."
165         fi
166     fi
167 done << EOF
168 bmdb1.debian.org        5435    debian-backup           main            9.6
169 bmdb1.debian.org        5440    debian-backup           debsources      9.6
170 sallinen.debian.org     5473    debian-backup           snapshot        9.6
171 #
172 # puppet notice:  this is just a partial file.  The tail EOF comes
173 # from a different concat fragment