Automatize a sincronização de pastas FTP com o WinSCP
Eu concluí essas tarefas com sucesso usando o incrível e sempre tão legal produto WinSCP . Eu forneci a você um exemplo Batch Script também para ajudar a configurar essa tarefa para automação e, assim, fornecer uma resposta completa e completa para ajudar a satisfazer suas necessidades.
Essentially you will:
Script em lote
No script em lote abaixo, você desejará ter certeza de que essas variáveis estão definidas com os valores corretos: localdir
, remotedir
, winscplogin
e logfile
, portanto, certifique-se de que eles estejam definidos para apontar para o correto pastas, o WinSCP definiu a conexão FTP e o arquivo de log - o restante deve funcionar apenas como está.
@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.
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 isremote
, changes from the local directory are applied to the remote directory. When the first parameter isboth
, 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:
source