Sometimes you need to mirror files between a couple of different servers in your windows environment. There are a couple of command-line tools that can be used together to make this a very easy job. To further simplify things, especially if you need to mirror the same files/folders on a recurring basis, you can drop it all into an old-school .bat file and run it with the click of the mouse or then launch it via task scheduler. I found myself in need of doing just this recently and I came up with this script (all of my specific-use info has been cleansed/replaced):
echo First we will mount our shares to drive X and Y
net use x: "\\18.104.22.168\Share Name1" /user:admin plaintextpassword
net use y: "\\22.214.171.124\Share Name2" /user:admin plaintextpassword
echo Copy folders from first share
robocopy "X:\Folder 1" "D:\path\Folder 1" /MIR
robocopy "X:\CoolFolder2" "D:\path\CoolFolder 2" /MIR
echo Copy folders from second share
robocopy "Y:\Another Folder 1" "D:\differentpath\longerpath\Another Folder 1" /MIR
robocopy "Y:\Yet Another Folder 2" "D:\differentpath\longerpath\Yet Another Folder 2" /MIR
echo All Done Copying
echo Will now unmount shares
net use x: /delete
net use y: /delete
echo All Done
The reason I provide scripts like this? Well, first, I am archiving them here for myself. Second, sometimes it is hard to find good examples of commands so you can figure out proper syntax and having something like this can help a lot. I am not going to walk through the entire script, but I am going to point out some of the bits and pieces that are important…
First, general batch file syntax stuff…
You may have noticed I use double-quote marks around all of my paths. The reason for this is because often times, especially in the Microsoft world, you will run into folders with spaces in their names. Spaces cause trouble with command lines. To get around this, we wrap our paths in double-quotes and this tells our command that everything inside is part of the path, exactly as written. Anytime your path contains spaces, make sure you wrap it in quotation marks.
This isn’t a very good script for automation, it was meant to be run manually. Throughout the script you will see “Pause” written. This stops the script from running and prompts the user to hit a key before it will continue on, which can be useful if you want them to check on what the script is doing as it moves along.
Sometimes you might need to put a delay in your script. In this case, I added a 10 second delay to ensure the shares are mounted before the rest of the script runs.
Robocopy is an excellent tools for copying files from one place to another. It has a slew of flags that can be used to do various things with the copy operation. In this case I am using the mirror flag, denoted by “/MIR.” Mirror is excellent if you want a perfect sync of files and folders between source and destination. Robocopy will not only copy all new or changed files from the source, it will also delete any files or folders at the destination that are not present in the source. Once you are done, your destination should match your source darn near perfectly.
Finally, Net Use…
The Net Use command allows you to mount a remote share as if it were a local drive on the system. If you are looking at my script, how it works is pretty self-evident.
That’s it! Hopefully this has been helpful!