- 浏览: 311525 次
- 性别:
- 来自: 深圳
-
文章分类
最新评论
-
lzz3717845:
有道理,受教了~谢谢
关于SQL中自动增长列值回复(还原)的问题 -
桃汁天天:
ExtJs中如何动态设置其选中状态!! -
sangumaolv2:
你TM在说什么啊。。。。
关于SQL中自动增长列值回复(还原)的问题 -
duyupeng:
你好,我最近也是在使用这个控件,只是用了_id、_parent ...
ExtJs 中的TreeGrid(Ext.ux.maximgb.tg.EditorGridPanel详解与其他问题解决办法) -
wokao_wg:
大伙可以看看作者说的http://www.sencha.com ...
ExtJs 中的TreeGrid(Ext.ux.maximgb.tg.EditorGridPanel详解与其他问题解决办法)2
同一目錄下config.xml:
<?xml version="1.0" encoding="UTF-8"?> <config index="0"> <backup> <whether>0</whether> <!-- wheter download --> <ServerName>10.1.100.13</ServerName> <UserName>test</UserName> <PassWord>test123</PassWord> <DataBase>BarCode09</DataBase> <BackupObject>2;5</BackupObject><!-- All Objects = 0; Tables = 1; Table Triggers = 2; Views = 3; Functions = 4; Stored Procedures = 5; --> <WildCard>el_mp*;el_get_date</WildCard> </backup> <replace> <BackupFolder>D:\sp\masterplan\backup</BackupFolder> <whether>1</whether> <!--whether replace --> <newChar>live.dbo.</newChar> <oldChar>uat18.dbo.</oldChar> <ReplaceFolder>D:\sp\masterplan\replace</ReplaceFolder> </replace> <restore> <whether>0</whether> <!--whether upload/restore --> <ServerName>10.1.100.58</ServerName> <UserName>sa</UserName> <PassWord>sa123</PassWord> <DataBase>stu</DataBase> </restore> </config>
PowerShell腳本如下:
[String]$CurrentPath = get-location; [String]$xmlDocPath = $CurrentPath+"\config.xml"; #write-host $xmlDocPath $xmlDoc = New-Object "System.Xml.XmlDocument" $xmlDoc.Load($xmlDocPath) $modelList = $xmlDoc.GetElementsByTagName("config");#tagName $index=$modelList.ItemOf(0).GetAttribute("index") if($index -eq 0 ) { #-------------------Start Param----------------- $backupNodelist=$xmlDoc.GetElementsByTagName("backup"); $backupConfig=$backupNodelist.ItemOf(0); #first <backup> node #below is backup tag attributes $whetherBackup=$backupConfig.GetElementsByTagName("whether").ItemOf(0).get_InnerXml(); $backupServer=$backupConfig.GetElementsByTagName("ServerName").ItemOf(0).get_InnerXml(); $backupLogin=$backupConfig.GetElementsByTagName("UserName").ItemOf(0).get_InnerXml(); $backupPassWord=$backupConfig.GetElementsByTagName("PassWord").ItemOf(0).get_InnerXml(); $backupDataBase=$backupConfig.GetElementsByTagName("DataBase").ItemOf(0).get_InnerXml(); $backupObject=$backupConfig.GetElementsByTagName("BackupObject").ItemOf(0).get_InnerXml(); $backupWildCard=$backupConfig.GetElementsByTagName("WildCard").ItemOf(0).get_InnerXml(); $replaceNodelist=$xmlDoc.GetElementsByTagName("replace"); $replaceConfig=$replaceNodelist.ItemOf(0); #first <replace> node #below is replace tag attributes $backupFolder=$replaceConfig.GetElementsByTagName("BackupFolder").ItemOf(0).get_InnerXml(); $whetherReplace=$replaceConfig.GetElementsByTagName("whether").ItemOf(0).get_InnerXml(); $newChar=$replaceConfig.GetElementsByTagName("newChar").ItemOf(0).get_InnerXml(); $oldChar=$replaceConfig.GetElementsByTagName("oldChar").ItemOf(0).get_InnerXml(); $replaceFolder=$replaceConfig.GetElementsByTagName("ReplaceFolder").ItemOf(0).get_InnerXml(); $restoreNodelist=$xmlDoc.GetElementsByTagName("restore"); $restoreConfig=$restoreNodelist.ItemOf(0); #first <restore> node #below is restore tag attributes $whetherRestore=$restoreConfig.GetElementsByTagName("whether").ItemOf(0).get_InnerXml(); $restoreServer=$restoreConfig.GetElementsByTagName("ServerName").ItemOf(0).get_InnerXml(); $restoreLogin=$restoreConfig.GetElementsByTagName("UserName").ItemOf(0).get_InnerXml(); $restorePassWord=$restoreConfig.GetElementsByTagName("PassWord").ItemOf(0).get_InnerXml(); $restoreDataBase=$restoreConfig.GetElementsByTagName("DataBase").ItemOf(0).get_InnerXml(); #-------------------End Param---------------------- #Load Class #cls #load assemblies [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null #Need SmoExtended for backup [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | Out-Null [Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo") | Out-Null [Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum") | Out-Null $serverConn=new-object Microsoft.SqlServer.Management.Common.ServerConnection $serverConn.ServerInstance=$backupServer $serverConn.LoginSecure =$false $serverConn.Login = $backupLogin $serverConn.Password =$backupPassWord #trap errors(catch error ) $errors =$backupFolder+"\errors.txt" trap { "______________________" | out-file $errors -append; "ERROR SCRIPTING TABLES" | out-file $errors -append; get-date | out-file $errors -append; "ERROR: " + $_ | out-file $errors -append; "`backupServer = $backupServer" | out-file $errors -append; "`backupDataBae = $backupDataBase" |out-file $errors -append; "`backupFolder = $backupFolder" | out-file $errors -append; "`restoreServer = $restoreServer" | out-file $errors -append; "`restoreDataBae = $restoreDataBase " | out-file $errors -append; #throw "ERROR: See $errors" } function MakeDirectory { param([string]$DirName) Process { if (!(Test-Path -path $DirName)) { New-Item $DirName -type directory | Out-Null } } } $srv = new-object Microsoft.SqlServer.Management.Smo.Server($serverConn) $db=$srv.databases[$backupDataBase] $dbName=$db.Name $scr = New-Object "Microsoft.SqlServer.Management.Smo.Scripter" $scr.Server = $srv $scrOptions = New-Object "Microsoft.SqlServer.Management.SMO.ScriptingOptions" #Script Options Setting $scrOptions.AllowSystemObjects = $false #not include system object $scrOptions.IncludeDatabaseContext = $false #not include database infomation $scrOptions.IncludeIfNotExists = $true $scrOptions.ClusteredIndexes = $true $scrOptions.NoCollation = $true $scrOptions.Default = $true #$scrOptions.DriAll = $true $scrOptions.Indexes = $true $scrOptions.NonClusteredIndexes = $true $scrOptions.IncludeHeaders = $false $scrOptions.ToFileOnly = $true $scrOptions.Permissions = $true $scrOptions.ScriptDataCompression = $true $scrOptions.ScriptSchema = $true $scrOptions.AppendToFile = $true $sEnc = [System.Text.Encoding]::UTF8 $scrOptions.Encoding=$sEnc $scr.Options = $scrOptions if($whetherBackup -eq 1){ if (Test-Path -path $backupFolder){ remove-item $backupFolder\*.* -Recurse } } if($whetherReplace -eq 1){#remove replaceFolder before replace if (Test-Path -path $replaceFolder){ remove-item $replaceFolder\*.* -Recurse } } $WildCard=""; function getWildCard{ param($obj) $WildCard=""; foreach($element in $backupWildCard.split(';')) { $WildCard+="'"+$obj+"' -like '"+$element+"' -or " } $WildCard=$WildCard.substring(0,$WildCard.length-5) invoke-expression $WildCard } function ScriptDropStatement { param($object) $scrOptions.ScriptDrops = $true # drop statement of sql MakeDirectory($backupFolder); $scrOptions.FileName = $backupFolder+"\"+$object.Name+".sql"; $scr.Options = $scrOptions; $scr.Script($object); } function ScriptCreateStatement { param($object) $scrOptions.ScriptDrops = $false #create statement for sql MakeDirectory($backupFolder); $scrOptions.FileName = $backupFolder+"\"+$object.Name+".sql"; $scr.Options = $scrOptions; $scr.Script($object); } if($whetherBackup -eq 1){ #If want to backup ">>>>>Start Backup[Download] the Database Script !!!" $multiObject = $backupObject.split(";"); foreach($obj in $multiObject) { $tables=$db.Tables; if ($obj -eq 0 -or $obj -eq 1) { $srv.SetDefaultInitFields([Microsoft.SqlServer.Management.SMO.Table],"IsSystemObject") foreach ($table in $tables | where-object {getWildCard($_.name)} | where-object{$_.IsSystemObject -eq $false}) { MakeDirectory ($backupFolder); #ScriptDropStatement($table) ScriptCreateStatement($table) write-host "Table "$table.Name" backup Complete !" } } if ($obj -eq 0 -or $obj -eq 2) { $srv.SetDefaultInitFields([Microsoft.SqlServer.Management.SMO.Trigger],"IsSystemObject") foreach ($table in $tables) { foreach ($trigger in $Table.Triggers |where-object {getWildCard($_.name)} | where-object{$_.IsSystemObject -eq $false}) { MakeDirectory ($backupFolder); ScriptDropStatement($trigger) ScriptCreateStatement($trigger) write-host "Trigger "$trigger.Name" backup Complete !" } } } if ($obj -eq 0 -or $obj -eq 3) { $srv.SetDefaultInitFields([Microsoft.SqlServer.Management.SMO.View],"IsSystemObject") foreach ($view in $db.Views | where-object { getWildCard($_.name)} | where-object {$_.IsSystemObject -eq $false} ) { MakeDirectory ($backupFolder); #Check for folder, and create if needed ScriptDropStatement($view) ScriptCreateStatement($view) write-host "View "$view.Name" backup Complete !" } } if ($obj -eq 0 -or $obj -eq 4) { $srv.SetDefaultInitFields([Microsoft.SqlServer.Management.SMO.UserDefinedFunction],"IsSystemObject") foreach ($function in $db.UserDefinedFunctions | where-object {getWildCard($_.name)} | where-object {$_.IsSystemObject -eq $false}) { MakeDirectory ($backupFolder); #Check for folder, and create if needed ScriptDropStatement($function) ScriptCreateStatement($function) write-host "Function "$function.Name" backup Complete !" } } if ($obj -eq 0 -or $obj -eq 5) { $srv.SetDefaultInitFields([Microsoft.SqlServer.Management.SMO.StoredProcedure],"IsSystemObject") foreach ($procedure in $db.StoredProcedures | where-object {getWildCard($_.name)} | where-object{$_.IsSystemObject -eq $false}) { MakeDirectory ($backupFolder); #Check for folder, and create if needed ScriptDropStatement($procedure) ScriptCreateStatement($procedure) write-host "Stored Procedure "$procedure.Name" backup Complete !" } } } ">>>>>>>>Backup[Download] the Database Script Complete !!!" } if($whetherReplace -eq 1){#If want to replace ">>>>>>>>Start Replace The BackupFolder's Script !" if(!(Test-Path -path $backupFolder)){ write-host "Information: "$backupFolder" Directory not exist! Please Backup Before !" }else{ MakeDirectory($replaceFolder);#Check replaceFolder whether exist! $file=Get-ChildItem $backupFolder; if($oldChar -eq "" -and $newChar -eq "") { write-host "No replace necearray !" }else{ foreach($str in $file) { $backfileAddr=$backupFolder+"\"+$str; if(Test-Path -path $backfileAddr){ $replacefileAddr=$replaceFolder+"\"+$str; write-host $replacefileAddr; $content=Get-Content -path $backfileAddr -Encoding UTF8 $content | foreach-object {$_ -replace $oldChar,$newChar} | Set-Content $replacefileAddr -force -Encoding UTF8 }else{ write-host "file "$backfileAddr" can't found! So the replace Action can't work !" } } } } ">>>>>>Replace Script Complete !" } if($whetherRestore -eq 1){#If want to restore ">>>>>>>Start Restore Database !" if(!(Test-Path -path $replaceFolder)){ write-host "Information: "$replaceFolder "Directory not exist !please replace Before" }else{ foreach($f in get-childitem -path $replaceFolder -Filter *.sql | sort-object) { sqlcmd -i $f.fullname -S $restoreServer -U $restoreLogin -P $restorePassWord -d $restoreDataBase } } ">>>>>>>Restore DataBase Complete !" } if($whetherBackup -eq 0 -and $whetherReplace -eq 0 -and $whetherRestore -eq 0){ ">>>>>>You don't let progress do anything !<<<<<<" } }
同一目錄下的批處理文件run.bat:
cls powershell -command "&{set-executionpolicy RemoteSigned }" powershell -command "&{.\beta.ps1 Club.Secretary}"
- database.rar (3.5 KB)
- 下载次数: 2
发表评论
-
PowerDesigner连接数据库导出ER图
2012-06-28 18:16 3175PowerDesigner建立与数据库的连接,以便生成数 ... -
各类数据库中的SQL Profiler
2012-01-12 15:52 2061SQL Profiling and ... -
表变量与临时表
2011-12-27 09:51 935SQL Server中的临时表和表变量 ... -
sqlserver的换行问题
2011-09-20 20:29 1995制表符: CHAR(9) 换行符: CHAR(10) ... -
SQLServer脚本一键备份与还原(可配置文件及多通配符支持...)
2011-05-31 17:22 3116需求: 需要数据库中的对象(eg: Table 、 T ... -
在SQL Server中将数据库表结构及全部数据导成脚本
2011-05-20 15:24 2018当我们想将数据库搬到另一个环境中运行时,如 ... -
SMO+PowerShell 实现SQLServer数据库的备份与还原
2011-05-17 21:31 3502最近有需求,需要用脚 ... -
SQL中的DateAdd()
2011-04-18 22:04 3771MS SQL Server中DATEADD和DATEDIF ... -
一个表写给另一个表的情书!
2011-02-09 22:01 1198亲爱的Mrs TableMM: ... -
SQL Server datetime 常用日期格式转换
2010-12-20 20:59 1511我们经常出于某种目的需要使用各种各样的日期格式,当然我们可以使 ... -
SQL Server 2005启用sa账号
2010-11-11 09:35 1313... -
PIVOT初接触
2010-10-28 20:33 1104以前一直没接触过PIVOT这个概念!当接触时,赶紧查了下资料如 ... -
常用函数
2010-10-28 15:40 987SQL函数 left()、charindex()的使用 ... -
关于SQL中自动增长列值回复(还原)的问题
2010-10-23 14:13 4415大家都知道,只要有新加记录,SQL中自动增长字段的值总是自动 ... -
数据库中涉及到的几种连接方式
2010-10-14 19:43 1278通俗的讲: A left join B ...
相关推荐
总结来说,MOSS网站的跨域还原是一项复杂但至关重要的任务,它涉及到SharePoint的备份与恢复机制、用户映射、IIS配置调整等多个方面。理解和掌握这一技术,可以有效地支持SharePoint环境的迁移和恢复工作,保证组织...
这通常需要手动备份OWA的设置,以便在恢复后还原。微软的TechNet文章提供了关于如何备份和恢复这些配置的详细信息。 总的来说,Exchange 2007单服务器灾难恢复是一个复杂但至关重要的过程,需要精心规划和执行,以...
40. ICACLS:显示、修改、备份或还原文件和目录的ACL,用于更详细的权限管理。 41. IF:在批处理程序中执行有条件的处理操作,用于逻辑判断。 42. LABEL:创建、更改或删除磁盘的卷标,用于磁盘卷标的管理。 43. ...
scratch少儿编程逻辑思维游戏源码-米克 demo.zip
scratch少儿编程逻辑思维游戏源码-萝卜男孩拯救世界.zip
scratch少儿编程逻辑思维游戏源码-酷忍者.zip
教育科技_微信小程序_二手交易平台_大学校园二手书与物品循环利用公益系统_风华读书人校园二手交易平台_基于C2C模式的校内闲置物品交易系统_支持多校区独立运营的二手书交易平台_包含
全新UI彩虹外链网盘系统源码前后端美化模板整站 模版文件.zip
maoxig_nonebot-plugin-ai-timetable_32152_1745865455265
少儿编程scratch项目源代码文件案例素材-足球顶尖高手.zip
少儿编程scratch项目源代码文件案例素材-作战基地.zip
少儿编程scratch项目源代码文件案例素材-云端之上 1-4名玩家.zip
scratch少儿编程逻辑思维游戏源码-魔幻之塔.zip
scratch少儿编程逻辑思维游戏源码-楼层酷跑.zip
scratch少儿编程逻辑思维游戏源码-圈.zip
少儿编程scratch项目源代码文件案例素材-纸片马里奥自定义战役.zip
少儿编程scratch项目源代码文件案例素材-自由下落.zip
少儿编程scratch项目源代码文件案例素材-阻击蜈蚣.zip
健康监测与疾病预防_脉搏波分析_六轴加速度传感器_生理参数融合_STC12硬件采集_Android数据处理_SpringBoot后端_MySQL数据库_MatlabPython实验