近期公司的一些重要服务器需要对数据进行备份,所以就想到用vbs进行备份,一开始用powershell进行备份,后来还是选用了vbs,主要操作为将服务器的重要备份数据进行备份(剪贴)到指定的服务器目录上,然后对备份的目录文件进行文件名+日期进行备份,由于数据比较重要,然后又将备份后的数据进行了二次复制到云服务器上,最主要的是将本地的备份数据通过获取文件夹修改时间将最新的备份数据进行复制,所以就有两个脚本来完成,然后通过windows系统的计划任务进行自定义执行。具体脚本内容见下:
一、将指定路劲的数据进行剪贴到指定路劲
sourcefilespath="R:\DBBackup" desfilepath="\\192.168.4.100\Data_Backup\DataBackup"&""&Year(date)&-Month(date)&-Day(date)&" "&Hour(time)&-Minute(time)&"\" Set fso=CreateObject("Scripting.FileSystemObject") If Not fso.FolderExists(desfilepath) Then fso.CreateFolder desfilepath ‘fso.copyfile 为复制文件 ‘fso.copyfile sourcefilepath,desfilepath ‘fso.movefile 移动文件 MoveFiles sourcefilespath,desfilepath End If Function MoveFiles(yPath,sPath) On Error Resume Next Dim folder,Files,File,subFolder,subFolders Set fso = createobject("scripting.FileSystemObject") Set Folder = fso.getFolder(yPath) Set Files = Folder.Files ‘msgbox yPath & sPaht For Each File In Files fso.MoveFile File,sPath&"\" ‘msgbox File Next Set subFolder = Folder.SubFolders For Each subFolders In subFolder folderTemp = Split(subFolders,"\") FolderName=FolderTemp(ubound(folderTemp)) fso.createFolder(sPath&"\"&FolderName) MoveFiles subFolders,sPath&"\"&FolderName&"\" fso.DeleteFolder subFolders Next End Function
二、将备份后的数据拷贝到其他主机进行二次备份,区别在于备份到云服务器上的数据需要对第一次备份的数据进行判断,将最新的数据进行备份即可,所以通过文件夹的修改时间进行获取然后通过本地时间进行判断最近时间才进行脚本中的复制操作。
sourcefilespath="D:\Data_Backup" desfilepath="\\10.12.0.51\Backup\DataBackup"&""&Year(date)&-Month(date)&-Day(date)&" "&Hour(time)&-Minute(time)&"\" Set dic=CreateObject("Scripting.Dictionary") Set fso=CreateObject("Scripting.FileSystemObject") If Not fso.FolderExists(desfilepath) Then fso.CreateFolder desfilepath ‘fso.copyfile 为复制文件 ‘fso.copyfile sourcefilepath,desfilepath ‘fso.movefile 移动文件 backFolderPath=GetLastModify(sourcefilespath) CopyFiles backFolderPath,desfilepath End If ‘‘移动文件 Function MoveFiles(yPath,sPath) On Error Resume Next Dim folder,Files,File,subFolder,subFolders Set fso = createobject("scripting.FileSystemObject") Set Folder = fso.getFolder(yPath) Set Files = Folder.Files ‘msgbox yPath & sPaht For Each File In Files fso.MoveFile File,sPath&"\" ‘msgbox File Next Set subFolder = Folder.SubFolders For Each subFolders In subFolder folderTemp = Split(subFolders,"\") FolderName=FolderTemp(ubound(folderTemp)) fso.createFolder(sPath&"\"&FolderName) MoveFiles subFolders,sPath&"\"&FolderName&"\" fso.DeleteFolder subFolders Next End Function ‘‘获得最后修改时间的文件夹 Function GetLastModify(folder) Set fso = createobject("scripting.FileSystemObject") Set Folder=fso.getFolder(folder) Set subFolders = Folder.SubFolders nowdate= Now For Each subFolder In subFolders dic.Add datediff("s",subFolder.DateLastModified,nowdate),subFolder.path Next NumArray=dic.Keys fSortArray NumArray GetLastModify=dic.Item(NumArray(UBound(NumArray))) End Function Function fSortArray(aSortThisArray) Dim oArrayList, iElement Set oArrayList = CreateObject( "System.Collections.ArrayList" ) For iElement = 0 To UBound(aSortThisArray) oArrayList.Add aSortThisArray(iElement) Next oArrayList.Sort set fSortArray = oArrayList End Function ‘‘拷贝文件 Function CopyFiles(yPath,sPath) On Error Resume Next Dim folder,Files,File,subFolder,subFolders Set fso = createobject("scripting.FileSystemObject") Set Folder = fso.getFolder(yPath) Set Files = Folder.Files ‘msgbox yPath & sPaht For Each File In Files fso.copyFile File,sPath&"\" ‘msgbox File Next Set subFolder = Folder.SubFolders For Each subFolders In subFolder folderTemp = Split(subFolders,"\") FolderName=FolderTemp(ubound(folderTemp)) fso.createFolder(sPath&"\"&FolderName) CopyFiles subFolders,sPath&"\"&FolderName&"\" ‘fso.DeleteFolder subFolders Next End Function
时间: 2024-10-25 06:38:28