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