今天一台MSSQL2000出問題,SQLSERVERAGENT服務又啟不過來,胡亂撮寫的一個指令碼。
'*******************************************************************' 目的:當MSSQL的SQLSERVERAGENT啟不過來時,可以用這個指令碼來備份全部資料庫,也可以做成計劃任務來執行' 注意還原master資料庫要用單使用者啟動MSSQL'*******************************************************************dim spathspath="E:\sqldatabackup\db\" '請注意修改備份路徑dim backpathbackpath=spath & cstr(date() & hour(now) & minute(now) & second(now)) & "\"deletefile(spath & "all.sql")CreateFolderDemo()strContent = ExportDataCall LogToFile(strContent, "all.sql")'---------------------------------Set shell = WScript.CreateObject("WScript.Shell")cmd = "osql -E -i all.sql"Shell.Run cmd, 1, TrueSet shell = Nothing'---------------------------------'Wscript.Echo "完成!"'建立檔案夾 Function CreateFolderDemo() Dim fso, f Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.CreateFolder(backpath & "") CreateFolderDemo = f.Path Set fso = Nothing End Function '刪除檔案function deletefile(filename) if filename<>"" then Set fso = CreateObject("Scripting.FileSystemObject")if fso.FileExists(filename) then fso.DeleteFile filename else ' "<script>alert(''該檔案不存在'');</script>" end if end if Set fso = Nothingend function '寫檔案Function LogToFile(strContent,strFileName)Const ForReading = 1, ForWriting = 2,ForAppending = 8Dim fso, fSet fso = CreateObject("Scripting.FileSystemObject")Set f = fso.OpenTextFile(strFileName, ForAppending, True)f.WriteLine strContent Set fso = NothingEnd Function'產生備份指令碼Function ExportData()Dim strConn,strSql,strDataDim objConn,objRsstrConn = "Provider=sqloledb;Data Source=(local);Initial Catalog=master;Integrated Security=SSPI;"Set objConn = CreateObject("ADODB.Connection")objConn.Open strConn'strSql = "SELECT name,filename FROM sysdatabases"strSql = "SELECT name FROM sysdatabases"Set objRs = objConn.Execute(strSql)If NOT objRs.EOF ThenWhile NOT objRs.EOF strData = strData & "backup database [" & objRs(0).value &"] to disk='" & backpath & objRs(0).value & ".bak'" & vbCrLf & "GO" & vbCrLf objRs.MoveNextWendEnd IfIf Err.Number <> 0 ThenExportData = Err.DescriptionElseExportData = strDataEnd IfobjRs.CloseobjConn.CloseSet objRs = NothingSet objConn = NothingEnd Function