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
14 # Copyright 2014 Peter Palfrader
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:
24 # The above copyright notice and this permission notice shall be
25 # included in all copies or substantial portions of the Software.
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.
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
44 SELF="`basename "$0"`[$$]"
45 DELTA_WAIT_SUCCESS=$(( MAX_WAIT_SUCCESS - MIN_WAIT_SUCCESS ))
46 MYHOSTNAME=$(hostname -f)
55 [ "$verbose" -gt 0 ] && echo "$*"
56 logger -p daemon.info -t "$SELF" "$*"
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))
63 printf '%02d:%02d:%02d\n' $(($secs/3600)) $(($secs/60%60)) $(($secs%60))
68 if [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ]; then
69 echo "Usage: $0 [<host>:<port>]"
73 if [ "$#" -gt 0 ]; then
82 # get a lock, but only if we did not force the run
83 if [ -z "$forcehostport" ]; then
85 if ! flock -w 0 -e 200; then
86 log "Cannot acquire lock on $STATEDIR."
91 while read host port username cluster version; do
92 [ "${host#\#}" = "$host" ] || continue
93 [ -z "$host" ] && continue
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."
102 log "Forcing $host:$port $version/$cluster run."
106 if ! [ -e "$flagfile" ]; then
108 log "Planning to run $host:$port $version/$cluster because no flag file exists."
111 mtime="$(stat --printf "%Y" "$flagfile")"
112 delta=$(( now - mtime ))
113 if [ "$delta" -lt "$MIN_WAIT" ]; then
115 log "Skipping $host:$port $version/$cluster because last attempt was only $(format_timedelta "${delta}") (< $(format_timedelta "${MIN_WAIT}")) ago."
117 if ! [ -e "$flagfilesuccess" ]; then
119 log "Planning to run $host:$port $version/$cluster because no success flag exists."
121 mtime="$(stat --printf "%Y" "$flagfilesuccess")"
122 delta=$(( now - mtime ))
123 if [ "$delta" -lt "$MIN_WAIT_SUCCESS" ]; then
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
128 log "Planning to run $host:$port $version/$cluster because last success was $(format_timedelta "${delta}") (>= $(format_timedelta "${MAX_WAIT_SUCCESS}")) ago."
130 # get a "randomish" but stable value for this backup run
131 val=$(echo "$MYHOSTNAME-$host-$port-$mtime" | sha256sum | head -c 8)
133 rnd_cuttoff=$(($val % $DELTA_WAIT_SUCCESS))
134 age_after_min=$((delta - MIN_WAIT_SUCCESS))
135 if [ "$age_after_min" -lt "$rnd_cuttoff" ]; then
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}"))."
140 log "Planning to run $host:$port $version/$cluster because random computer says so ($(format_timedelta "${age_after_min}") >= $(format_timedelta "${rnd_cuttoff}"))."
148 if [ "$runme" -gt 0 ]; then
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"
155 [ "$rc" = 0 ] && touch "$flagfilesuccess"
158 log "Cannot acquire lock on $flagfile, skipping $host:$port $version/$cluster."
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 5473 debian-backup snapshot 9.6
170 # puppet notice: this is just a partial file. The tail EOF comes
171 # from a different concat fragment