18046bbb97a87304bdc75c9f08bb5e9e3956500c
[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 SELF="`basename "$0"`[$$]"
45 DELTA_WAIT_SUCCESS=$(( MAX_WAIT_SUCCESS - MIN_WAIT_SUCCESS ))
46 MYHOSTNAME=$(hostname -f)
47
48 if [ -t 0 ]; then
49     verbose=1
50 else
51     verbose=0
52 fi
53
54 log() {
55     [ "$verbose" -gt 0 ] && echo "$*"
56     logger -p daemon.info -t "$SELF" "$*"
57 }
58
59
60 if [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ]; then
61     echo "Usage: $0 [<host>:<port>]"
62     exit 0
63 fi
64
65 if [ "$#" -gt 0 ]; then
66     forcehostport="$1"
67     shift
68 else
69     forcehostport=""
70 fi
71
72 mkdir -p "$STATEDIR"
73
74 # get a lock, but only if we did not force the run
75 if [ -z "$forcehostport" ]; then
76     exec 200< "$STATEDIR"
77     if ! flock -w 0 -e 200; then
78         log "Cannot acquire lock on $STATEDIR."
79         exit 0
80     fi
81 fi
82
83 while read host port username  cluster version; do
84     [ "${host#\#}" = "$host" ] || continue
85     [ -z "$host" ] && continue
86
87     flagfile="$STATEDIR/$host-$port.last-attempt"
88     flagfilesuccess="$STATEDIR/$host-$port.last-success"
89     if [ -n "$forcehostport" ]; then
90         if [ "$forcehostport" != "$host:$port" ]; then
91             log "Skipping $host:$port $version/$cluster because this run is limited to $host:$port."
92             runme=0
93         else
94             log "Forcing $host:$port $version/$cluster run."
95             runme=1
96         fi
97     else
98         if ! [ -e "$flagfile" ]; then
99             runme=1
100             log "Planning to run $host:$port $version/$cluster because no flag file exists."
101         else
102             now=$(date +%s)
103             mtime="$(stat --printf "%Y" "$flagfile")"
104             delta=$(( now - mtime ))
105             if [ "$delta" -lt "$MIN_WAIT" ]; then
106                 runme=0
107                 log "Skipping $host:$port $version/$cluster because last attempt was only ${delta}s ago."
108             else
109                 if ! [ -e "$flagfilesuccess" ]; then
110                     runme=1
111                     log "Planning to run $host:$port $version/$cluster because no success flag exists."
112                 else
113                     mtime="$(stat --printf "%Y" "$flagfilesuccess")"
114                     delta=$(( now - mtime ))
115                     if [ "$delta" -lt "$MIN_WAIT_SUCCESS" ]; then
116                         runme=0
117                         log "Skipping $host:$port $version/$cluster because last success was only ${delta}s ago."
118                     elif [ "$delta" -gt "$MAX_WAIT_SUCCESS" ]; then
119                         runme=1
120                         log "Planning to run $host:$port $version/$cluster because last success was ${delta}s ago."
121                     else
122                         # get a "randomish" but stable value for this backup run
123                         val=$(echo "$MYHOSTNAME-$host-$port-$mtime" | sha256sum | head -c 8)
124                         val=$((16#$val))
125                         valmod=$(($val % $DELTA_WAIT_SUCCESS))
126                         after_min=$((delta - MIN_WAIT_SUCCESS))
127                         if [ "$after_min" -gt "$valmod" ]; then
128                             runme=1
129                             log "Planning to run $host:$port $version/$cluster because random computer says so ($after_min > $valmod)."
130                         else
131                             runme=0
132                             log "Skipping $host:$port $version/$cluster because random computer says wait ($after_min <= $valmod)."
133                         fi
134                     fi
135                 fi
136             fi
137         fi
138     fi
139
140     if [ "$runme" -gt 0 ]; then
141         touch "$flagfile"
142         exec 201< "$flagfile"
143         if flock -w 0 -e 201; then
144             log "Running $host:$port $version/$cluster."
145             /usr/local/bin/postgres-make-one-base-backup "$host" "$port" "$username" "$cluster" "$version"
146             rc=$?
147             [ "$rc" = 0 ] && touch "$flagfilesuccess"
148             flock -u 201
149         else
150             log "Cannot acquire lock on $flagfile, skipping $host:$port $version/$cluster."
151         fi
152     fi
153 done << EOF
154 seger.debian.org        5432    debian-backup           dak             9.6
155 bmdb1.debian.org        5435    debian-backup           main            9.6
156 bmdb1.debian.org        5436    debian-backup           wannabuild      9.6
157 bmdb1.debian.org        5440    debian-backup           debsources      9.6
158 fasolo.debian.org       5433    debian-backup           dak             9.6
159 sibelius.debian.org     5433    debian-backup           snapshot        9.4
160 <%- if @hostname != "backuphost" -%>
161 moszumanska.debian.org  5432    debian-backup           main            9.1
162 <%- end -%>
163 #
164 # puppet notice:  this is just a partial file.  The tail EOF comes
165 # from a different concat fragment