Automating FTP PUT Jobs from UNIX to a Windows FTP Server

Last month I had to find a way to automate the transfer of files from a UNIX database server to a Windows fileshare via the pre-existing FTP method. This was previously executed manually. The trouble I ran into was passing the FTP commands from a UNIX shell script to the Windows FTP server (specifically, user credentials) correctly. Second, I also wanted to automate the process of archiving these files to a new directory after transfer. Here's what I came up with, variables requiring your modification in bold:
################################################################################
#************************* Script Title ******************************
#
# Script Name: FTP PUT TO WINDOWS FTP SERVER
# Script Filename: FTP_PUT_TO_WINFTPSERVER.sh
#
#
#********************* Script Description ************************************
#
# This shell script will look for files matching 'FTP_*.dat' and FTP any
# found to a Windows FTP server, then archive them in a folder named
# the current date.
#
#************************* Modification Log *******************************
#
# Date Name Description
#_____________________________________________________________________________
#
# 10/19/2011 Bryce Baker Original script
#
#*******************************************************************************
################################################################################

#!bin/sh
#***** Set local working directory *****
cd /path/to/script/and/files

#***** Set ftp server, credentials, and file(s) to search for *****
HOST='SERVERADDRESS'
USER='FTPUSERNAME'
PASSWD='FTPPASSWORD'
FILE='FTP_*.dat'

#***** Send files to FTP server *****
ftp -vn $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
prompt n
mput $FILE
quit
END_SCRIPT

#***** Archive files *****
#***** Set DATE variable to current date *****
DATE='20'`date '+%y%m%d'`
mkdir $DATE
mv $FILE /path/to/archive/folder/$DATE/

exit 0

Comments

  1. what is the destination location of the transferred file? how can we specify it in this code?

    ReplyDelete
  2. The destination is the root of the FTP directory on the destination server. You can change the directory by doing a change directory (cd) before the mput command:

    cd /path/to/file/

    ReplyDelete

Post a Comment

Popular posts from this blog

Continuously Monitor Folder for New Files with PowerShell as a WindowsService

Unprotect All Excel Sheets for All Workbooks in a Directory

ChromeOS Flow by Hexxeh Install to HDD