Introduction

Backup is performed with the means of FreeFileSync software that is operated from within the batch file. Batch file contains all parameters needed for the backup operation.  It can backup to a network or local location. Current procedure accomplishes incremental backup with a retention period indicated in configuration file.

Setup generates reports that being sent over email as attachments in case of backup operation completed with errors. Otherwise, if no errors registered, "OK" email is sent. In addition there is CSV file in public location 192.168.28.2/srv/th_share/_backup_global_report/backup_global_report.csv that can help to get overview of backup situation (this is experimental feature yet)

Installation

We suppose that destination backup folder (Samba share) is created and credentials are known.

The following files are required for setup:

On host machine:

      0. In case of installation on Windows 10 machine that will communicate to Linux Samba share: check if you can login to the destination backup folder with Samba credentials using File Explorer. In some cases Windows OS cannot see Samba share if it is running outdated protocol and in some other cases that not yet explored enough (displayed error: "Windows cannot access \\IP_ADDRESS. Check the spelling of the name... etc.)". Solution: Run command prompt with administrator rights and execute two following commands. Then reboot and try again.

sc.exe config lanmanworkstation depend= bowser/mrxsmb10/nsi 
sc.exe config mrxsmb20 start= disabled
  1. Install FreeFileSync to default folder C:\Program Files\FreeFileSync
  2. Create new folder C:\backup_config
  3. Copy to this folder following files:
    1. backup.bat
    2. SwithMail.exe
    3. BatchRun.ffs_batch
  4. Open in a text editor backup.bat file and edit parameters in top part of the file:
  5. Open Task Scheduler (press Win+R combination and run taskschd.msc)
  6. Import the file backup-on-idle.xml to create new task
  7. Test setup by running the task on demand (right click and run). Check  folders C:\backup_config\batch_logs and C:\backup_config\ffs_logs and read reports.

Appendix 1

:: 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
<?xml version="1.0" encoding="utf-8"?>
<FreeFileSync XmlType="BATCH" XmlFormat="11">
    <Compare>
        <Variant>TimeAndSize</Variant>
        <Symlinks>Exclude</Symlinks>
        <IgnoreTimeShift/>
    </Compare>
    <Synchronize>
        <Variant>Mirror</Variant>
        <DetectMovedFiles>false</DetectMovedFiles>
        <DeletionPolicy>Versioning</DeletionPolicy>
        <VersioningFolder Style="TimeStamp">%Destination%\_modified\%Target%</VersioningFolder>
    </Synchronize>
    <Filter>
        <Include>
            <Item>*</Item>
        </Include>
        <Exclude>
            <Item>\System Volume Information\</Item>
            <Item>\$Recycle.Bin\</Item>
            <Item>\RECYCLER\</Item>
            <Item>\RECYCLED\</Item>
            <Item>*\desktop.ini</Item>
            <Item>*\thumbs.db</Item>
        </Exclude>
        <TimeSpan Type="None">0</TimeSpan>
        <SizeMin Unit="None">0</SizeMin>
        <SizeMax Unit="None">0</SizeMax>
    </Filter>
    <FolderPairs>
        <Pair>
            <Left/>
            <Right/>
        </Pair>
    </FolderPairs>
    <Errors Ignore="true" Retry="0" Delay="5"/>
    <PostSyncCommand Condition="Completion"/>
    <Batch>
        <ProgressDialog Minimized="true" AutoClose="true"/>
        <ErrorDialog>Show</ErrorDialog>
        <PostSyncAction>None</PostSyncAction>
        <LogfileFolder Limit="-1">C:\backup_config\ffs_logs</LogfileFolder>
    </Batch>
</FreeFileSync>

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2018-06-25T17:09:09.0960454</Date>
    <Author>GREEN\Admin</Author>
    <URI>\backup</URI>
  </RegistrationInfo>
  <Triggers>
    <IdleTrigger>
      <Enabled>true</Enabled>
    </IdleTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>S-1-5-18</UserId>
      <RunLevel>HighestAvailable</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <Duration>PT5M</Duration>
      <WaitTimeout>PT1H</WaitTimeout>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>true</RunOnlyIfIdle>
    <DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
    <UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>C:\backup_config\backup.bat</Command>
      <WorkingDirectory>C:\backup_config</WorkingDirectory>
    </Exec>
  </Actions>
</Task>