Files
kx-scripts/noct_sftp_copy.sh
T
2026-04-18 16:51:24 +01:00

149 lines
4.8 KiB
Bash

declare -A FILE_DEST_MAP=(
["noct_fxdcf*.csv"]="/app/tyquant/deploy/kxinstall/delta-data/fileStore/fileUpload/noctData/dailyCashFlow"
["noct_evrFxRate*.csv"]="/app/tyquant/deploy/kxinstall/delta-data/fileStore/fileUpload/noctData/EVRFxRate"
["noct_fxtv*.csv"]="/app/tyquant/deploy/kxinstall/delta-data/fileStore/fileUpload/noctData/transactionView"
)
#!/bin/bash
# =============================================================================
# Script: noct_sftp_copy.sh
# Description: Copies 3 CSV files from a single source directory to 3
# separate destination directories and changes ownership to
# a specified user. Designed for silent cron execution with
# full logging on Red Hat Enterprise Linux.
# Cron example (runs daily at 02:00 as root):
# 0 2 * * * /usr/local/bin/noct_sftp_copy.sh
# =============================================================================
# --- Configuration -----------------------------------------------------------
# Owner to assign to all copied files (change as needed)
TARGET_USER="tyquant"
# Source directory containing all CSV files
SRC_DIR="/app/smbctyuat/NoctSFTP"
# Map each CSV glob pattern to its destination directory
# Format: ["glob_pattern"]="destination/path"
declare -A FILE_DEST_MAP=(
["noct_fxdcf*.csv"]="/app/tyquant/deploy/kxinstall/delta-data/fileStore/fileUpload/noctData/dailyCashFlow"
["noct_evrFxRate*.csv"]="/app/tyquant/deploy/kxinstall/delta-data/fileStore/fileUpload/noctData/EVRFxRate"
["noct_fxtv*.csv"]="/app/tyquant/deploy/kxinstall/delta-data/fileStore/fileUpload/noctData/transactionView"
)
# Log file location and retention (days)
LOG_FILE="/var/log/noct_sftp_copy.log"
LOG_RETAIN_DAYS=30
# -----------------------------------------------------------------------------
# --- Logging -----------------------------------------------------------------
# Ensure the log directory exists
mkdir -p "$(dirname "$LOG_FILE")"
# All stdout and stderr from this point on goes only to the log file (silent to cron)
exec >> "$LOG_FILE" 2>&1
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*"
}
# Rotate out log entries older than LOG_RETAIN_DAYS
rotate_log() {
local tmp cutoff
tmp=$(mktemp)
cutoff=$(date -d "$LOG_RETAIN_DAYS days ago" '+%Y-%m-%d')
grep -v "^\[" "$LOG_FILE" > "$tmp" || true
grep "^\[" "$LOG_FILE" | awk -v cutoff="[$cutoff" '$1 >= cutoff' >> "$tmp" || true
mv "$tmp" "$LOG_FILE"
}
# -----------------------------------------------------------------------------
log "===== START noct_sftp_copy ============================================"
# Prune old log entries
rotate_log
# Check that the script is run as root (required for chown)
if [[ $EUID -ne 0 ]]; then
log "ERROR: Script must be run as root (or with sudo) to change file ownership."
log "===== END noct_sftp_copy [FAILED] ==================================="
exit 1
fi
# Verify the target user exists
if ! id "$TARGET_USER" &>/dev/null; then
log "ERROR: User '$TARGET_USER' does not exist."
log "===== END noct_sftp_copy [FAILED] ==================================="
exit 1
fi
# Verify the source directory exists
if [[ ! -d "$SRC_DIR" ]]; then
log "ERROR: Source directory not found: $SRC_DIR"
log "===== END noct_sftp_copy [FAILED] ==================================="
exit 1
fi
# --- Helper ------------------------------------------------------------------
copy_and_chown() {
local src="$1"
local dest_dir="$2"
local owner="$3"
local filename
filename=$(basename "$src")
if ! mkdir -p "$dest_dir"; then
log "ERROR: Could not create directory: $dest_dir"
return 1
fi
if ! cp "$src" "$dest_dir/"; then
log "ERROR: Failed to copy '$filename' to '$dest_dir'"
return 1
fi
if ! chown "$owner" "$dest_dir/$filename"; then
log "ERROR: Failed to change ownership of '$dest_dir/$filename'"
return 1
fi
log "OK: '$filename' -> '$dest_dir/' (owner: $owner)"
}
# --- Main --------------------------------------------------------------------
ERRORS=0
for pattern in "${!FILE_DEST_MAP[@]}"; do
dest_dir="${FILE_DEST_MAP[$pattern]}"
# Expand the glob pattern to find all matching files
matches=( "$SRC_DIR"/$pattern )
# Check if the glob returned any real matches
if [[ ! -f "${matches[0]}" ]]; then
log "ERROR: No file found matching pattern '$pattern' in '$SRC_DIR'"
(( ERRORS++ ))
continue
fi
log "INFO: Found ${#matches[@]} file(s) matching pattern '$pattern'"
# Copy all matching files to the destination
for src_file in "${matches[@]}"; do
copy_and_chown "$src_file" "$dest_dir" "$TARGET_USER" || (( ERRORS++ ))
done
done
if [[ $ERRORS -eq 0 ]]; then
log "===== END noct_sftp_copy [SUCCESS] =================================="
exit 0
else
log "===== END noct_sftp_copy [FAILED] ($ERRORS error(s)) ================"
exit 1
fi