7

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.

8

https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy

/xo Excludes older files.

That’s what you need.

1
  • How would excluding older files prevent a new file in the destination from being overwritten? Wouldn't /XO result in the opposite of what's being requested, resulting in new files being overwritten? Shouldn't /XN be utilized, either with or without /MON? – JW0914 Oct 28 '18 at 16:43
1

Utilize:

  • /MON:1: MONitor source; run again when more than n 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 format YYYY.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
    • %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


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>
3
  • If down voting an answer, please provide the reason why, else the answer can't have whatever is wrong corrected. – JW0914 Nov 10 '18 at 18:28
  • An issue might be that the output of %date% and %time% is locale-dependent. Very annoying from my perspective as it easily breaks scripts. – Paul B. May 8 '19 at 8:40
  • @PaulB. I wasn't aware of that, thanks for letting me know =] Is there a solution to this, or should I simply remove those variables from the script? – JW0914 May 8 '19 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.