added argmuent parsing for rsync options, added default global options and profile-specific options, updated Master.conf
This commit is contained in:
+26
-15
@@ -19,27 +19,23 @@ set -e
|
||||
#-----------------------------------------------------------------------------------------------
|
||||
#----------------- User Variables, Please adjust here in Master.conf as needed -----------------
|
||||
#-----------------------------------------------------------------------------------------------
|
||||
#
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
# Load config & helpers
|
||||
# Load configs & helpers
|
||||
source "$SCRIPT_DIR/../Master.conf"
|
||||
source "$SCRIPT_DIR/../common.sh"
|
||||
|
||||
# Input directory
|
||||
# ----------------------------- Input -----------------------------
|
||||
DIRECTORY="$1"
|
||||
if [[ -z "$DIRECTORY" ]]; then
|
||||
echo "Usage: $0 <directory-to-sync> [--dry-run] [VAR=value ...]"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
shift 1
|
||||
shift
|
||||
parse_args "$@"
|
||||
|
||||
# Profile detection
|
||||
# ----------------------------- Profile -----------------------------
|
||||
PROFILE_NAME=$(basename "$DIRECTORY" | tr '[:upper:]' '[:lower:]')
|
||||
echo "Detected profile: $PROFILE_NAME"
|
||||
|
||||
MAX_RSYNC_PROCS=${PROFILE_MAX_PROCS[$PROFILE_NAME]:-$MAX_RSYNC_PROCS}
|
||||
BW_LIMIT=${PROFILE_BW_LIMIT[$PROFILE_NAME]:-$BW_LIMIT}
|
||||
@@ -50,23 +46,38 @@ RETRY_COUNT=${PROFILE_RETRY_COUNT[$PROFILE_NAME]:-$RETRY_COUNT}
|
||||
SLEEP=${PROFILE_SLEEP[$PROFILE_NAME]:-$SLEEP}
|
||||
EXCLUDE_DIRS=(${PROFILE_EXCLUDE_DIRS[$PROFILE_NAME]:-${EXCLUDE_DIRS[@]}})
|
||||
|
||||
echo "Settings: MAX_RSYNC_PROCS=$MAX_RSYNC_PROCS, BW_LIMIT=$BW_LIMIT, CONTAINERS=${CRITICAL_CONTAINER_NAMES[*]}, EXCLUDES=${EXCLUDE_DIRS[*]}"
|
||||
[[ "$ENABLE_LOGGING" == true ]] && echo "Detected profile: $PROFILE_NAME"
|
||||
[[ "$ENABLE_LOGGING" == true ]] && echo "Settings: MAX_RSYNC_PROCS=$MAX_RSYNC_PROCS, BW_LIMIT=$BW_LIMIT, CONTAINERS=${CRITICAL_CONTAINER_NAMES[*]}, EXCLUDES=${EXCLUDE_DIRS[*]}"
|
||||
|
||||
# ----------------------------- Main Sync -----------------------------
|
||||
wait_for_rsync() {
|
||||
for attempt in $(seq 1 "$RETRY_COUNT"); do
|
||||
RSYNC_COUNT=$(pgrep -fc "rsync.*root@${REMOTE_SERVER}" || true)
|
||||
if [ "$RSYNC_COUNT" -ge "$MAX_RSYNC_PROCS" ]; then
|
||||
[[ "$ENABLE_LOGGING" == true ]] && echo "[$LOCAL_SERVER_NAME] Waiting... ($RSYNC_COUNT active rsync processes)"
|
||||
sleep "$SLEEP"
|
||||
else
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
echo "Too many rsync processes, exiting."
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Main sync logic
|
||||
main() {
|
||||
echo "[$LOCAL_SERVER_NAME] Syncing $DIRECTORY → $REMOTE_SERVER:$DIRECTORY"
|
||||
[[ "$ENABLE_LOGGING" == true ]] && echo "[$LOCAL_SERVER_NAME] Syncing $DIRECTORY → $REMOTE_SERVER:$DIRECTORY"
|
||||
|
||||
for i in $(seq 1 "$RETRY_COUNT"); do
|
||||
if check_server_online; then break; fi
|
||||
if ping -c 1 "$REMOTE_SERVER" &>/dev/null; then break; fi
|
||||
echo "Remote $REMOTE_SERVER_NAME down, retrying in $SLEEP seconds..."
|
||||
sleep "$SLEEP"
|
||||
done
|
||||
check_server_online || { echo "Remote $REMOTE_SERVER_NAME unreachable. Exiting."; exit 1; }
|
||||
ping -c 1 "$REMOTE_SERVER" &>/dev/null || { echo "Remote $REMOTE_SERVER_NAME unreachable. Exiting."; exit 1; }
|
||||
|
||||
wait_for_rsync
|
||||
stop_containers
|
||||
get_rsync_opts
|
||||
|
||||
RSYNC_OPTS=(-av --info=progress2 --human-readable --bwlimit="$BW_LIMIT" --delete --inplace --no-whole-file)
|
||||
for ex in "${EXCLUDE_DIRS[@]}"; do RSYNC_OPTS+=(--exclude="$ex"); done
|
||||
[ "$DRY_RUN" = true ] && RSYNC_OPTS+=("--dry-run")
|
||||
|
||||
@@ -90,7 +101,7 @@ main() {
|
||||
done
|
||||
|
||||
start_containers
|
||||
echo "[$LOCAL_SERVER_NAME] Sync complete."
|
||||
[[ "$ENABLE_LOGGING" == true ]] && echo "[$LOCAL_SERVER_NAME] Sync complete."
|
||||
}
|
||||
|
||||
main
|
||||
Reference in New Issue
Block a user