:: Created by Daniel da.net@auroville.org.in
:: Install FreeFileSync in default C:\Program Files\FreeFileSync folder
:: Create folder C:\backup_config and copy sample configuration files to it
:: Configure parameters below
@ECHO OFF
:: Path to the destination backup folder either network or local.
:: In case of a network place use format \\SERVER\Folder in case of a local foler use DRIVE_LETTER:\FOLDER
:: Don't put backslash at the end of the line
SET Destination=\\192.168.28.2\FAMC-GREEN
:: Server credentials - in case of network location.
:: If using local folder or public share that doesn't require credentials, keep both fields blank.
SET User=famcbackup
SET Password=htHL678kh
:: Path to FFS profile file
SET FFSProfile=BatchRun.ffs_batch
:: Changed and deleted files retention period
SET retention=30
:: Global report file located in public network location
SET GlobalReportFile="\\192.168.28.2\TH Share\_backup_global_report\backup_global_report.csv"
:: Set folders for backup. Spaces are allowed. No need quote marks. Every location must start with drive letter, colon, backslash (C:\... D:\...).
:: Keep empty or delete lines that are not needed. If needed more location, add more lines using the same format.
SET source[1]=C:\Users\FAMC\Desktop
SET source[2]=
SET source[3]=
SET source[4]=
SET source[5]=
SET source[6]=
SET source[7]=
SET source[8]=
SET source[9]=
SET source[10]=
:: Send email report as:
SET GmailUser=da.info@auroville.org.in
SET GmailPassword="auroville 2018 AV"
SET SendEmailTo=da.net@auroville.org.in
::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::: DON'T EDIT BELOW THIS LINE ::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Creating TIMESTAMP in good format
FOR /f "tokens=2 delims==" %%a IN ('wmic OS Get localdatetime /value') DO SET "dt=%%a"
SET "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
SET "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
SET "timestamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
:: Checking if backup was running today already. If yes - go to END (do nothing)
IF EXIST batch_logs\batch_log_%YYYY%-%MM%-%DD%* GOTO END
:: Creating batch log file
IF NOT EXIST "batch_logs" MKDIR "batch_logs"
SET BatchLogFile=batch_logs\batch_log_%timestamp%.txt
ECHO This is backup log created on %timestamp% > %BatchLogFile%
:: Creating FFS log file
IF NOT EXIST "ffs_logs" MKDIR "ffs_logs"
SET FFSLogFile=ffs_logs\ffs_log_%timestamp%.txt
ECHO. > %FFSLogFile%
:: Preparing message for user
ECHO Dear user! > message.txt
ECHO. >> message.txt
ECHO We backup only following folders on this computer >> message.txt
ECHO. >> message.txt
:: Mounting network volume or skipping this if no need to login
IF "%User%"=="" GOTO NOLOGIN
net use %Destination% /user:%User% %Password%
IF ERRORLEVEL 1 (
ECHO ERROR Mounting network volume %Destination% >> %BatchLogFile%
) ELSE (
ECHO OK Mounting network volume %Destination% >> %BatchLogFile%
)
:NOLOGIN
:: Running cycles that will sync pairs of directories using same FFS profile for each pair separately
SET i=1
SET ErrLevel=0
:LOOP
SETLOCAL EnableDelayedExpansion
:: Defining Source and Target folders for current cycle run
IF "!source[%i%]!"=="" GOTO ENDLOOP
SET Source=!source[%i%]!
SET Target=!source[%i%]:~0,1!!source[%i%]:~2,150!
:: Running backup for current couple
"C:\Program Files\FreeFileSync\FreeFileSync.exe" %FFSProfile% -dirpair "%Source%" "%Destination%\%Target%"
IF ERRORLEVEL 1 (
ECHO ERROR %Source% %Destination%\%Target% >> %BatchLogFile%
SET /A ErrLevel+=1
) ELSE (ECHO OK %Source% %Destination%\%Target% >> %BatchLogFile%)
:: Identifying name of FFS log file that was just created
SETLOCAL EnableExtensions DisableDelayedExpansion
SET "LastLog="
FOR /f "delims=" %%a IN ('DIR /b /o-d ffs_logs\ 2^>NUL') DO (
IF NOT DEFINED LastLog SET "LastLog=%%a"
)
:: Streaming last FFS report into common report file
TYPE "ffs_logs\%LastLog%" >> %FFSLogFile%
ECHO.>>%FFSLogFile%
DEL "ffs_logs\%LastLog%"
:: Adding source folder to user message that will be displayed at the end
ECHO %Source% >> message.txt
SET /A i+=1
GOTO LOOP
:ENDLOOP
:: Deleting files older than %retention% from backup _modified folder
PUSHD "%Destination%\_modified" &&(
forfiles -s -m *.* -d -%retention% -c "cmd /c del /s /q @path"
) & POPD
IF ERRORLEVEL 1 (
ECHO WARNING Deleting files older than %retention% days from %Destination%\_modified. Nothing to delete >> %BatchLogFile%
) ELSE (ECHO OK Deleted files older than %retention% days from %Destination%\_modified. >> %BatchLogFile%)
:: Deleting log file older than %retention% from batch_logs and ffs_logs
forfiles /p "batch_logs" /s /m *.* /d -%retention% /c "cmd /c del @path"
IF ERRORLEVEL 1 (
ECHO WARNING Deleting batch log files older than %retention% days. Nothing to delete >> %BatchLogFile%
) ELSE (ECHO OK Deleted batch log files older than %retention% days. >> %BatchLogFile%)
forfiles /p "ffs_logs" /s /m *.* /d -%retention% /c "cmd /c del @path"
IF ERRORLEVEL 1 (
ECHO WARNING Deleting FFS log files older than %retention% days. Nothing to delete >> %BatchLogFile%
) ELSE (ECHO OK Deleted FFS log files older than %retention% days. >> %BatchLogFile%)
:: Checking for errors
IF NOT %ErrLevel%==0 GOTO FFSERROR
:: Sending email Backup completed successful
SwithMail.exe /s /from %GmailUser% /name "backup" /pass %GmailPassword% /server "smtp.gmail.com" /p "587" /SSL /to %SendEmailTo% /sub "%COMPUTERNAME% backup OK" /b "Backup completed successful"
GOTO FFSEXIT
:FFSERROR
:: Sending email Backup completed with ERROR with %FFSLogFile% %BatchLogFile% in attachment
SwithMail.exe /s /from %GmailUser% /name "backup" /pass %GmailPassword% /server "smtp.gmail.com" /p "587" /SSL /to %SendEmailTo% /sub "%COMPUTERNAME% backup ERROR" /b "Backup completed with errors. See log attached." /a "%FFSLogFile%|%BatchLogFile%"
:FFSEXIT
::Writing to global report CSV file (experimental)
ECHO %YYYY%-%MM%-%DD%,%HH%:%Min%:%Sec%,%COMPUTERNAME%,%ErrLevel% >> %GlobalReportFile%
IF "%User%"=="" GOTO NOLOGOUT
:: Restartning Workstation service to release network share
net stop lanmanworkstation /y
net start lanmanworkstation /y
:NOLOGOUT
::Displaing message to user
ECHO. >> message.txt
ECHO If you want to add more backup locations, please talk to Admin >> message.txt
msg * < message.txt
:END |