JBAS-1739 ADP file management script
authorBill Erickson <berickxx@gmail.com>
Tue, 28 Feb 2017 17:43:39 +0000 (12:43 -0500)
committerBill Erickson <berickxx@gmail.com>
Thu, 21 Mar 2019 19:46:23 +0000 (15:46 -0400)
Script to archive daily ADP files and alert when a daily file is not
delivered.  Runs on the SFTP server.

Signed-off-by: Bill Erickson <berickxx@gmail.com>
KCLS/admin-scripts/adp-file-mgmt.sh [new file with mode: 0755]

diff --git a/KCLS/admin-scripts/adp-file-mgmt.sh b/KCLS/admin-scripts/adp-file-mgmt.sh
new file mode 100755 (executable)
index 0000000..bd745ec
--- /dev/null
@@ -0,0 +1,60 @@
+#!/bin/bash
+set -ueo pipefail
+
+BASE_PATH=/home/sftpadp/pending/
+PROC_PATH="$BASE_PATH/Processed"
+MAIL_CMD=mailx
+MAIL_RECIP=bserickson@kcls.org,dbebeling@kcls.org,smylavarapu@kcls.org,itsopsalert@kcls.org
+#MAIL_RECIP=bserickson@kcls.org
+CURRENT_YMD=$(date +'%F');
+COMPRESS_AGE="6"
+DELETE_AGE="365"
+
+FILES=(AllEmployees.csv TerminatedEmployees.csv)
+
+function announce {
+    echo "$(date +'%F %T') $(hostname): $1"
+}
+
+ERROR_MESSAGES=""
+function report {
+    ERROR_MESSAGES="$ERROR_MESSAGES$(date +'%F %T') $1\n"
+    announce "$1"
+}
+
+# email import summary output
+function report_results {
+
+    if [ ! "$(which $MAIL_CMD)" ]; then
+        return;
+    fi
+
+    if [ -z "$ERROR_MESSAGES" ]; then
+        return;
+    fi
+
+    echo -e "$ERROR_MESSAGES" | $MAIL_CMD \
+        -s "ADP File Error [$(date +'%F')]" $MAIL_RECIP
+}
+
+cd $BASE_PATH;
+
+for FILE in "${FILES[@]}"; do
+    if [ -f "$FILE" ]; then
+        announce "Found file $FILE";
+        BASE_FILE="${FILE%.*}"
+        mv $FILE "$PROC_PATH/$BASE_FILE.$CURRENT_YMD.csv"
+    else
+        report "$FILE NOT FOUND!";
+    fi;
+done;
+
+# Delete files older than DELETE_AGE
+/usr/bin/find $PROC_PATH -type f -mtime +$DELETE_AGE -delete
+
+# Compress files older than COMPRESS_AGE
+/usr/bin/find $PROC_PATH -type f -mtime +$COMPRESS_AGE \
+    -not -name "*zip" -exec /usr/bin/zip -m {}.zip {} \;
+
+report_results;
+