JBAS-1975 Archive ADPToJDE files same as ADP
authorBill Erickson <berickxx@gmail.com>
Fri, 9 Feb 2018 21:09:50 +0000 (16:09 -0500)
committerBill Erickson <berickxx@gmail.com>
Thu, 21 Mar 2019 19:46:23 +0000 (15:46 -0400)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
KCLS/admin-scripts/jde-file-mgmt.sh [new file with mode: 0755]

diff --git a/KCLS/admin-scripts/jde-file-mgmt.sh b/KCLS/admin-scripts/jde-file-mgmt.sh
new file mode 100755 (executable)
index 0000000..ad57fa1
--- /dev/null
@@ -0,0 +1,122 @@
+#!/bin/bash
+# Author: Bill Erickson <bserickson@kcls.org>
+#
+# Script for archiving ADP employee files and notifying KCLS 
+# and ADP staff when files are not present.
+set -ueo pipefail
+
+BASE_PATH=/home/sftpadp/JDE
+PROC_PATH="$BASE_PATH/Processed"
+MAIL_CMD=mailx
+MAIL_RECIP=bserickson@kcls.org,dbebeling@kcls.org
+#MAIL_RECIP=bserickson@kcls.org,dbebeling@kcls.org,smylavarapu@kcls.org,itsopsalert@kcls.org
+#ADP_RECIP=northwestservice@adp.com
+CURRENT_YMD=$(date +'%F');
+COMPRESS_AGE="6" # zip files older than this many days
+DELETE_AGE="365" # delete files older than this many days
+TEST_MODE=""
+STAGE="";
+
+FILES=(ADPtoJDE.csv)
+
+function announce {
+    echo "$(date +'%F %T') $(hostname): $1"
+}
+
+ERROR_MESSAGES=""
+function report {
+    #ERROR_MESSAGES="$ERROR_MESSAGES$(date +'%F %T') $1\n"
+    ERROR_MESSAGES="$ERROR_MESSAGES$1\n"
+    announce "$1"
+}
+
+# email summary report
+function report_results {
+
+    if [ ! "$(which $MAIL_CMD)" ]; then
+        return;
+    fi
+
+    if [ -z "$ERROR_MESSAGES" ]; then
+        return;
+    fi
+
+    if [ $STAGE == 1 ]; then
+        SUBJECT="ADPtoJDE File Error [KCLS $(date +'%F')]"
+        # stage-1 failures also notify ADP
+        [ -z "$TEST_MODE" ] && MAIL_RECIP="$MAIL_RECIP,$ADP_RECIP"
+        ERROR_MESSAGES="
+Hi ADP Team,\n
+We did not receive the the following files today.\n
+$ERROR_MESSAGES"
+    else 
+        SUBJECT="ADP2JDE File 2nd Check [$(date +'%F')]"
+    fi
+
+    echo -e "$ERROR_MESSAGES" | $MAIL_CMD -s "$SUBJECT" $MAIL_RECIP
+}
+
+function check_files {
+
+    for FILE in "${FILES[@]}"; do
+        BASE_FILE="${FILE%.*}"
+        DEST_FILE="$PROC_PATH/$BASE_FILE.$CURRENT_YMD.csv"
+
+        if [ $STAGE == 2 -a -f $DEST_FILE ]; then
+            # File already archived.  Nothing to do.
+            continue
+        fi
+
+        if [ -f "$FILE" ]; then
+            if [ $STAGE == 1 ]; then
+                announce "Found file $FILE";
+            else
+                # Always report file disposition in stage 2.
+                report "$FILE : Found";
+            fi;
+            mv $FILE $DEST_FILE
+        else
+            report "$FILE : NOT Found";
+        fi;
+    done;
+}
+
+function cleanup_files {
+
+    # 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 {} \;
+}
+
+function usage {
+    cat <<USAGE
+        $0 -s 1 / 2
+USAGE
+    exit;
+}
+
+while getopts "hs:t" opt; do
+    case $opt in
+        s) STAGE="$OPTARG";;
+        t) TEST_MODE="1";;
+        h) usage;
+    esac
+done;
+
+[ -z "$STAGE" ] && usage;
+
+if [ -n "$TEST_MODE" ]; then
+    announce "Test Mode"
+    BASE_PATH=/tmp/pending/
+    PROC_PATH="$BASE_PATH/Processed"
+    MAIL_RECIP=opensrf@localhost,sftpagent@localhost
+fi
+
+cd $BASE_PATH;
+check_files;
+[ $STAGE == 1 ] && cleanup_files;
+report_results;
+