VBS scripts get the latest data backup and duplicate data files
We mentioned earlier, the recent company has been on the OA system on-line busy, so for OPS I need to use some scripting tools to share melancholy for me, and now the problem is that we need to copy the SQL database data from a real-world scheduled backup to the test environment, and then restore the database, Since we can restore the database by using the SQL-brought script, we need to unify the restored database name as we all know that the database is named after the scheduled task backup, so we are not very convenient when we use the script to automatically restore, after all, not very user-friendly , so we think of the most recent backup of the SQL data file through the VBS script copied to the local, and then the same name, so you can use the SQL comes with a script to specify the restore database, of course, one of the most important topic is, because SQL automatic backup is named after the date, We need to judge the latest backup data, so we need to be in the script by judging the last modification date and the current date comparison, the latest data copy and rename, the nonsense is not much to say, specifically, see below:
Let us first declare: At the beginning of the script, we define a variable rname, the goal is to better use, the Rname value is equal to the name of the end of the need for duplicate names;
We first confirm the target directory data first
650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;background-image:none;border-bottom:0 px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M02/85/8F/wKioL1eof7bRmFIfAAGwUIeTV0g651.png "height=" 399 "/>
dim rnamername = "Sqlrecoverydata" sourcefilespath= "D:\EcologyDB_Backup" ' desfilepath= "\ \ 10.12.0.51\backup\databackup "&" "&year (date) &-month (date) &-day (date) &" "&hour (Time ) &-minute (time) & "\" desfilepath= "D:\DBRcoveryDir\" Set dic=createobject ("Scripting.Dictionary") Set fso=createobject ("Scripting.FileSystemObject") If not fso. FolderExists (Desfilepath) Then ' FSO. Createfolder desfilepathend ifbackfolderpath=getlastmodify (Sourcefilespath) ' msgbox Backfolderpathfso.copyfile backfolderpath,desfilepath&rnamefunction getlastmodify (folder) set fso = createobject ("Scripting. FileSystemObject ") set folder= Fso.getfolder (folder) Set subFolders = Folder.Files nowdate= now for each&Nbsp;subfolder in subfolders dic. Add datediff ("s", Subfolder.datelastmodified,nowdate),subfolder.path next numarray=dic. Keys bn = numarray (0) for each nn in numarray if bn >= nn Then bn = nn end if next getlastmodify = dic. Item (BN) end function
Then we save the script, we start to execute the script, after execution we look at the execution result;
650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;background-image:none;border-bottom:0 px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M00/85/8F/wKioL1eof7fg4rzVAAHLWHxILms743.png "height=" 444 "/>
Note that if the data needs to be copied remotely, we only need to add two directories as a share, and then add the directory permissions for each machine.
We then use the Windows-brought scheduled task to run the script at timed
650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;background-image:none;border-bottom:0 px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M01/85/8F/wKiom1eof7ijFaDsAAGxY2--jn4223.png "height=" 284 "/>
Finally, let's talk about SQL database restore if you encounter a prompt that the database is using, we can execute the following statement on the master database
ALTER DATABASE [DBName]
SET OFFLINE with ROLLBACK IMMEDIATE
After execution, the following issues occur if the restore occurs;
650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;background-image:none;border-bottom:0 px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M02/85/8F/wKiom1eof7mDQFhQAAExtUJ9iCw300.png "height=" 296 "/>
We'll just check the overwrite existing database under the Restore option.
650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;background-image:none;border-bottom:0 px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M00/85/8F/wKiom1eof7rgKELjAAEsAiNUGvo727.png "height=" 351 "/>
This article from "Gao Wenrong" blog, declined reprint!
VBS scripts get the latest data backup and duplicate data files