A reader asked in a comment how to script STSADM backups and be able to keep multiple copies. He's using a command like this:
Stsadm.exe –o backup –url http://www.toddklindt.com –filename tk.stsadm -overwrite
Or
Stsadm.exe –o export –url http://www.toddklindt.com/blog -filename tk-blog.cmp –overwrite
He wants to keep more than one instance of the backups, in case someone deletes something and they don't get back to you right away. I have a great way to work around this. Windows has an environment variable that is equal to the date. We can start with this. It's not in a good format, so we'll have to tweak it some:
For one, the backslashes will cause us problems in the file system. Second, leading with the day of the week will not sort very well. What we want is something along the lines of YYMMDD. Fortunately there is a way to parse through variables to get snippets of the information. While this is documented in the SET command, it works fine without it. To get the full skinny type set /? at a Command Prompt to get the full usage. Set has a lot of usage, but here's the part we care about:
We can use this approach to pull out the parts of the %date% variable that we want. Here's what the command would look like:
The string "%date:~-2%%date:~4,2%%date:~7,2%" turns into YYMMDD. The first part "%date:~-2%" says 'start at the end and take the last two characters.' The next part, "%date:~4,2%" says 'skip the first four characters and give me the next 2'. The last part, "%date:~7,2%" says 'skip the first seven characters and give me the next 2.' You put all that together and you get YYMMDD. Now just couple that with your STSADM command and you're all set. It would look something like this:
I included a filename with spaces to demonstrate how to take care of that. Pretty slick, huh? I can hear some people out there saying, "That's great, but now my drive is going to get full of backups. Some help you turned out to be!" Settle down, I've got the fix for that too. Another built in command FORFILES will fix that right up. Add the following line to your scheduled script:
forfiles /d -7 /m *.stsadm /c "cmd /c del @file"
That command will delete all files that end in .stsadm that are more than seven days old. Now you can keep exactly as many backups of your sites or webs as you want.
Handy?
Thanks to the reader that asked.
tk