Vbscript通过网络备份数据库
上篇 写到,数据库备份需要转到其他的服务器上才能更令人放心,
今天用vbscript写了一小段脚本,配合计划任务,
每天定时将 134数据库服务器上的数据库全备份copy到132服务器上。
1 on error resume next
2 dim fsoLog, logFile
3 set fsoLog = createObject("Scripting.FileSystemObject")
4 set logFile = fsoLog.openTextFile("D:\backup\dbFileCopy.log", 8, true)
5 Logfile.writeLine "---------------------------------------------------"
6 logFile.writeLine now() & " start"
7
8 dim nowdate
9 nowdate=formatDate(now())
10 dim dbFileName
11 dbFileName= nowdate & "000000_full.bak"
12 dim objNetwork
13 Set objNetwork = CreateObject("Wscript.Network")
14 '下面这一句代码的意思是将 192.168.22.132\D$这个网络共享映射到本地的X盘,administrator 是用户名,123456是密码
15 objNetwork.MapNetworkDrive "X:", "\\192.168.22.132\d$", False, "administrator", "123456"
16 If err.number <> 0 then
17 Logfile.writeline err.description
18 Logfile.writeline err.number
19 Else
20
21 Logfile.writeline "d:\backup\" & dbFileName
22
23 Dim oFSO
24 Set oFSO = CreateObject("scripting.FileSystemObject")
25 oFSO.CopyFile "d:\backup\" & dbFileName, "x:\dbFile_134.bak" '开始拷贝操作
26
27 If err.number <> 0 then
28 Logfile.writeline strCommand
29 Logfile.writeline err.description
30 Logfile.writeline err.number
31 End If
32 End if
33
34 objNetwork.RemoveNetworkDrive "X:" '完成之后,断开网络驱动器的映射
35
36 logFile.writeLine now() & " files copied"
37
38 Wscript.Quit
39
40
41
42 function formatDate(date1)
43 dim y,d,m
44 dim ret
45 y=year(date1)
46 m=month(date1)
47 d=day(date1)
48 if m<10 then
49 m="0" & m
50 end if
51 if d<10 then
52 d="0" & d
53 end if
54
55 ret=y & m & d
56 formatDate = ret
2 dim fsoLog, logFile
3 set fsoLog = createObject("Scripting.FileSystemObject")
4 set logFile = fsoLog.openTextFile("D:\backup\dbFileCopy.log", 8, true)
5 Logfile.writeLine "---------------------------------------------------"
6 logFile.writeLine now() & " start"
7
8 dim nowdate
9 nowdate=formatDate(now())
10 dim dbFileName
11 dbFileName= nowdate & "000000_full.bak"
12 dim objNetwork
13 Set objNetwork = CreateObject("Wscript.Network")
14 '下面这一句代码的意思是将 192.168.22.132\D$这个网络共享映射到本地的X盘,administrator 是用户名,123456是密码
15 objNetwork.MapNetworkDrive "X:", "\\192.168.22.132\d$", False, "administrator", "123456"
16 If err.number <> 0 then
17 Logfile.writeline err.description
18 Logfile.writeline err.number
19 Else
20
21 Logfile.writeline "d:\backup\" & dbFileName
22
23 Dim oFSO
24 Set oFSO = CreateObject("scripting.FileSystemObject")
25 oFSO.CopyFile "d:\backup\" & dbFileName, "x:\dbFile_134.bak" '开始拷贝操作
26
27 If err.number <> 0 then
28 Logfile.writeline strCommand
29 Logfile.writeline err.description
30 Logfile.writeline err.number
31 End If
32 End if
33
34 objNetwork.RemoveNetworkDrive "X:" '完成之后,断开网络驱动器的映射
35
36 logFile.writeLine now() & " files copied"
37
38 Wscript.Quit
39
40
41
42 function formatDate(date1)
43 dim y,d,m
44 dim ret
45 y=year(date1)
46 m=month(date1)
47 d=day(date1)
48 if m<10 then
49 m="0" & m
50 end if
51 if d<10 then
52 d="0" & d
53 end if
54
55 ret=y & m & d
56 formatDate = ret
57 end function