BackupScript with RoboCopy

BackupScript with RoboCopy

I just recently was introducted to RoboCopy from Windows, to me it is their answer to rsync. RobyCopy is a tool is ran from the terminal just like rsycn with flags and such.

Example: robocopy C:\Users\atorr\Backups W:\robocopy /M /XA:H /W:0 /R:1 /REG

So what am I looking at? Honenstly the best answer I found was found from Jacob over at Caught in Pixels and he did such a good job, I am totally giving him 111% respect: https://caughtinpixels.com/how-to-create-a-backup-script-using-robocopy/

And the parameters explained for the nerds:

ParameterDescription
C:\DataIs the source directory. I always store data that I backup in a folder called Data. It makes backup easy, just as well as it makes migration to a new computer very easy.
T:\DesktopPC\C-DriveIs the destination directory
/MIRThis tells robocopy to make a mirror backup.
NB! Another important detail and word of warning, is that when you make a Mirrored backup, if you delete something in the Source, it will also be deleted in the Destination, when the script runs. This is also true if you delete something by accident.
/XA:HLeave out the hidden files. If they are hidden they are in my terminology not important.
/W:0Wait time between retries, the default is 30 seconds. If a file is locked, because it is in use, the robocopy can wait and retry. I don’t want the backup to wait, it should continue straight away, otherwise it can take too long. I’ll get the file backed up the next time.
/R:1The number of retries, if the file is locked. I set the to 1, the default is 1.000.000. If you combine that with with the /W paramater and a default of 30 seconds, that is potentially 30.000.000 seconds pr file permanently locked. The backup will never finish if you just have one single file that is permanently locked.
/REGThis one saves new default values on /W and /R to the registry. In theory this means that I only need to use this parameter once, but I keep it around for when I restore Windows or get a new computer.
/XDExclude Directories. This one is used to exclude directories like c:\temp. I only use it with fully qualified paths to be sure what it excludes.
/XFExclude files. I use this to exclude certain file extensions, like *.lrprev

Yep that is it! I am not going to lie, this is a very nice tool to have when you have only powershell access, or scripting, or even in a gui, as this is actually faster to move the data then a simple copy/paste. It also works across network paths as well.

Oh here is a tiny tiny tiny example of a backup script that I created wanting to first creatre a backup of the Documents folder, zip it up, and time stamp the filename, then robocopy it over to my external drive, then removed the zip from the source.

Compress-Archive -LiteralPath 'C:\Users\atorr\Documents' -DestinationPath "C:\Users\atorr\Backups\backup-Documents-$(get-date -f yyyy-MM-dd).zip"
robocopy C:\Users\atorr\Backups W:\robocopy /M /XA:H /W:0 /R:1 /REG
del C:\Users\atorr\Backups\*.zip

And then you delete the source! ** If you want to, always double check your backups first before deleting anything. Trust me on this one.

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply

    Your email address will not be published. Required fields are marked *