new format avoid WHILE loop READ

This commit is contained in:
Radek
2025-02-21 14:50:28 +00:00
parent 397d480141
commit c48b65a253

View File

@@ -5,7 +5,7 @@ cd /opt/AZURE || exit 1
# Configurable variables # Configurable variables
BUSINESS_HOURS_START=9 BUSINESS_HOURS_START=9
BUSINESS_HOURS_END=17 BUSINESS_HOURS_END=20
AZURE_ACCOUNT="" AZURE_ACCOUNT=""
AZURE_SAS="" AZURE_SAS=""
LOCK_FILE="/tmp/run_azcopy.lock" LOCK_FILE="/tmp/run_azcopy.lock"
@@ -27,39 +27,6 @@ if [[ -z "$SOURCE_LIST_FILE" || ! -f "$SOURCE_LIST_FILE" ]]; then
fi fi
# Lock file to prevent multiple instances # 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"
# Loop through directories in the list
while IFS=">" read -r SOURCE_DIR DEST_CONTAINER; do
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" echo "ERROR: Invalid directory: $SOURCE_DIR. Exiting." | tee -a "$LOG_FILE"
rm -f "$LOCK_FILE" rm -f "$LOCK_FILE"
exit 1 exit 1
@@ -72,7 +39,6 @@ while IFS=">" read -r SOURCE_DIR DEST_CONTAINER; do
fi fi
DEST_URL="$AZURE_ACCOUNT/$DEST_CONTAINER" DEST_URL="$AZURE_ACCOUNT/$DEST_CONTAINER"
echo "Syncing $SOURCE_DIR to container: $DEST_CONTAINER" | tee -a "$LOG_FILE" echo "Syncing $SOURCE_DIR to container: $DEST_CONTAINER" | tee -a "$LOG_FILE"
# Check if the container exists by attempting to write a small test file # Check if the container exists by attempting to write a small test file
@@ -87,34 +53,35 @@ while IFS=">" read -r SOURCE_DIR DEST_CONTAINER; do
fi fi
rm -f "$TEST_FILE" rm -f "$TEST_FILE"
# Run azcopy for actual sync in the background # Run azcopy in the foreground (one directory at a time)
if [[ "$LOGGING" == "true" ]]; then if [[ "$LOGGING" == "true" ]]; then
azcopy sync "$SOURCE_DIR" "$DEST_URL?$AZURE_SAS" --recursive --cap-mbps "$BANDWIDTH_CAP" | tee -a "$LOG_FILE" & azcopy sync "$SOURCE_DIR" "$DEST_URL?$AZURE_SAS" --recursive --cap-mbps "$BANDWIDTH_CAP" | tee -a "$LOG_FILE" &
else else
azcopy sync "$SOURCE_DIR" "$DEST_URL?$AZURE_SAS" --recursive --cap-mbps "$BANDWIDTH_CAP" > /dev/null 2>&1 & azcopy sync "$SOURCE_DIR" "$DEST_URL?$AZURE_SAS" --recursive --cap-mbps "$BANDWIDTH_CAP" > /dev/null 2>&1 &
fi fi
AZCOPY_PID=$! AZCOPY_PID=$!
# Monitor the process # Monitor the process every 30 seconds
while kill -0 $AZCOPY_PID 2>/dev/null; do while kill -0 $AZCOPY_PID 2>/dev/null; do
if is_business_hours; then if is_business_hours; then
echo -e "\nBusiness hours started! Stopping azcopy..." | tee -a "$LOG_FILE" echo -e "\nBusiness hours started! Stopping azcopy..." | tee -a "$LOG_FILE"
kill $AZCOPY_PID kill $AZCOPY_PID
wait $AZCOPY_PID 2>/dev/null # Ensure the process is fully stopped wait $AZCOPY_PID 2>/dev/null # Ensure process stops completely
rm -f "$LOCK_FILE" rm -f "$LOCK_FILE"
exit 1 exit 1
fi fi
sleep 30 # Check every 30 seconds sleep 30 # Check every 30 seconds
done done
# Check for sync failure # Check if sync failed
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
echo "ERROR: Sync failed for $SOURCE_DIR to $DEST_CONTAINER. Stopping script." | tee -a "$LOG_FILE" echo "ERROR: Sync failed for $SOURCE_DIR to $DEST_CONTAINER. Stopping script." | tee -a "$LOG_FILE"
rm -f "$LOCK_FILE" rm -f "$LOCK_FILE"
exit 1 exit 1
fi fi
done < "$SOURCE_LIST_FILE" done
echo "All directories synced successfully!" | tee -a "$LOG_FILE" echo "All directories synced successfully!" | tee -a "$LOG_FILE"
@@ -125,4 +92,3 @@ echo "All directories listed in $SOURCE_LIST_FILE have been synced." >> "$COMPLE
echo "Completion report generated: $COMPLETION_REPORT" echo "Completion report generated: $COMPLETION_REPORT"
rm -f "$LOCK_FILE" rm -f "$LOCK_FILE"
exit 0 exit 0