Friday, July 31, 2015

Robocopy purge files older than....

No comments
I use Robocopy for a lot of things in my workplace. One of the main jobs I use it for is to replicate a file server share from one NAS to a redundant NAS for quick fail-over should the NAS fail. I also like to use that copy for quick restores should someone delete something they shouldn't. The problem with that is if I run a purge on the weekends and someone accidentally deletes something just 6 hours prior to my purge well its gone. So I had to come up with a solution to only purge items older than say 7 days. Originally I thought I could get it all done with PowerShell. I tried using something like the following.
$source = "D:\share"
$destination = "F:\share"
# Directory Compare and Delete if older than 7 Days
Compare-Object (Get-ChildItem $source) (Get-ChildItem $destination)`
| where {$_.SideIndicator -eq "=>"}`
| where {$_.InputObject.LastWriteTime -le (Get-Date).Adddays(-7)}`
| Foreach {Remove-Item -Path $_.InputObject.Fullname}

While that script does actually work it quickly begins to consume large amounts of RAM depending how large your directories are. In my case it ran for about 45 minutes at which point it consumed over 2.5GB of RAM and had my file server at its knees with 98% RAM utilization. Need less to say I had to come up with another solution and this is what I came up with.

This script uses Robocopy to run a purge but with the L switch so it only logs what it would purge without actually performing the task. I then use PowerShell to parse that log file and only pull out the "EXTRA File" and "EXTRA Dir" lines into a parsed log file and clean it up to only have one file path per line.  I then assign the $files variable to all lines that have a "lastwritetime" of 7 days or older. Then perform a for each on the $files variable and delete each file as well as do some console output and logging.  

No comments :

Post a Comment