' =============== 區域網路檔案自動備份 VBS 指令碼 =================
'
' 條件:10.97.1.X 開使用者xcopy/xcopy 屬於adminstrator
' 共用BACKUP 檔案所在目錄,使xcopy 使用者完全控制!
' 運行後在管理進程中有2個進程:wscript.exe,ping.exe
' 以日期為log檔案名稱,只保留7天的日誌
'
' =============================================================
Do While True
dim f,txtfile
Set f = CreateObject("Scripting.FileSystemObject")
set txtfile = f.opentextfile("D:\web\barcodeprinter\BACKUP\"&date&".txt",8,True)
'==== 判定7天前的log 檔案是否存在,存在則刪除!
If f.FileExists("D:\web\barcodeprinter\BACKUP\"&date-5&".txt") Then
f.DeleteFile("D:\web\barcodeprinter\BACKUP\"&date-5&".txt")
end if
'====
txtfile.writeline("========= 即時備份開始: "&date&" "&time&"==========")
txtfile.writeline(" ")
Call backupfile(f,txtfile)
WScript.Sleep 10000
txtfile.writeline("========= 即時備份結束: "&date&" "&time&"==========")
txtfile.writeline("")
set f =nothing
set txtfile=nothing
Loop
'------------------------------------ 即時備份過程 -----------------------------------
sub backupfile(f,txtfile)
Dim Path,fso,net
num=1
do while num<10
if num=1 then
ip="10.97.1.3"&num
source_path="\\"&ip&"\產品數位跟蹤"
target_path="BACKUP\barcodeprinter31.mdb"
end if
if num=2 then
ip="10.97.1.3"&num
source_path="\\"&ip&"\產品數位跟蹤"
target_path="BACKUP\barcodeprinter32.mdb"
end if
if num=3 then
ip="10.97.1.3"&num
source_path="\\"&ip&"\產品數位跟蹤"
target_path="BACKUP\barcodeprinter33.mdb"
end if
if num=4 then
ip="10.97.1.3"&num
source_path="\\"&ip&"\產品數位跟蹤"
target_path="BACKUP\barcodeprinter34.mdb"
end if
if num=5 then
ip="10.97.1.3"&num
source_path="\\"&ip&"\qjcf_no1"
target_path="BACKUP\barcodeprinter35.mdb"
end if
if num=6 then
ip="10.97.1.3"&num
source_path="\\"&ip&"\產品數位跟蹤(BarCodePrinter)"
target_path="BACKUP\barcodeprinter36.mdb"
end if
if num=7 then
ip="10.97.1.3"&num
source_path="\\"&ip&"\產品數位跟蹤(BarCodePrinter)"
target_path="BACKUP\barcodeprinter37.mdb"
end if
if num=8 then
ip="10.97.1.3"&num
source_path="\\"&ip&"\產品數位跟蹤"
target_path="BACKUP\barcodeprinter38.mdb"
end if
if num=9 then
ip="10.97.1.3"&num
source_path="\\"&ip&"\產品數位跟蹤(BarCodePrinter)"
target_path="BACKUP\barcodeprinter39.mdb"
end if
'
ip="10.97.1.3"&num
source_path="\\"&ip&"\產品數位跟蹤"
target_path="D:\WEB\barcodeprinter\BACKUP\barcodeprinter3"&num&".mdb"
'------------ ping 測試,網路通ping="T"; -------------------
Set FileSys = CreateObject("Scripting.FileSystemObject")
Set WShShell = CreateObject("WScript.Shell")
RetCode = WShShell.Run("ping " &IP , 0, True )
if RetCode = 0 Then ' ping 測試通過
ping="T"
else
ping="F"
end if
set filesys = nothing
set wshshell = nothing
'------------ ping 結束 ------------------------------------
if ping="T" then
On Error Resume Next '出錯不顯示,執行下一條
Path="X:"
Set fso = CreateObject("Scripting.FileSystemObject")
Set net = CreateObject("WScript.Network")
If Not fso.DriveExists(fso.GetDriveName(Path)) Then
net.MapNetworkDrive Path,source_path,False,"xcopy","xcopy"
End If
Set file = fso.GetFile(Path&"\barcodeprinter.mdb")
file.Copy(target_path)
net.RemoveNetworkDrive Path,True
If Err.Number<>0 Then
txtfile.writeline("--- "&ip&" 備份失敗! "&date&" "&time)
else
txtfile.writeline("--- "&ip&" 備份成功! "&date&" "&time&"--- OK!")
end if
else
txtfile.writeline("--- "&ip&" 網路不通! "&date&" "&time)
end if
set fso=nothing
set net=nothing
num=num+1
loop
end sub
'------------------------------------ 即時備份過程 -----------------------------------
這裡備份的檔案是barcodeprinter.mdb這個資料庫檔案,大家可以根據自己的實際情況進行代碼修改