Robocopy - Overwrite all files unless file in destination is newer
The title pretty much sums it up.
I had restored a file share recently from a backup. Come to find out after about a day of users in production we started to notice some files were corrupt. Pulling those files from the backup again shows that the backups are in fact intact. However, users are modifying files going forward.
Using robocopy, can I overwrite all files in this share skipping newer files on the destination? In other words, I basically want to recopy everything unless the user has made changes to a file on the share.
The /is flag seems intriguing, but unsure if it will overwrite a file with a newer timestamp.
And insight would be greatly appreciated.
https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy
/xo Excludes older files.
That’s what you need.
Utilize:
-
/MON:1
: MONitor source; run again when more thann
changes seen -
/XN
: eXclude Newer files
robocopy D:\Source\Sync \\ShareName\Sync /TBD /E /XN /MON:1 /LOG+:"\\ShareName\Sync\Logs\Sync.log" /V /TEE /TS /XD Logs
The following script will monitor the source directory for any changes and log whenever a file is copied to the destination directory.
Script: File-Sync.cmd
::
::[[--- RoboCopy File Sync Script ---]]::
::=========================================================================
@echo off
:: Parameters ::
::-------------------------------------------------------------------------
:: Set Variables ::
:: Date:
set Date=%date:~0,4%%date:~5,2%%date:~8,2%
:: Time:
:: Hour:
set Hour=%time:~0,2%
if "%hour:~0,1%" == " " set HH=0%hour:~1,1%
:: Minute:
set MM=%time:~3,2%
:: Seconds
set SS=%time:~6,2%
set Dtime=%HH%%mm%%ss%
:: Destination:
set Dest=\\ShareName\Sync
:: Log:
set LogDir=\\Sharename\Sync\Logs
:: Source:
set Src=D:\Source\Sync
:: RoboCopy:
:: Log:
set Log=/V /TEE /TS /LOG:"%LogDir%\%date%_%dtime%.log"
:: Options:
set Options=/TBD
:: What to Copy:
set What=/COPY:DAT /DCOPY:DAT /E /XD Logs /XN /MON:1
:: Save Job:
set Save=/SAVE:C:\ProgramData\Scripts\Robocopy\File-Sync
:: Sync ::
::-------------------------------------------------------------------------
RoboCopy %Src% %Dest% %What% %Options% %Log% %Save%
-
Date & Time: (Locale dependent)
-
%date%
: Outputs a date in the following format:yyyy.MM.dd
-
%dtime%
: Outputs time in the following format:HHmmss
-
-
Directories:
-
%Dest%
: Destination Directory -
%LogDir%
: Log Directory -
%Src%
: Source Directory
-
-
RoboCopy:
-
%log%
: Logging Options, outputs a log file in the following formatYYYY.MM.DD_HHMMSS.log
-
/V
: Produce Verbose output, showing skipped files -
/TEE
: Output to console window, as well as the log file- Helpful if needing to run from a terminal occassionally
-
/TS
: Include source file Time Stamps in the output -
/LOG:
: Output status to LOG file (overwrite existing log)-
Will never overwrite, provided
%dtime%
is included
-
Will never overwrite, provided
-
-
%options%
: RoboCopy options-
/TBD
: wait for share names To Be Defined
-
-
%what%
: What to copy-
/COPY:DAT
: what to COPY for files (default is/COPY:DAT
)- Copy Flags: D=Data, A=Attributes, T=Timestamps
-
/DCOPY:DAT
: Same as above, except for directories -
/E
: copy subdirectories, including Empty ones -
/XD
: EXclude Directories matching given names/paths- Required if Log Directory is stored in the Source directory
-
/XN
: eXclude Newer files -
/MON:1
: MONitor source; run again when more than n changes seen
-
-
/SAVE
: SAVE parameters to the named job file- Outputs job file to:
C:\ProgramData\Scripts\Robocopy\File-Sync.rcj
- Outputs job file to:
-
Run Script Silently: NoShell-RoboCopy.vbs
'
'::[[--- Run RoboCopy File Sync Silently ---]]::'
'=========================================================================
If WScript.Arguments.Count >= 1 Then
ReDim arr(WScript.Arguments.Count-1)
For i = 0 To WScript.Arguments.Count-1
Arg = WScript.Arguments(i)
If InStr(Arg, " ") > 0 Then Arg = """" & Arg & """"
arr(i) = Arg
Next
RunCmd = Join(arr)
CreateObject("Wscript.Shell").Run RunCmd, 0, True
End If
-
Execute:
wscript "C:\ProgramData\Scripts\Robocopy\NoShell-RoboCopy.vbs" "C:\ProgramData\Scripts\Robocopy\File-Sync.cmd"
Task Schedule: File-Sync.xml
(runs 30s after user login)
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2018-10-25T07:45:58.952588</Date>
<Author>Windows\User</Author>
<Description>Task to synchronize local disk to network backup</Description>
<URI>\Backup\Network File Sync</URI>
</RegistrationInfo>
<Triggers>
<LogonTrigger>
<Enabled>true</Enabled>
<Delay>PT30S</Delay>
</LogonTrigger>
</Triggers>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>false</AllowHardTerminate>
<StartWhenAvailable>true</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
<UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
<Priority>7</Priority>
<RestartOnFailure>
<Interval>PT1M</Interval>
<Count>3</Count>
</RestartOnFailure>
</Settings>
<Actions Context="Author">
<Exec>
<Command>%WinDir%\system32\wscript.exe</Command>
<Arguments>"C:\ProgramData\Scripts\Robocopy\NoShell-RoboCopy.vbs" "C:\ProgramData\Scripts\Robocopy\File-Sync.cmd"</Arguments>
</Exec>
</Actions>
</Task>