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 if [ "$(id -u)" = 0 ]; then
45 echo >&2 "Do not run me as root. Probably you want sudo -u debbackup."
49 SELF="`basename "$0"`[$$]"
50 DELTA_WAIT_SUCCESS=$(( MAX_WAIT_SUCCESS - MIN_WAIT_SUCCESS ))
51 MYHOSTNAME=$(hostname -f)
60 [ "$verbose" -gt 0 ] && echo "$*"
61 logger -p daemon.info -t "$SELF" "$*"
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))
68 printf '%02d:%02d:%02d\n' $(($secs/3600)) $(($secs/60%60)) $(($secs%60))
73 if [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ]; then
74 echo "Usage: $0 [<host>:<port>]"
78 if [ "$#" -gt 0 ]; then
87 # get a lock, but only if we did not force the run
88 if [ -z "$forcehostport" ]; then
90 if ! flock -w 0 -e 200; then
91 log "Cannot acquire lock on $STATEDIR."
96 while read host port username cluster version; do
97 [ "${host#\#}" = "$host" ] || continue
98 [ -z "$host" ] && continue
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."
107 log "Forcing $host:$port $version/$cluster run."
111 if ! [ -e "$flagfile" ]; then
113 log "Planning to run $host:$port $version/$cluster because no flag file exists."
116 mtime="$(stat --printf "%Y" "$flagfile")"
117 delta=$(( now - mtime ))
118 if [ "$delta" -lt "$MIN_WAIT" ]; then
120 log "Skipping $host:$port $version/$cluster because last attempt was only $(format_timedelta "${delta}") (< $(format_timedelta "${MIN_WAIT}")) ago."
122 if ! [ -e "$flagfilesuccess" ]; then
124 log "Planning to run $host:$port $version/$cluster because no success flag exists."
126 mtime="$(stat --printf "%Y" "$flagfilesuccess")"
127 delta=$(( now - mtime ))
128 if [ "$delta" -lt "$MIN_WAIT_SUCCESS" ]; then
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
133 log "Planning to run $host:$port $version/$cluster because last success was $(format_timedelta "${delta}") (>= $(format_timedelta "${MAX_WAIT_SUCCESS}")) ago."
135 # get a "randomish" but stable value for this backup run
136 val=$(echo "$MYHOSTNAME-$host-$port-$mtime" | sha256sum | head -c 8)
138 rnd_cuttoff=$(($val % $DELTA_WAIT_SUCCESS))
139 age_after_min=$((delta - MIN_WAIT_SUCCESS))
140 if [ "$age_after_min" -lt "$rnd_cuttoff" ]; then
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}"))."
145 log "Planning to run $host:$port $version/$cluster because random computer says so ($(format_timedelta "${age_after_min}") >= $(format_timedelta "${rnd_cuttoff}"))."
153 if [ "$runme" -gt 0 ]; then
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"
160 log "Base backup for $host:$port $version/$cluster exited with rc $rc."
161 [ "$rc" = 0 ] && touch "$flagfilesuccess"
164 log "Cannot acquire lock on $flagfile, skipping $host:$port $version/$cluster."
167 done < '<%= scope['postgres::backup_server::globals::base_backup_clusters'] %> '