Wednesday, February 28, 2007

Backup Script for SharePoint 2007

Thanks (Muchos Gracias) to a posting on MSD2D by Herman Vargas and my colleague, Mario Barajas for translating all the comments that were in Spanish. I've managed to modify the script for a SharePoint 2007 backup. Not the best out there, but it's something.

I have not tested the script fully and assume that it would work. The script keeps the 5 latest backup copies. You can modify the following to suit what you need:

C_BackupPath : The path where the backup resides
C_NamePrefix : Prefix of the filename of the "backup prefix". Example: SharePoint_20070228
C_SiteURL : The URL of the SharePoint Site to be backed up

C_SharePointBin: Path to the Bin directory of Sharepoint installation
C_MaxBackupPerFolder: Number of XML Manifest per C_BackupPath folder.


Below is the .vbs script:

' Description:
' Tool for planning and automating files/entries
' Microsoft Office SharePoint Server 2007
'
' Will generate file entry names with the following style: MOSS_YYYYMMDD
' E.g.: SharePoint_20070228
' If there are more than 5 entries in the directory, then it will choose the one with the oldest date and will delete it

Option Explicit

Const C_BackupPath = "C:\Backup"
Const C_SiteURL = http://sharepointserver/site
Const C_NamePrefix = "SharePoint_"
Const C_SharePointBin = "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\Bin"
Const C_MaxBackupPerFolder = 5

Dim fso, folder, filedate, f, file
Dim sBackupPrefix
Dim CntXml
Dim DeletedBackup

Set fso = CreateObject("Scripting.FileSystemObject")

if Not fso.FolderExists(C_BackupPath) then
WScript.Echo "The folder " & C_BackupPath & " does not exist!"
WScript.Quit
end if

Set folder = fso.GetFolder(C_BackupPath)
filedate = now
Set f = Nothing

' Counts the number of XML manifests that are in the directory
CntXml=0
for each file in folder.files
if lCase(right(file.name,4)) = ".xml" then
CntXml=CntXml+1
end if
next

'WScript.Echo "CntXml... " & CntXml

DeletedBackup=""
' If it is greater than the maximum
if CntXml > C_MaxBackupPerFolder then

' Looks for the older entry/file
for each file in folder.files
if lCase(right(file.name,4)) = ".xml" then
if file.DateLastModified < deletedbackup =" lcase(Mid(file.name,1,Instr(1,file.name," filedate =" file.DateLastModified">"" then
for each file in folder.files
if lcase(mid(file.name, 1, len(DeletedBackup)))=DeletedBackup then
Set f = file
f.delete
' WScript.Echo "Deleting..." & file.name
Set f = Nothing
end if
next
end if

end if


Set folder=nothing
Set fso=nothing


' Assigns a name of the new file
sBackupPrefix = C_NamePrefix & year(now) & right("00" & Month(now),2) & right("00" & day(now),2)

Dim objShell
Dim strcmd

Set objShell = CreateObject("WScript.Shell")

strcmd = C_SharePointBin & "\stsadm.exe -o backup -url " & C_SiteURL & " -filename " & C_BackupPath & "\" & sBackupPrefix & ".bak -overwrite"

objShell.Exec(strCmd)

WScript.Echo ""& C_SharePointBin & "\stsadm.exe -o backup -url " & C_SiteURL & " -filename " & C_BackupPath & "\" & sBackupPrefix & ".bak -overwrite"

WScript.Echo "Backup of site collection successful"

Set objshell = nothing

2 comments:

Anonymous said...

This script is a great idea, but it doesn't work. I think there is a missing parenthesis or two and maybe some other errors in this line: if file.DateLastModified < deletedbackup =" lcase(Mid(file.name,1,Instr(1,file.name," filedate =" file.DateLastModified">"" then

Anonymous said...

Nice, but are just assuming backup was a success, how do you know?