Added Logging

This commit is contained in:
2026-03-25 00:30:36 +00:00
parent 4221b8a390
commit 935d06bfa9
3 changed files with 63 additions and 85 deletions
+15 -60
View File
@@ -1,36 +1,16 @@
#!/bin/bash
set -e
#-----------------------------------------------------------------------------------------------
#----------------- User Variables, Please adjust here in Master.conf as needed -----------------
#-----------------------------------------------------------------------------------------------
## This is the main Core Rsync script
#
# User variables can be adjusted in Master.conf
# Scripts now just contain runtime logic
#
# This allows for profiles and multiple things to use the core script
# Use arguments in unRAID User Plugin for directory
# Example: /mnt/user/appdata/unraid_scripts/Rsync/rsync.sh /mnt/user/appdata-test/Arrs/
#
# /mnt/user/appdata-test/Arrs/ is the argument
# If the last name /Arrs/ matches a profile in Master.conf, then profile defaults are used
# Otherwise, global configuration in Master.conf is used
#-----------------------------------------------------------------------------------------------
#----------------- User Variables, Please adjust here in Master.conf as needed -----------------
#-----------------------------------------------------------------------------------------------
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Load configs & helpers
source "$SCRIPT_DIR/../Master.conf"
source "$SCRIPT_DIR/../common.sh"
# ----------------------------- Input -----------------------------
DIRECTORY="$1"
if [[ -z "$DIRECTORY" ]]; then
echo "Usage: $0 <directory-to-sync> [--dry-run] [VAR=value ...]"
exit 1
fi
[[ -z "$DIRECTORY" ]] && { log "Usage: $0 <directory-to-sync> [--dry-run] [VAR=value ...]"; exit 1; }
shift
parse_args "$@"
@@ -46,62 +26,37 @@ RETRY_COUNT=${PROFILE_RETRY_COUNT[$PROFILE_NAME]:-$RETRY_COUNT}
SLEEP=${PROFILE_SLEEP[$PROFILE_NAME]:-$SLEEP}
EXCLUDE_DIRS=(${PROFILE_EXCLUDE_DIRS[$PROFILE_NAME]:-${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[*]}"
log "Detected profile: $PROFILE_NAME"
log "Settings: MAX_RSYNC_PROCS=$MAX_RSYNC_PROCS, BW_LIMIT=$BW_LIMIT, CONTAINERS=${CRITICAL_CONTAINER_NAMES[*]}, EXCLUDES=${EXCLUDE_DIRS[*]}, DRY_RUN=$DRY_RUN"
# ----------------------------- 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() {
[[ "$ENABLE_LOGGING" == true ]] && echo "[$LOCAL_SERVER_NAME] Syncing $DIRECTORY$REMOTE_SERVER:$DIRECTORY"
log "[$LOCAL_SERVER_NAME] Starting sync: $DIRECTORY$REMOTE_SERVER:$DIRECTORY"
for i in $(seq 1 "$RETRY_COUNT"); do
if ping -c 1 "$REMOTE_SERVER" &>/dev/null; then break; fi
echo "Remote $REMOTE_SERVER_NAME down, retrying in $SLEEP seconds..."
sleep "$SLEEP"
done
ping -c 1 "$REMOTE_SERVER" &>/dev/null || { echo "Remote $REMOTE_SERVER_NAME unreachable. Exiting."; exit 1; }
[[ $(check_server_online) ]] || { log "Remote $REMOTE_SERVER_NAME unreachable. Exiting."; exit 1; }
wait_for_rsync
stop_containers
get_rsync_opts
# Build rsync command
get_rsync_opts
for ex in "${EXCLUDE_DIRS[@]}"; do RSYNC_OPTS+=(--exclude="$ex"); done
[ "$DRY_RUN" = true ] && RSYNC_OPTS+=("--dry-run")
[[ "$DRY_RUN" == true ]] && RSYNC_OPTS+=("--dry-run")
for attempt in $(seq 1 "$RETRY_COUNT"); do
echo "Starting rsync attempt $attempt/$RETRY_COUNT..."
log "Starting rsync attempt $attempt/$RETRY_COUNT..."
if rsync "${RSYNC_OPTS[@]}" -e "ssh -i \"$SSH_KEY\" -T -o Compression=no -o IPQoS=throughput" \
"$DIRECTORY" "root@${REMOTE_SERVER}:$DIRECTORY"; then
echo "Rsync completed successfully."
log "Rsync completed successfully."
break
else
echo "Rsync attempt $attempt failed."
if [ "$attempt" -lt "$RETRY_COUNT" ]; then
echo "Retrying in $SLEEP seconds..."
sleep "$SLEEP"
else
echo "All $RETRY_COUNT rsync attempts failed."
start_containers
exit 1
fi
log "Rsync attempt $attempt failed."
[[ "$attempt" -lt "$RETRY_COUNT" ]] && { log "Retrying in $SLEEP seconds..."; sleep "$SLEEP"; } || { log "All rsync attempts failed."; start_containers; exit 1; }
fi
done
start_containers
[[ "$ENABLE_LOGGING" == true ]] && echo "[$LOCAL_SERVER_NAME] Sync complete."
log "[$LOCAL_SERVER_NAME] Sync complete."
}
main