在 SPS2003 中有個備分的功能,我們可以使用它來進行備分,不過通常我們使用的都是 GUI 的版本,這裡提供一個 GUI Less 的方法,可以應用在 Schedule Task 的 Script 或者是 Batch 中,備分命令是這樣下的:
C:\Program Files\SharePoint Portal Server\Bin\spsbackup /all /file \RemoteServer\SPSBACKUP\20040820
如此一來,我們可以直接建立一個批次檔,然後放在 Schedule Task 中執行,讓系統幫我們自動進行每天的備分工作。但是,我所想作的不只是這樣,我希望系統的備分工作可以更自動,更人性化一點。我的劇本:
[1] 每天自動進行備分。
[2] 備分時的檔案名稱使用當天的日期
[3] 對於備分的資料僅保存一個星期,超過一個星期的資料自動刪除。
這個劇本對於我的應用系統來說,已經足夠了,而且我可不希望每天的備分檔案把我的硬碟給塞滿了。那麼,我們可以利用 Script 程式來作一點簡單的變化,實現這個更完美的備分程序。
[SPS-BACKUP.VBS]
— START SPS-BACKUP.VBS
Option Explicit
‘ Useg: Cscript sps-backup.vbs to run this file.
‘
‘ This program will backup SPS2003 and try to delete old backup files
‘ 2004-04-13 by James Lin (james{at}awaytech.com)
Const L_BackupProgramDir = “C:\Progra~1\ShareP~1\Bin”
Const L_BackupProgram = “SPSBackup.exe”
Const L_BackupServer = “myBackup”
Const L_BackupDir = “\SPS-Backup”
Const L_BackupCaption_Text = “SPS Backup Progress V1.0 by James Lin (james{at}awaytech.com)”
Const L_Say = “@.@ “
Const L_DayKeep = 7
Dim sToday, sBackDir, objFS, sFolder, objWShell
Dim sYear, sMonth, sDay, strDelString
‘Check if run with CScript
If (Not IsCScript()) Then
Dim i
for i = 0 to (oArgs.Count-1)
strArgs = strArgs & ” ” & oArgs(i)
next
Set WshShell = WScript.CreateObject(“WScript.Shell”)
WshShell.Run “Cscript.exe ” & WScript.ScriptFullName & strArgs
WScript.Quit ‘ terminate this script
End If
‘Show Caption
wscript.echo L_BackupCaption_Text
wscript.echo “”
sYear = year(date)
sMonth = month(date)
sDay = day(date)
if LEN(sMonth) < 2 Then sMonth = “0” & sMonth
if LEN(sDay) < 2 Then sDay = “0” & sDay
sToday = sYear & sMonth & sDay
wscript.echo L_Say & “Backup to file ” & sToday
‘Run SPSBackup
Set objWShell = CreateObject(“WScript.Shell”)
dim strCommand
strCommand = ” /all /file ” & L_BackupServer & L_BackupDir & sToday
wscript.echo L_Say & “Command argument: ” & strCommand
strCommand = L_BackupProgram & strCommand
objWShell.Run “%comspec% /k ” & L_BackupProgramDir & strCommand
sYear = year(date – L_DayKeep)
sMonth = month(date – L_DayKeep)
sDay = day(date – L_DayKeep)
if LEN(sMonth) < 2 Then sMonth = “0” & sMonth
if LEN(sDay) < 2 Then sDay = “0” & sDay
sToday = sYear & sMonth & sDay
‘Delete old backup set
wscript.echo L_Say & “Try to delete ” & sToday & ” backup set…”
Dim sFiles, sFile
SET objFS = CreateObject(“Scripting.FileSystemObject”)
SET sFolder = objFS.GetFolder(“” & L_BackupServer & L_BackupDir)
SET sFiles = sFolder.Files
If sFiles.Count <> 0 Then
For Each sFile In sFiles
If MID(sFile.Name,1,8) = sToday Then
wscript.echo L_Say & “Delete – ” & sFile.Name
objFS.DeleteFile (sFile)
End If
Next
End If
SET objFS = NOTHING
wscript.echo L_Say & “Done!!!”
wscript.quit
Function IsCScript()
If (Instr(UCase(WScript.FullName), “CSCRIPT”) <> 0) Then
IsCScript = true
Else
IsCScript = false
End if
End function
— END SPS-BACKUP.VBS
你可能會需要對 SPS-BACKUP.VBS 檔案中的常數值進行一些修改,以符合你的 SPS2003 環境。另外,我們還需要另一個檔案來驅動這個 Script,既然要放在 Schedule Task 中,最簡單的方法就是使用一個批次檔了,這個批次檔看起來會像這個樣子:
[SPS-BACKUP.BAT]
— START SPS-BACKUP.BAT
REM Daily backup files to remote server – by James Lin
REM This procedure will keep 7 days
@C:\WINDOWS\system32\cscript.exe sps-backup.vbs
— END SPS-BACKUP.BAT
然後,我們直接把這個批次檔加入 Schedule Task 中,設定好每天會自動執行的時間,一切就都會自動的進行了。如果需要保留更長時間的備分檔,直接修改 SPS-BACKUP.VBS 中 L_DayKeep 這個常數值就可以了。
Keywords: SharePoint 2003 Backup Script, Batch Backup
[End]