1

I'm developing a website and I have FTP access for uploading local files to a directory on the site via FTP. I'd like to automate this deployment process as much as possible using the command line.

Is there a Windows command line utility that I can use to help me further automate my process of synchronizing a local directory with a remote directory via FTP with this task?

sduplooy
  • 127

3 Answers3

4

Automate FTP Folder Synchronization with WinSCP

I've successfully completed such tasks using the awesome and ever so cool WinSCP product. I've provided you a sample Batch Script as well to help setup such a task for automation and thus providing you a rather full and complete answer to help satisfy your needs.

Essentially you will:

  1. Define the FTP connection within the WinSCP GUI
  2. Save the defined FTP connection and give it a name that indicates what it pertains
  3. Use the FTP connection name in the script to open the connection for the FTP commands—see the Batch Script section

    enter image description here
    enter image description here
    enter image description here


Batch Script

In the below batch script you will want to be sure these variables are set with the correct values: localdir, remotedir, winscplogin, and logfile so be sure those are set to point to the correct folders, WinSCP defined FTP connection, and log file—the rest should just work as-is.

@ECHO ON

:SetFileLogVariables
SET localdir=C:\dev\site123
SET remotedir="dev\site123"
SET logfile=C:\logs\FTP_dev_site123_sync.log

:SetPrgVariables
SET prgwinscp="C:\Program Files\WinSCP3\WinSCP.com"
SET winscplogin="DevSiteSync"
SET winscpfile=%temp%\~tmpWinSCPFTPSyncT_%~N0.txt
IF EXIST "%winscpfile%" DEL /Q /F "%winscpfile%"

:SetWinSCPSyncCommand
SET ftpcmd=synchronize remote "%localdir%\"

:ftpout
ECHO.                                                                   >> %logfile%
ECHO ***************************  FTP OUT  ***************************  >> %logfile%
ECHO Synchronizing files to %winscplogin% server  on %date% at %time%   >> %logfile%

ECHO option batch on          >> %winscpfile%
ECHO option confirm off       >> %winscpfile%
ECHO option transfer binary   >> %winscpfile%
ECHO open %winscplogin%       >> %winscpfile%
ECHO cd %remotedir%           >> %winscpfile%
ECHO %ftpcmd%                 >> %winscpfile%
ECHO close                    >> %winscpfile%
ECHO exit                     >> %winscpfile%

ECHO %winscpfile%                                >> %logfile%
TYPE %winscpfile%                                >> %logfile%
ECHO ------------------------------------------- >> %logfile%
%prgwinscp% /script=%winscpfile%                 >> %logfile%
ECHO ------------------------------------------- >> %logfile%
IF EXIST "%winscpfile%" DEL /Q /F "%winscpfile%"
ECHO Transmission complete on %date% at %time%   >> %logfile%
EXIT

WinSCP Connection Configuration Note

You will probably want to tell the WinSCP defined FTP connection to NOT Remember the last directory used since the script will go to the folder explicitly.

You complete this by highlighting the defined FTP connection name within the WinSCP GUI and then select Edit | Advanced | Directories | uncheck the box next to Remember the last directory used | OK | Save.

enter image description here

enter image description here

enter image description here


synchronize

Syntax

synchronize local|remote|both [ <local directory> [ <remote directory> ] ]

Remarks

When the first parameter is local, changes from remote directory are applied to local directory. When the first parameter is remote, changes from the local directory are applied to the remote directory. When the first parameter is both, both local and remote directories can be modified.

When directories are not specified, current working directories are synchronized.

Note: Overwrite confirmations are always off for the command.

Switches:

enter image description here

source


Further Resources

1

I have used successfully the venerable and free NcFTP Client.

Example usage :

ncftpput -u user -p password ftp.server.com /server-folder file-path
harrymc
  • 498,455
1

A simple bash ftp script can transfer over a file:

#!/bin/sh
HOST='hostname'
USER='username'
PASSWD='password'
FILE='test.txt'

ftp $HOST <<END_SCRIPT
USER $USER
PASS $PASSWD
passive
ls
cd test
put $FILE
quit
END_SCRIPT
exit 0

Easier than writing you own script to loop over files and folders, use lsftp:

#!/bin/bash
HOST='hostname'
USER='username'
PASS='password'
TARGETFOLDER='/test'
SOURCEFOLDER='/mnt/c/test'

lftp -f "
set ssl:verify-certificate no
open $HOST
user $USER $PASS
mirror --reverse --delete --verbose $SOURCEFOLDER $TARGETFOLDER
bye
"
simlev
  • 3,912