-
Notifications
You must be signed in to change notification settings - Fork 3
/
upload-transactions.sh
executable file
·57 lines (51 loc) · 1.52 KB
/
upload-transactions.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/bin/bash
TXN_PATH=/mnt/exports
TXN_FILE=ecivis-transactions.csv
SSH_KEY=$HOME/.ssh/ecivis-data-integration.key
SFTP_HOST=di-ft-test.ecivis.com
SFTP_USER=sftpuser
SFTP_PATH=/home/$SFTP_USER/data/queue
MAIL_ENABLED=yes
MAIL_SUBJECT="eCivis Transaction Upload"
error() {
if [ "_$MAIL_ENABLED" == _yes ]; then
echo -e "Error: $1" | tr -d \\r | mail -s "$MAIL_SUBJECT: Error" "$MAIL_TO"
else
echo "Error: $1"
fi
exit 1
}
# Validation
if [ ! -f "$TXN_PATH/$TXN_FILE" ]; then
error "The transaction file ($TXN_PATH/$TXN_FILE) was not found."
elif [ ! -f "$SSH_KEY" ]; then
error "The SFTP user's private key file ($SSH_KEY) was not found."
fi
# Ensure the private key is protected
chmod 0600 "$SSH_KEY"
# Upload transaction file
SFTP_OUT=$(
sftp -oIdentityFile="$SSH_KEY" -oIdentitiesOnly=yes -oCheckHostIP=no -oStrictHostKeyChecking=no -oLogLevel=INFO -oCompression=yes $SFTP_USER@$SFTP_HOST 2>&1 <<EOF
cd $SFTP_PATH
put $TXN_PATH/$TXN_FILE
ls -l $TXN_FILE
quit
EOF
)
# Notify administrator
if [ $? == 0 ]; then
if [ "_$MAIL_ENABLED" == _yes ]; then
echo -e "Successfully uploaded $TXN_FILE to $SFTP_HOST\n\n$SFTP_OUT" | tr -d \\r | mail -s "$MAIL_SUBJECT: Success" "$MAIL_TO"
else
echo "Successfully uploaded $TXN_FILE to $SFTP_HOST"
fi
exit 0
else
if [ "_$MAIL_ENABLED" == _yes ]; then
echo -e "Error uploading $TXN_FILE to $SFTP_HOST\n\n$SFTP_OUT" | tr -d \\r | mail -s "$MAIL_SUBJECT: Failure" "$MAIL_TO"
else
echo "Error uploading $TXN_FILE to $SFTP_HOST"
fi
exit 1
fi