|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +################################################################################ |
| 4 | +# Name : push_exports.sh # |
| 5 | +# Author : Ibrahim Musayev # |
| 6 | +# Purpose : Automation of SFTP Gateway file transfers # |
| 7 | +# # |
| 8 | +# History : 05.09.22 Ibrahim Musayev, creation # |
| 9 | +################################################################################ |
| 10 | + |
| 11 | +TIMESTAMP=`date +'%Y%m%d_%H%M%S'` |
| 12 | +DATE=`date +'%y%m%d'` |
| 13 | +YEAR=`date +'%Y'` |
| 14 | +WEEK=`date -d 'last week' +'%U'` |
| 15 | +LAST_MONDAY=`date -d 'last monday' +'%Y%m%d'` |
| 16 | +LAST_SUNDAY=`date -d 'last sunday' +'%Y%m%d'` |
| 17 | +DESTINATION=$Gateway |
| 18 | +KEY=$PUBLIC_KEY |
| 19 | +hostname=$(hostname -s) |
| 20 | +FILENAME_1="${MASKED}_${LAST_MONDAY}__${LAST_SUNDAY}.zip" |
| 21 | +FILENAME_2="${MASKED}_${LAST_MONDAY}__${LAST_SUNDAY}.zip" |
| 22 | +FILENAME_3="${MASKED}_${LAST_MONDAY}__${LAST_SUNDAY}.zip" |
| 23 | +OPT_ROOT="/opt/${DIR_1}/${YEAR}" |
| 24 | +ZIP_SOURCE="${MASKED}.${WEEK}.${DATE}.zip" |
| 25 | +LOGFILE=$C_LOG |
| 26 | +DEBUG=false |
| 27 | + |
| 28 | +if [[ ! -d $(dirname ${LOGFILE}) ]]; then |
| 29 | + mkdir -p $(dirname ${LOGFILE}) |
| 30 | +fi |
| 31 | + |
| 32 | +FindAndPush () { |
| 33 | + cd $OPT_ROOT/w$WEEK |
| 34 | + FILELIST=`find . -type f \( -name "${FILENAME_1}" -o -name "${FILENAME_2}" -o -name "${FILENAME_3}" \) 2>/dev/null` |
| 35 | + |
| 36 | + for FILE in $FILELIST; do |
| 37 | + if [ -s ${FILE} ]; then |
| 38 | + zip -qu ${ZIP_SOURCE} ${FILE} |
| 39 | + EXITCODE=$? |
| 40 | + else |
| 41 | + echo -e "${TIMESTAMP} INFO Zip file is empty. Waiting for the job to complete ${FILE}.\n" | tee -a ${LOGFILE} |
| 42 | + fi |
| 43 | + done |
| 44 | + |
| 45 | + echo "put ${ZIP_SOURCE}" |sftp -P 6323 -p -i /home/$USER/.ssh/${KEY} ${DESTINATION} >> ${LOGFILE} |
| 46 | + |
| 47 | + if [[ ${EXITCODE} -ne 0 ]]; then |
| 48 | + echo "`date +'%Y%m%d_%H%M%S'` ERROR Got exit code ${EXITCODE} during zipping. Exiting ..." | tee -a ${LOGFILE} |
| 49 | + exit 1 |
| 50 | + else |
| 51 | + TIMESTAMP=`date +'%Y%m%d_%H%M%S'` |
| 52 | + echo -e "${TIMESTAMP} INFO File transfer to server ${DESTINATION} completed.\n" | tee -a ${LOGFILE} |
| 53 | + exit 0 |
| 54 | + fi |
| 55 | +} |
| 56 | + |
| 57 | +FindAndPush |
0 commit comments