3 # initiate a mirror staged mirror update from sync-source.
5 # if we have a serial file and we got a serial on the command line, only sync if the serial is different
7 # Copyright (c) 2012 Peter Palfrader
9 # Permission is hereby granted, free of charge, to any person obtaining
10 # a copy of this software and associated documentation files (the
11 # "Software"), to deal in the Software without restriction, including
12 # without limitation the rights to use, copy, modify, merge, publish,
13 # distribute, sublicense, and/or sell copies of the Software, and to
14 # permit persons to whom the Software is furnished to do so, subject to
15 # the following conditions:
17 # The above copyright notice and this permission notice shall be
18 # included in all copies or substantial portions of the Software.
20 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 NAME="$(basename "$0")"
35 echo "Usage: $0 [--one-stage] <basedir> <sync-source> [<serial>]"
38 if [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ]; then usage; exit 0; fi
61 SYNC_SOURCE=${1:-}; shift
62 SYNC_SERIAL=${1:-}; shift || true
63 if [ -z "$BASEDIR" ]; then usage >&2; exit 1; fi
64 if [ -z "$SYNC_SOURCE" ]; then usage >&2; exit 1; fi
67 RSYNC_BASE_OPTIONS="-avz --delete"
68 RSYNC_SSH_OPTIONS="ssh -o AddressFamily=inet -o BatchMode=yes"
71 LOGFILE="$LOGDIR/$NAME-run.log"
78 CNF_FILE="$HOME/etc/$NAME.conf"
79 ! [ -e "$CNF_FILE" ] || . "$CNF_FILE"
81 SOURCE="${SYNC_SOURCE}/"
84 ###############################################
86 # point stdout and stderr to the logfile if it's not a tty.
87 # save stdout to fd5 for communications with the master
92 # move current stdout to fd5 and reopen to logfile
96 # duplicate stdout to fd5
105 echo "[$(date)][$NAME][$$] $1"
110 if ! flock -e 200; then
111 log "Cannot acquire lock."
112 echo >&5 "[MSM] LOCK-ERROR"
118 ###############################################
122 #log "called with $* and ${SSH_ORIGINAL_COMMAND:-no ssh original command options}."
126 if [ -e "${BASEDIR}${ACTIVE}" ] && [ "$(readlink "${BASEDIR}${ACTIVE}")" = "$ALPHA" ] ; then
133 log "active is $active; staging is $staging"
135 rsync_source="${SOURCE}"
136 rsync_curactive="${BASEDIR}${active}/"
137 rsync_target="${BASEDIR}${staging}/"
139 if [ -e "$rsync_curactive/.serial" ] && [ -n "$SYNC_SERIAL" ] && [ "$(cat $rsync_curactive/.serial)" = "$SYNC_SERIAL" ]; then
140 log "active is already at serial $SYNC_SERIAL. No action required."
141 echo >&5 "[MSM] ALREADY-CURRENT"
145 echo >&5 "[MSM] STAGE1-START"
146 log "Running $RSYNC $RSYNC_BASE_OPTIONS -e $RSYNC_SSH_OPTIONS --link-dest $rsync_curactive $rsync_source $rsync_target"
147 $RSYNC $RSYNC_BASE_OPTIONS -e "$RSYNC_SSH_OPTIONS" --link-dest "$rsync_curactive" "$rsync_source" "$rsync_target"
149 echo >&5 "[MSM] STAGE1-DONE"
150 if [ "$one_stage" -gt 0 ]; then
158 ln --symbolic --force --no-target-directory "$staging" "${BASEDIR}$ACTIVE"
159 rm -rf "$rsync_curactive"
160 echo >&5 "[MSM] STAGE2-DONE"
164 echo >&5 "[MSM] STAGE2-ABORT"
168 echo >&5 "[MSM] STAGE2-UNKNOWN-ACTION $action"
169 log "stage2 unknown action $action"