Automating FTP File Transfer Verification with Bash Script
Managing file transfers, especially in bulk, can be a complex task. Automated scripts are essential for ensuring the integrity and success of such operations. This guide will walk you through creating a Bash script that checks whether an FTP server has successfully sent one or more files from a specified directory.
The script will:
Monitor the transfer of files from a specified directory. Confirm the success or failure of the transfer for each file. Count the number of successfully transferred files and those that failed. Generate a report based on the transfer status and send an email notification.Step-by-Step Guide
Let's start by defining and understanding the script. The script is stored in a Bash file and executed with appropriate permissions.
1. Store the Folder Path Containing the Files
We begin by storing the path to the directory in the script. This path will contain the files to be transferred:
FILES/home/
2. Initialize Variables to Hold Messages and Errors
Variables are initialized to hold messages and error counts:
MESSAGES ERRORS COUNT0 ERRORCOUNT0
3. Loop Through the Files in the Directory
We then loop through each file in the specified directory:
for f in $FILES do Get the base filename BASE`basename $f` Build the SFTP command. Note the space in the folder name. CMD"cd "$FILES"; CMD Execute the SFTP command. echo -e $CMD | sftp -oIdentityFilersa On success, make a note and delete the local copy of the file. if [ $? 0 ] MESSAGES"${MESSAGES}File $BASE was successfully transferred. COUNT$((COUNT 1)) Deleted local copy... rm -f $f fi On failure, add an error message. if [ $? ! 0 ] ERRORS"${ERRORS}File $BASE failed to transfer. ERRORCOUNT$((ERRORCOUNT 1)) fi done
4. Generate a Report Based on the Transfer Status
After the loop, we generate a report based on the transfer status:
SUBJECT"FTP Transfer Status Report" BODY if [ "${MESSAGES}" ! "" ] BODY"${BODY}File transfers succeeded: ${MESSAGES} " fi if [ "${ERRORS}" ! "" ] BODY"${BODY}File transfers failed: ${ERRORS} " fi
5. Send a Notification via Email
Finally, we send an email with the report:
echo -e $BODY | mail -s $SUBJECT xyz@
Summary
This script provides a robust solution for monitoring FTP transfers. It ensures that all necessary files are sent, and it sends notifications on success or failure. By customizing the script, you can extend its functionality to suit your specific needs, such as handling different authentication methods or transferring files to multiple FTP servers.