Cari Blog Ini

21 Desember 2011

VB Script for Backup Database Using SQL Command

Save the code below, as "Backup_Database_Northwind.vbs"
Then double-click the file, to get the results example "Backup_DB_Northwind_20111221_1500.bak"


'--------------------------------------------------------------------------
'-----code start
'--------------------------------------------------------------------------
OPTION EXPLICIT
dim ws, MyStr, MyTimeStamp, MyFileName
dim q



MyTimeStamp = cstr(year(now)) & _
    right(cstr(100+month(now)),2) & _
    right(cstr(100+day(now)),2) & "_" & _
    right(cstr(100+hour(now)),2) & _
    right(cstr(100+minute(now)),2)


Set ws = WScript.CreateObject("WScript.Shell")

MyFileName ="'C:\BackupDatabase\Backup_DB_Northwind_" & MyTimeStamp  & ".bak'"

MyStr ="sqlcmd -S ""IT102"" -d ""Northwind"" -U ""sa"" -P ""sasasa"" -Q ""BACKUP DATABASE Northwind TO DISK =" & MyFileName & """  "

ws.run MyStr

'--------------------------------------------------------------------------
'-----code end
'--------------------------------------------------------------------------