File Synchronization/Backup using robocopy and WinSCP

If the server side is Windows, we can use Robocopy to sync file between server and local

If the server side is Linux, it is better use FTP which is more CPU efficient than smbd than FTP on most Linux platform.


Robocopy version

The below Windows batch file is to backup Windows SMB server files to local HDD.  Some parameters are explained below:

  1. /mir - Mirror option is used, so file deleted in server will be deleted in local
  2. /np - no progress bar
  3. /v - verbose output
  4. /R:5 - retry 5 times
  5. /W:15 - wait time between retries in seconds
  6. /IPG:9 - interpacket gap, to slow down the job to reserve bandwidth for other applications
  7. /XD - exclude directory
  8. /LOG+ - log in append mode
  9. /zb - copy in restartable mode, need batch job access right.


REM https://www.deviousweb.com/2018/01/09/robocopy-throttle-bandwidth/
REM IPG - Inter-packet GAPl; np - no progress; v - verbose; R - retry; W - Wait between retry
REM https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy
REM https://www.tutorialspoint.com/batch_script/batch_script_functions_with_return_values.htm

call :run_robocopy software
call :run_robocopy docs1
call :run_robocopy photo
call :run_robocopy web

EXIT /B %ERRORLEVEL%

:run_robocopy
set src=\\ftpserver\%~1
set dest=D:\Users\username\server_backup\%~1
md %dest%
cd %dest%
net use s: /delete
net use s: %src%
robocopy s: %dest% /mir /np /v /R:5 /W:15 /IPG:9 /XD "#recycle" /LOG+:"d:\users\username\server_backup\backup.log"
REM  /zb 
exit /b 0

WinSCP version

  1. local - The command will sync files from Remote to Local.  Only changes from Remote remote will reflect to local
  2. -delete: if the file is deleted on server, it will be deleted in local.  If the file is renamed in remote, it will be delete local file plus download the file with new filename. 
  3. -filemask: All files (*) will be sync except files inside folder "#recycle" and *.tmp.
  4. -log: The log file will include YYYYMMDDHHMM in the filename.

TITLE NAS Backup (WinSCP)
@echo off
set basefolder=D:\Users\username\server_backup
"c:\program files (x86)\winscp\winscp.com" ^
  /log="%basefolder%\winscp_!Y!M!D!T.log" /loglevel=0 /ini=nul ^
  /command ^
    "open ftp://ftpuser:ftppassword@ftpserver/" ^
    "synchronize local -delete %basefolder%\docs1 /docs1 -filemask=*|#recycle/;*.tmp" ^
    "synchronize local -delete %basefolder%\photo /photo -filemask=*|#recycle/;*.tmp" ^
    "synchronize local -delete %basefolder%\software /software -filemask=*|#recycle/;*.tmp" ^
    "synchronize local -delete %basefolder%\web /web -filemask=*|#recycle/;*.tmp" ^
    "exit"
set WINSCP_RESULT=%ERRORLEVEL%
if %WINSCP_RESULT% equ 0 (
  echo Success
) else (
  echo Error
)
exit /b %WINSCP_RESULT%






Comments

Popular Posts