Monday, August 13, 2007

Robocopy Better

I still cannot get over the fact that Robocopy is just a command line utilitiy that happens to have lots of options. It doesn't know how to branch around. At most it will mirror the content of the source folder to the target folder. I recently made my first boo-boo with Robocopy.

I had to copy the home folders for, say, 3 users, SteveJobs, GuyKawasaki, and DavidPogue, from \\server1\home1$ to \\server2\home2$ . Up to then I have been dealing with just one-user data copy so the .cmd file's content would be

robocopy "\\server3\home3$\user1" "\\server4\home4$\user1" /copy:dat /mir /mot:60 /np /r:2 /w:2 /log+:user1.log

So, to handle three users would I not just add two more lines to the .cmd file ? So I had

robocopy "\\server1\home1$\SteveJobs" "\\server2\home2$\SteveJobs" /copy:dat /mir /mot:60 /np /r:2 /w:2 /log+:SteveJobs.log

robocopy
"\\server1\home1$\GuyKawasaki" "\\server2\home2$\GuyKawasaki" /copy:dat /mir /mot:60 /np /r:2 /w:2 /log+:GuyKawasaki.log

robocopy
"\\server1\home1$\DavidPogue" "\\server2\home2$\DavidPogue" /copy:dat /mir /mot:60 /np /r:2 /w:2 /log+:DavidPogue.log

Simple, right? Uh, no. What happened was that because of the Mirror option ( /mir ) the .cmd file is stuck with the first Robocopy command. The other two commands never got a chance to execute, because with the /mir option in use, as soon as the first Robocopy finished running it hangs around and wait for and monitor for any changes every 60 minutes ( /mot:60 ).

The solution I came up, which seemed great at the time, was simply to add START /MIN in front of each Robocopy command, i.e.

start /min robocopy "\\server1\home1$\SteveJobs" "\\server2\home2$\SteveJobs" /copy:dat /mir /mot:60 /np /r:2 /w:2 /log+:SteveJobs.log

start /min robocopy
"\\server1\home1$\GuyKawasaki" "\\server2\home1$\GuyKawasaki" /copy:dat /mir /mot:60 /np /r:2 /w:2 /log+:GuyKawasaki.log

start /min robocopy
"\\server1\home1$\DavidPogue" "\\server2\home2$\DavidPogue" /copy:dat /mir /mot:60 /np /r:2 /w:2 /log+:DavidPogue.log


With the START command used, each Robocopy is kicked off in its own little world, so that it doesn't matter how long the first one takes to run, the second kicks off right after the first one starts running. The Minumum option ( /min ) is to make the commands run minimized, although I doubt if it's necessary. In the new department, I have much to learn about server maintenance etc. but if the issue involves scripting, I am in familiar territory already.

One side effect of using Start in the .cmd file is that when scheduled, the .cmd completes almost instantly. That's because as far as the .cmd is concerned, it finished processing its content. It didn't care that one Robocopy job may take days to complete. To kill the jobs, I had to use ProcessExplorer and tracked down each of the job. In my case, there was about twenty users so it wasn't so bad, but if I do a major migration involving hundreds or thousands of users this "solution" won't be so great. For the big scenario, I would have use GOTO statements with labels and perhaps some date checking to determine when to stop copying.

Thanks to Simon Shepard's web site, http://www.ss64.com/index.html , I learned more about Robocopy's various switches. Simon referred to the /xo option , used for excluding older files on the source, as unnecessary but I think it has its use. For instance, making use of /xo allows one to cut the user over to the new home and let the user start using the new home folder even while the copy process still runs. As user make changes on the new home data, the new data won't be overwritten because the source data then will be older and thus excluded. Still http://www.ss64.com/index.html is one great site for anyone wanting to learn more about the various Windoze NT/XP commands.

0 comments:

Post a Comment