Add noctopus_tyquant.sh
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
#!/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 filename to its destination directory
|
||||
# Format: ["filename.csv"]="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 keeps 13 months.
|
||||
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 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 [[ ! -f "$src" ]]; then
|
||||
log "ERROR: Source file not found: $src"
|
||||
return 1
|
||||
fi
|
||||
|
||||
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 filename in "${!FILE_DEST_MAP[@]}"; do
|
||||
dest_dir="${FILE_DEST_MAP[$filename]}"
|
||||
copy_and_chown "$SRC_DIR/$filename" "$dest_dir" "$TARGET_USER" || (( ERRORS++ ))
|
||||
done
|
||||
|
||||
if [[ $ERRORS -eq 0 ]]; then
|
||||
log "===== END copy_csv_files [SUCCESS] =================================="
|
||||
exit 0
|
||||
else
|
||||
log "===== END copy_csv_files [FAILED] ($ERRORS error(s)) ================"
|
||||
exit 1
|
||||
fi
|
||||
Reference in New Issue
Block a user