原创 luojiabao lv51933
#!/bin/bash
# add by hkbn 2024/05/10
# upload files in ErpAck
#ebs server
#...
ret=0
#app server
SRC_FTP_BASE="/HNAS/CUX003/Esker/files"
#middle server
L_DEST_FTP_USER="erpclient";
L_DEST_FTP_SERVER="192.168.90.194";
L_DEST_FTP_BASE="/home/erpclient/Esker/file"
#middle server -> sftp server
#middle->QA sftp
#ErpAck ErpAck
#In In
#In_InvoicesUpdate In_InvoicesUpdate
#In_MasterData In_MasterData
#In_PaidInvoices In_PaidInvoices
#In_BudgetImport In_BudgetImport
#In_ContractImport In_ContractImport
#In_FiscalArchivesUpdate In_FiscalArchivesUpdate
#move files to archive rather than clean them ,
#find $L_DEST_FTP_BASE/$L_DEST_FTP_NAME/ -maxdepth 1 -type f -exec rm -rf {} \;
#if [[ "$(ls -A $L_DEST_FTP_BASE/$L_DEST_FTP_NAME/ | wc -l)" -eq 0 ]]; then
# echo "clean files success!"
#else
# echo "clean files failed,plz chk the folder $L_DEST_FTP_BASE/$L_DEST_FTP_NAME/ !"
# ret=$?
# exit $ret
#fi
#esker sftp server
#R_DEST_FTP_USER="[email protected]"
R_DEST_FTP_USER="[email protected]"
R_DEST_FTP_SERVER="sftp-az3.ondemand.esker.com"
MID_DIRS=("ErpAck" "In" "In_InvoicesUpdate" "In_MasterData" "In_PaidInvoices" "In_BudgetImport" "In_ContractImport" "In_FiscalArchivesUpdate")
echo "start :: loading upload files from middle to sftp ..."
MID_SOURCE_DIR=""
REMOTE_DEST_DIR=""
FILES=0
echo "before middle->sft ; ret: $ret"
if [[ $ret -eq 0 ]]; then
for L_DEST_FTP_NAME in "${MID_DIRS[@]}"; do
MID_SOURCE_DIR="${L_DEST_FTP_BASE}/${L_DEST_FTP_NAME}"
MID_ARCHIVE_DIR="${MID_SOURCE_DIR}/archive"
echo "MID_SOURCE_DIR :: $MID_SOURCE_DIR"
echo "L_DEST_FTP_NAME :: $L_DEST_FTP_NAME"
if [ "$L_DEST_FTP_NAME" == "Out_Payment" ]; then
REMOTE_DEST_DIR="Out_PaidInvoices"
else
REMOTE_DEST_DIR="$L_DEST_FTP_NAME"
fi
echo "REMOTE_DEST_DIR: $REMOTE_DEST_DIR"
ssh -t -o StrictHostKeyChecking=no -oBatchMode=no ${L_DEST_FTP_USER}@${L_DEST_FTP_SERVER} "bash -s" ${L_DEST_FTP_NAME} ${MID_SOURCE_DIR} ${MID_ARCHIVE_DIR} ${L_DEST_FTP_BASE} ${R_DEST_FTP_USER} ${R_DEST_FTP_SERVER} ${REMOTE_DEST_DIR} << 'ENDSSH'
echo "add file to deffiles for $1..."
echo "traversing folder on $2..."
echo "following mid server archive on $3..."
echo "base dest path is $4..."
declare -a deffiles
deffiles=()
for file in "${2}"/*; do
if [[ -f "$file" ]]; then
deffiles+=("$(basename "$file")")
fi
done
echo "the files are ${deffiles[@]}"
echo "sftp user: ${5}, sftp server: ${6}"
sftp -oBatchMode=no ${5}@${6} << ENDSFTP
cd ${7}/
put ${4}/${1}/*
bye
ENDSFTP
if [[ "${#deffiles[@]}" -gt 0 ]]; then
echo "The following files on the path $2 will be moved to the $3"
echo "=> ${deffiles[@]}"
for f in "${deffiles[@]}"; do
mv ${2}/$f $3
done
else
echo "$2 have no files need to be moved to archive."
fi
chmod -R 755 ${4}/Out
ENDSSH
done
else
echo "transfer files from app to middle occur errors that influence uploading files from middle to sftp,pls chk previous steps!"
ret=$?
echo "ret: $ret"
fi
#exit with success
if [ $ret -eq 0 ]; then
echo "All directories processed successfully"
fi
exit $ret