user11500819
user11500819

Reputation:

Robocopy Maxage Minage with Mir Parameter

I am using Robocopy from Source to Destination.

The following will move all the files.

Step 1: robocopy c:\Source C:\Destination /MIR

Now when I execute second command in step, anything before January 1, 2019 should be removed from destination with /MIR parameter, however this is not occuring (files after Jan 2019, still remain in folder instead of being deleted with /MIR command). How would I resolve this issue? Remove anything before 20190101?

Step 2: robocopy c:\Source C:\Destination /MIR /maxage:19000101 /minage:20190101" 

Upvotes: 2

Views: 17133

Answers (1)

HAL9256
HAL9256

Reputation: 13483

This does seem odd behavior as one would think that what you are trying to use the /MINAGE switch to select everything younger than January 1, 2019. Then combine it with the /MIR switch, or more specifically the /PURGE /E switch, to remove the extra files and directories that are after January 1, 2019.

The reason that this doesn't work is that the /MINAGE switch only affects the files that are selected to be copied, even when the /MIR command works as expected.

Say we have 2 files 1 older than January 1, 2019, and one newer.

C:\Source         C:\Destination
 File-2018.txt     
 File-2019.txt     

When we run the standard \MIR command:

Robocopy.exe C:\Source C:\Destination /MIR 

Both files get copied as expected.

2 files Copied

C:\Source         C:\Destination
 File-2018.txt      File-2018.txt
 File-2019.txt      File-2019.txt

If we add an extra file to the destination:

C:\Source         C:\Destination
 File-2018.txt      File-2018.txt
 File-2019.txt      File-2019.txt
                    File-2020.txt

And re-run the /MIR command:

Robocopy.exe C:\Source C:\Destination /MIR 

We get, as expected, the extra file is deleted:

2 files Skipped
1 files Extras

C:\Source         C:\Destination
 File-2018.txt      File-2018.txt
 File-2019.txt      File-2019.txt

To the \MIR command, when we then add a /MINAGE parameter thinking that we are asking for everything Jan 1 2019 and older to stay and the rest to go:

Robocopy.exe C:\Source C:\Destination /MIR /MINAGE:20190101

We get the unexpected:

2 files Skipped

C:\Source         C:\Destination
 File-2018.txt      File-2018.txt
 File-2019.txt      File-2019.txt

To better illustrate what is going on, and why it "Skipped" the files, lets add another parameter, Include Skipped \IS:

Robocopy.exe C:\Source C:\Destination /MIR /IS /MINAGE:20190101

We get:

1 files Copied
1 files Skipped

C:\Source         C:\Destination
 File-2018.txt      File-2018.txt
 File-2019.txt      File-2019.txt

That means the \MINAGE parameter only filtered out what we wanted to copy. If we reset and clear out the directory, and re-run it, we can better see what happened:

Remove-Item C:\Destination -Force

C:\Source         C:\Destination
 File-2018.txt      
 File-2019.txt      

Robocopy.exe C:\Source C:\Destination /MIR /MINAGE:20190101

We get:

1 files Copied
1 files Skipped

C:\Source         C:\Destination
 File-2018.txt      File-2018.txt
 File-2019.txt      

It copied over the Older file, and not the Newer file. This means that when it iterated through files in the source, it used the /MINAGE parameter to figure out only what files I need to copy over, not what files I need to Remove.

The reason for this behavior is because /MINAGE and the corresponding /MAXAGE parameters is meant for very large directories with slow network links or smaller destination computers, where we typically are only interested in copying over certain sets of files (e.g. only new files from yesterday), and don't necessarily need to copy over all the files. Most often in these cases, the mirroring is not usually performed or desired as the set of files are typically "Deltas".

To highlight copy over remove, if mirror the directories and then we add 2 extra files, one older and one newer to the Destination:

C:\Source         C:\Destination
                    File-2017.txt
 File-2018.txt      File-2018.txt
 File-2019.txt      File-2019.txt
                    File-2020.txt

And re-run with the Include Skipped /IS so that we can see what is being copied:

Robocopy.exe C:\Source C:\Destination /MIR /IS /MINAGE:20190101

We get the somewhat surprising:

1 files Copied
1 files Skipped
2 files Extras

C:\Source         C:\Destination

 File-2018.txt      File-2018.txt
 File-2019.txt      File-2019.txt

This highlights that /MIR is meant for... well... Mirroring. The other parameters are there only to tell it what to Copy.

We start with Source files involved in the copy:

C:\Source
 File-2018.txt
 File-2019.txt

We apply the filtering by Include skipped /IS and /MINAGE:

C:\Source
 File-2018.txt    <- 1 files Copied
 File-2019.txt    <- 1 files Skipped

Then the \MIR Mirror command executes and removes all the files from the Destination that were not included in the Source Files:

C:\Destination
 File-2017.txt    <- 2 files Extra
 File-2020.txt  

Regardless of the age of the Extra files

The easy solution is to just not copy over everything to begin with. Start off with an empty directory, and only copy the files that are younger than January 1, 2019:

Remove-Item C:\Destination -Force
Robocopy.exe C:\Source C:\Destination /E /MINAGE:20190101

Edit

The improved method (instead of removing the entire directory first), you simply run a cleanup script on the destination to remove items younger than January 1, 2019 from your destination first:

#Cleanup younger files
Get-ChildItem C:\Destination -Recurse | Where { $_.LastWriteTime -gt (Get-Date '2019-01-01')} | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue

#Mirror over new files
Robocopy.exe C:\Source C:\Destination /MIR /MINAGE:20190101

Upvotes: 8

Related Questions