From 3cf4a230e1ef755059bf9d34affd2f9126a723ca Mon Sep 17 00:00:00 2001 From: kris Date: Sat, 18 Apr 2026 16:41:53 +0100 Subject: [PATCH] Add noct_sftp_copy.sh --- noct_sftp_copy.sh | 143 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 noct_sftp_copy.sh diff --git a/noct_sftp_copy.sh b/noct_sftp_copy.sh new file mode 100644 index 0000000..045a932 --- /dev/null +++ b/noct_sftp_copy.sh @@ -0,0 +1,143 @@ +#!/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 * * * /path/to/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=396 + +# ----------------------------------------------------------------------------- + +# --- 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 copy_csv_files ============================================" + +# 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 the matching file(s) + 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 + + # Warn if multiple files match — only the first will be copied + if [[ ${#matches[@]} -gt 1 ]]; then + log "WARNING: Multiple files match pattern '$pattern' — only '$(basename "${matches[0]}")' will be copied" + fi + + copy_and_chown "${matches[0]}" "$dest_dir" "$TARGET_USER" || (( ERRORS++ )) +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 \ No newline at end of file