Created
July 3, 2019 18:04
-
-
Save sjnovick/86681113d006c552ad2199c9fbd8f9bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# put_ftps.sh | |
# | |
# Delivers CSV's containg student data to hosted clearinghouse via FTPS. | |
# | |
# sjnovick 2019 | |
# | |
# PARAMETERS EXPECTED | |
# 1 = FTPS username | |
# 2 = FTPS password | |
# PARAMETERS (UNCOMMENT THESE TO USE AUTOMIC LOGIN MANAGER) | |
USER=$1 | |
PASS=$2 | |
# PARAMETERS (UNCOMMENT THESE TO USE LOCAL CREDS) | |
#USER="redacted" | |
#PASS="" | |
# FILE PATHS (SET THESE TO MATCH YOUR ENVIRONMENT) | |
LOGFILE="/app/put_ftps.log" # Where to write log file | |
PICKUP="/app/pickup" # Where to look for CSV files | |
ARCHIVE="/app/archive" # Where to archive CSV files | |
# PATTERN MATCHING (SET THESE TO MATCH YOUR ENVIRONMENT) | |
REGEX1='redacted.*\.csv' # Regex pattern matching | |
GLOB1='redacted*.csv' # Glob pattern matching | |
# Date variable | |
DATE2="$(date +"%m-%d-%y_%H%M%S")" | |
# FTP-related variables | |
SERVER="redacted" | |
PORT="990" | |
# Function: put_to_server | |
# Uses 'lftp' program to put files to FTPS server | |
# Accepts $PICKUP/$GLOB1 as parameter for mput command | |
put_to_server () { | |
# DoThis is a Linux "HereDoc" | |
lftp <<DoThis | |
set xfer:clobber on | |
set ftp:ssl-protect-data on | |
set ssl:verify-certificate no | |
connect -p $PORT -u $USER,$PASS $SERVER | |
mput $1 | |
quit | |
DoThis | |
} | |
# redirect STDERR to STDOUT and append both to LOGFILE | |
exec 1>> $LOGFILE 2>&1 | |
# log header | |
echo "<<< Starting: $(date) >>>" | |
# see if any matching CSV's exist in $PICKUP | |
ls $PICKUP | grep -q "$REGEX1" | |
# evaluate exit status of previous grep command | |
# if grep found something, $? = 0 | |
# if grep found nothing, $? = 1 | |
if [ $? -eq 0 ] | |
then | |
echo "CSV's found in $PICKUP ! Let's go..." | |
# create archive directory | |
#echo "Creating directory $ARCHIVE/$DATE2 ..." | |
#mkdir $ARCHIVE/$DATE2 | |
# copy files from $PICKUP to archive directory | |
echo "Copying CSV's to $ARCHIVE ..." | |
#cp -v $PICKUP/$GLOB1 $ARCHIVE/$DATE2 | |
cp -v $PICKUP/$GLOB1 $ARCHIVE | |
# copy files via put_to_server function | |
echo "Starting FTPS transfer..." | |
put_to_server "$PICKUP/$GLOB1" | |
# delete files from $PICKUP | |
echo "Deleting CSV's from $PICKUP ..." | |
rm -vf $PICKUP/$GLOB1 | |
else | |
echo "CSV's not found in $PICKUP ! Aborting..." | |
fi | |
# log footer | |
echo "<<< Finished: $(date) >>>" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment