Great script Sean!
Ok so I've been messing around with Robocopy and up until yesterday I was sort of annoyed that if I used Robocopy it would not be a 100% sure thing since it was not able to backup any locked or open files. For example, if you use Outlook for email and forgot to close the program before your backup program ran, it would completely skip that file and you would have no emails backed up.
Well I kept looking around on Google for a solution or alternative and ended up figuring out a really cool way to have my cake and eat it too!

So, like I said, its true that Robocopy doesn't handle open/locked files, BUT if we use Robocopy and wrap around it another program, called ShadowSpawn, we can then duplicate the same backup style that Windows Backup uses itself -- creating a temporary shadowed copy of all the files that you want to backup so that if you end up opening or using any of the files that you're copying, it will still work!

Ok, so this is how I set it up -- anyone who is reading this, feel free to customize your setup as you wish. I wanted an automated solution that would wake my computer up every night, create a shadow copy, back up my files, and then put my computer to sleep after it was done.
What I use:
--Windows 7 Task Scheduler (included in Win7)
--
ShadowSpawn 0.2.1-x64
--
Robocopy (included in Win7)
Ok, so the first step is to create a task in Task Scheduler. Give your task a name, mine is simply called Custom Backup. Make sure it is set to "Run with highest privileges". On the Triggers tab, set up your schedule. I have my backup run every night at midnight except for on Saturday nights (on Sat night I have the system do a full virus scan instead).
Then on the Actions tab select New, choose "Start a Program" as the action, and browse to your ShadowSpawn.exe file. Select that file and then in the "Add arguments (optional)" field, enter in your Robocopy script. MAKE SURE you change your Robocopy script to work with ShadowSpawn. It can't just be your regular Robocopy script. Instead, what you need to do is setup a drive letter (which isn't being used) as the Temporary Drive where all your files are shadowed while your backup is being made.
In my example below, I am backing up all the contents of the directory 'Stuff' on my hard drive (D:) to my external hard drive (Z:) but I am using the drive letter Q: as my temporary shadow drive. So D:\Stuff is shadowed to Q:, and then Robocopy takes everything from Q:\ and copies it to Z:\Backup\Stuff. You can put this all in as one command with your robocopy flags at the end.
Code:
D:\Stuff\ Q: Robocopy Q:\ Z:\Backup\Stuff /V /MIR /R:5 /W:5 /TEE /LOG+:Z:\Backup\BackupLog.txt
Be super careful about syntax errors. When you specify your fake drive, you don't include a backslash (just Q:), and when you use the Robocopy command, then you do include a backslash (Q:\).
So this will now do a shadowed backup of my D:\Stuff directory to my Z:\Backup\Stuff directory. Now, I like having my computer sleep whenever its not being used, so I want to make sure it wakes up every night to run this program. Under the Conditions tab in my task, I choose "Wake the computer to run this task" and uncheck all the other boxes on that page. You can of course choose to do it however you want.
Ok so that takes care of the computer waking up, and running the backup with the shadow drive. But one thing I want is for the computer to immediately go to sleep after it has finished! I could wait for the regular idle timeout to take place before it goes to sleep, but....why wait?

So, the next step is this. Open up notepad, copy the following text into it:
Code:
%windir%\System32\rundll32.exe powrprof.dll,SetSuspendState
and then save it as sleepnow.bat. If you saved it correctly, the icon of the file should change from a regular text file. Put this file in a safe place and on the Actions tab of the same Task you just created, click on New and browse to the sleepnow.bat file you just created. Make sure that this action shows up BELOW the ShadowSpawn/Robocopy action in your action sequence.
And there you go!

Now your computer will wake up, back itself up, and go to sleep, all on its own, AND you don't have to worry about files not being copied because they were in use!

Let me know if this helped you out, I sure hope it did!
Edited by GasMan320 - 11/12/11 at 3:42pm