# Only show errors, transfered files and print messages SILENT(ON) # Passive mode utilized because I am behind a firewall SETPASSIVE(ENABLED) # Retrieve the FTP server information from the command line ftp_customername=GETPARAM(3) ftp_server=GETPARAM(4) ftp_user=GETPARAM(5) ftp_password=GETPARAM(6) ftp_remotepath=GETPARAM(7) ftp_ignoredirectory=GETPARAM(8) PRINT ("Running script...") # ****************************************************** # This script uses the free command-line emailer Blat # to report reults via email. # http://www.blat.net. #******************************************************* # Blat parameters blat_path="C:\windows\system32\blat.exe" smtp_server="smtp.isp.com" email_from="scriptftp@yourdomain.com" email_to="webmaster@yourdomain.com" email_subject1=CONCAT("*ERROR in ScriptFTP for ",ftp_customername) email_subject2=CONCAT("Success for ",ftp_customername," ScriptFTP") #subject 3 defined further with error message below #email_subject3=CONCAT("**ERROR, retrying ScriptFTP for ",ftp_customername) email_body1="Could not connect to FTP server." email_body2="Could not synchronize files." email_body3="Directory didn't exist, created new one." email_body4=CONCAT(ftp_customername," FTP Successful! ") email_body5="Parameter missing." email_body6="Retrying synchronization..." #Get Date YYYYMMDD current_date=GETDATE(FORMAT1) #Set Log File Path LogFile=CONCAT("C:\Windows\temp\",ftp_customername,"-",current_date,".txt") # Log transfer on a text file LOGTO(LogFile) # Construct the blat command line. # Note that the following lines are very long and # your editor may have cutted it. A ScriptFTP # command must be contained in only one line. # # Smtp authentication disbled in this example. # Send log as an attachment blat_cmd_line1=CONCAT(blat_path," -server ",smtp_server," -f ",email_from,"-to ",email_to," -subject ",QUOTE,email_subject1,QUOTE," -body",QUOTE,email_body1,QUOTE," -ps ",QUOTE,LogFile,QUOTE) blat_cmd_line2=CONCAT(blat_path," -server ",smtp_server," -f ",email_from,"-to ",email_to," -subject ",QUOTE,email_subject1,QUOTE," -body",QUOTE,email_body2,QUOTE," -ps ",QUOTE,LogFile,QUOTE) blat_cmd_line3=CONCAT(blat_path," -server ",smtp_server," -f ",email_from,"-to ",email_to," -subject ",QUOTE,email_subject1,QUOTE," -body",QUOTE,email_body3,QUOTE," -ps ",QUOTE,LogFile,QUOTE) blat_cmd_line4=CONCAT(blat_path," -server ",smtp_server," -f ",email_from,"-to ",email_to," -subject ",QUOTE,email_subject2,QUOTE," -body",QUOTE,email_body4,QUOTE," -ps ",QUOTE,LogFile,QUOTE) blat_cmd_line5=CONCAT(blat_path," -server ",smtp_server," -f ",email_from,"-to ",email_to," -subject ",QUOTE,email_subject1,QUOTE," -body",QUOTE,email_body5,QUOTE," -ps ",QUOTE,LogFile,QUOTE) blat_cmd_line6=CONCAT(blat_path," -server ",smtp_server," -f ",email_from,"-to ",email_to," -subject ",QUOTE,email_subject3,QUOTE," -body",QUOTE,email_body6,QUOTE," -ps ") #test Parameters IF(ISEQUAL(ftp_customername,"")) PRINT("Missing Parameter #1, customername, exiting.") GOTO failed_parameter END IF IF(ISEQUAL(ftp_server,"")) PRINT("Missing Parameter #2, server, exiting.") GOTO failed_parameter END IF IF(ISEQUAL(ftp_user,"")) PRINT("Missing Parameter #3, user, exiting.") GOTO failed_parameter END IF IF(ISEQUAL(ftp_password,"")) PRINT("Missing Parameter #4, password, exiting.") GOTO failed_parameter END IF LocalWebFolder = CONCAT("D:\Backups\Websites\",ftp_customername) # Test if directory exists, if not create it result_directory=LOCALCHDIR(LocalWebFolder) IF(NOT(ISEQUAL(result_directory,"OK"))) PRINT("Creating folder.") result_makedirectory = LOCALMKDIR(LocalWebFolder) IF(NOT(ISEQUAL(result_makedirectory,"OK"))) PRINT("Directory Creation Failed, exiting.") EXEC(blat_cmd_line3) GOTO failed_directory END IF END IF LOCALCHDIR(LocalWebFolder) :sync_reconnect # Connect to FTP server # Try to connect three times. attempts="1" :connect PRINT("_____Connecting_____") result_ftp=OPENHOST(ftp_server,ftp_user,ftp_password) IF(NOT(ISEQUAL(result_ftp,"OK"))) PRINT(CONCAT("Cannot connect! Attempt #",attempts,".")) IF(ISEQUAL(attempts,"3")) EXEC(blat_cmd_line1) GOTO failed_connection ELSE attempts=ADD(attempts,"1") SLEEP("3") GOTO connect END IF END IF # if ftp_ignoredirectory exists # then do not attempt download of this IF(NOT(ISEQUAL(ftp_ignoredirectory,""))) ADDEXCLUSION(DOWNLOAD,ftp_ignoredirectory) PRINT(CONCAT("IGNORING Directory ",ftp_ignoredirectory)) END IF # syncronize from remote files result_sync=SYNC(LocalWebFolder,ftp_remotepath,DOWNLOAD,SUBDIRS) #426 is Connection closed; transfer aborted. IF(ISEQUAL(result_sync,"426")) PRINT("Error sending/receiving files.") PRINT(CONCAT("result_sync reports is >",result_sync,"<.")) GOTO retry_sync END IF #425 is Can't open data connection, usually because of a timeout IF (ISEQUAL(result_sync,"425")) PRINT("Error sending/receiving files.") PRINT(CONCAT("result_sync reports is >",result_sync,"<.")) GOTO retry_sync END IF #3 is Not connected and not logged in to remote server IF (ISEQUAL(result_sync,"3")) PRINT("Error sending/receiving files-- Retrying!") PRINT(CONCAT("result_sync reports is >",result_sync,"<.")) GOTO retry_sync END IF #4 is Connection Timeout IF (ISEQUAL(result_sync,"4")) PRINT("Error sending/receiving files-- Retrying!") PRINT(CONCAT("result_sync reports is >",result_sync,"<.")) GOTO retry_sync END IF IF(NOT(ISEQUAL(result_sync,"OK"))) PRINT("Error sending/receiving files-- Retrying!") PRINT(CONCAT("result_sync reports is >",result_sync,"<.")) EXEC(blat_cmd_line2) GOTO failed_put END IF PRINT("Files downloaded succesfully.") EXEC(blat_cmd_line4) CLOSEHOST() #SLEEP("60") EXIT(99) :retry_sync PRINT("Retrying Synchronization...") email_subject3=CONCAT("**ERROR #",result_sync,", retrying ScriptFTP for ",ftp_customername) EXEC(blat_cmd_line6) SLEEP("30") CLOSEHOST() GOTO sync_reconnect :failed_put PRINT("An Error was found while synchronizing the files.") CLOSEHOST() #SLEEP("60") EXIT(1) :failed_connection PRINT("Cannot connect to remote host.") #SLEEP("60") EXIT(2) :failed_directory PRINT("An Error was found while creating the folder.") CLOSEHOST() #SLEEP("60") EXIT(3) :failed_parameter PRINT("An Error was found, missing a parameter.") EXEC(blat_cmd_line5) CLOSEHOST() SLEEP("30") EXIT(4)