diff --git a/run_azcopy-multi-dir-multi-container.sh b/run_azcopy-multi-dir-multi-container.sh old mode 100644 new mode 100755 index 64bbe6e..c2ff94e --- a/run_azcopy-multi-dir-multi-container.sh +++ b/run_azcopy-multi-dir-multi-container.sh @@ -27,6 +27,43 @@ if [[ -z "$SOURCE_LIST_FILE" || ! -f "$SOURCE_LIST_FILE" ]]; then fi # Lock file to prevent multiple instances +if [[ -f "$LOCK_FILE" ]]; then + PID=$(cat "$LOCK_FILE") + if kill -0 "$PID" 2>/dev/null; then + echo "Another instance (PID $PID) is already running. Exiting..." + exit 1 + else + echo "Stale lock file found. Removing..." + rm -f "$LOCK_FILE" + fi +fi +echo $$ > "$LOCK_FILE" + +# Function to check business hours +is_business_hours() { + HOUR=$(printf "%d" "$(date +%H)") # Convert to decimal safely + [[ $HOUR -ge $BUSINESS_HOURS_START && $HOUR -lt $BUSINESS_HOURS_END ]] +} + +# Stop if running during business hours +if is_business_hours; then + echo "Business hours detected ($BUSINESS_HOURS_START:00 - $BUSINESS_HOURS_END:00). Exiting..." + rm -f "$LOCK_FILE" + exit 1 +fi + +echo "Starting sync job at $(date)" | tee -a "$LOG_FILE" + +# Read the directory list file into an array +mapfile -t SYNC_JOBS < "$SOURCE_LIST_FILE" + +# Loop through the array and process each entry +for LINE in "${SYNC_JOBS[@]}"; do + IFS=">" read -r SOURCE_DIR DEST_CONTAINER <<< "$LINE" + SOURCE_DIR=$(echo "$SOURCE_DIR" | xargs) # Trim spaces + DEST_CONTAINER=$(echo "$DEST_CONTAINER" | xargs) # Trim spaces + + if [[ -z "$SOURCE_DIR" || ! -d "$SOURCE_DIR" ]]; then echo "ERROR: Invalid directory: $SOURCE_DIR. Exiting." | tee -a "$LOG_FILE" rm -f "$LOCK_FILE" exit 1 @@ -92,3 +129,4 @@ echo "All directories listed in $SOURCE_LIST_FILE have been synced." >> "$COMPLE echo "Completion report generated: $COMPLETION_REPORT" rm -f "$LOCK_FILE" exit 0 +