Untitled

 avatar
unknown
plain_text
14 days ago
2.1 kB
6
Indexable
#!/bin/bash

# Check if both source and destination directories are provided
if [ "$#" -ne 2 ]; then
    echo "Usage: $0 /path/to/source /path/to/destination"
    exit 1
fi

# Assign the first and second arguments to SOURCE_DIR and DEST_DIR
SOURCE_DIR="$1"
DEST_DIR="${2%/}/"  # Ensure trailing slash
BASE_DIR=$(dirname "$DEST_DIR")  # Parent directory of DEST_DIR
STAGING_DIR="${BASE_DIR}/LIT-Transfer-Source-Staging"  # Staging directory

# Ensure SOURCE_DIR has a trailing slash
SOURCE_DIR="${SOURCE_DIR%/}/"

REQUIRED_FILE="processingMCP.xml"
SIP_NAME=$(basename "$SOURCE_DIR")  # Extract SIP folder name
SIP_STAGING_DIR="${STAGING_DIR}/${SIP_NAME}"  # Unique staging directory per SIP
FLAG_FILE="${SIP_STAGING_DIR}/FLAG_FILE"  # Flag file

# Create the staging directory for this SIP
mkdir -p "$SIP_STAGING_DIR"

# Check if the required file exists in the source directory
if [ ! -f "${SOURCE_DIR}${REQUIRED_FILE}" ]; then
    echo "Error: ${REQUIRED_FILE} not found in the source directory."
    echo "Please add ${REQUIRED_FILE} to ${SOURCE_DIR} before starting the transfer."
    exit 1
fi

# Create a flag file to indicate that transfer is in progress
touch "$FLAG_FILE"
echo "Flag file created for SIP: $SIP_NAME"

# Ensure cleanup on failure
trap 'rm -f "$FLAG_FILE"; exit' INT TERM EXIT

# Perform rsync operation to the SIP's staging directory
rsync -av --inplace --stats --chmod=Dugo=rwx,Fugo=rwx --chown=333:333 "$SOURCE_DIR/" "$SIP_STAGING_DIR/"

# Pause for manual check
# This can be uncommented for testing purposes or
# if we want to confirm whether the file in destination was created
#read -p "Press Enter to continue after verifying the flag file is created..."

# Check if rsync was successful
if [ $? -ne 0 ]; then
    echo "Error: Rsync transfer failed for $SIP_NAME."
    rm -f "$FLAG_FILE"  # Clean up flag if rsync fails
    exit 1
fi

# Move the fully copied SIP from staging to final destination
mv "$SIP_STAGING_DIR" "$DEST_DIR"

# Remove the TRANSFER_IN_PROGRESS flag to indicate completion
rm -f "$FLAG_FILE"
echo "Flag file removed: $FLAG_FILE for SIP: $SIP_NAME and moved to $DEST_DIR"

echo "Copy completed."
Leave a Comment