Tài liệu SQL Server MVP Deep Dives- P11 pdf

40 439 0
Tài liệu SQL Server MVP Deep Dives- P11 pdf

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

356 CHAPTER 27 Automating SQL Server Management using SMO code can then be executed every time your backup strategy calls for a full database backup of all databases on the target server If you remove the full database backup logic from the code sample the code can be run every time transaction log backups are to be run for all databases on the server, improving your recovery interval to minimize the potential data loss in the event of a system failure In the following script, shown in listing 2, we’ll connect to our target server, and then get the default backup directory from the Settings collection We’ll next grab the database collection and iterate through that to the backups For each database we’ll get the current date and time and put it into a string to use in the backup filename We’ll the full backup for the database, then we’ll check to see if the database recovery model is Simple If not, we’ll perform a transaction log backup on the database as well Listing Backup Action BackupSetDescription BackupSetName Database MediaDescription Devices BackupDevice AddDevice SqlBackup Figure The SMO Backup object Backing up user databases #backup.ps1 #Performs a Full backup followed by a transaction log backup on all user ➥databases param ( [string]$srvname='MyServer\MyInstance' ) # Load SMO assembly, and if we're running SQL 2008 DLLs load the ➥SMOExtended and SQLWMIManagement libraries $v = [System.Reflection.Assembly]::LoadWithPartialName ➥('Microsoft.SqlServer.SMO') $p = $v.FullName.Split(',') $p1 = $p[1].Split('=') $p2 = $p1[1].Split('.') if ($p2[0] -ne '9') { [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer ➥SMOExtended') | out-null [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer ➥SQLWMIManagement') | out-null } $s = new-object ('Microsoft.SqlServer.Management.Smo.Server') $srvname $bkdir = $s.Settings.BackupDirectory $dbs = $s.Databases foreach ($db in $dbs) { if ($db.IsSystemObject -eq $False -and $db.IsMirroringEnabled -eq $False) ➥{ Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark Licensed to Kerri Ross Restore 357 $dbname = $db.Name $dt = get-date -format yyyyMMddHHmmss $bk = new-object ('Microsoft.SqlServer.Management.Smo.Backup') $bk.Action = 'Database' $bk.BackupSetDescription = "Full backup of " + $dbname $bk.BackupSetName = $dbname + " Backup" $bk.Database = $dbname $bk.MediaDescription = "Disk" $bk.Devices.AddDevice($bkdir + "\" + $dbname + "_db_" + $dt + ".bak", ➥'File') $bk.SqlBackup($s) # Simple Recovery Model has a Value Property of # SQL Server 2008 doesn't recognize the enumerated value so the code is ➥slightly different # Set a variable to run the transaction log backup, and if Simple, turn ➥it off $trnbck = if ($p2[0] -eq '9') { if ($db.DatabaseOptions.RecoveryModel -eq 'Simple') { $trnbck = ➥0 } } else { if ($db.RecoveryModel.value -eq 3) { $trnbck = } } if ($trnbck -eq 1) { $dt = get-date -format yyyyMMddHHmmss $trn = new-object ('Microsoft.SqlServer.Management.Smo.Backup') $trn.Action = 'Log' $trn.BackupSetDescription = "Trans Log backup of " + $dbname $trn.BackupSetName = $dbname + " Backup" $trn.Database = $dbname $trn.MediaDescription = "Disk" $trn.Devices.AddDevice($bkdir + "\" + $dbname + "_tlog_" + $dt ➥+ ".trn", 'File') $trn.SqlBackup($s) } } } Restore All the backups in the world don’t a bit of good if they can’t be restored, and there are scenarios that require restores to be done One is where a server or disk drive fails and the data needs to be recovered for business to continue This is the primary reason we perform regular backups For this case, the backup is restored (generally) to the same location where the original database files existed In addition to this there may be the case where data is inadvertently modified or deleted, and some alternate recovery method is required, usually restoring the database backup to a new database name, so that the original data can be copied to the production database without disturbing other transactional activity occurring Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark Licensed to Kerri Ross 358 CHAPTER 27 Automating SQL Server Management using SMO Another use is for development and Quality Assurance (QA) testing, where a copy of the production database is restored in the development or QA environment to examine the effect of some application update Finally, and this is often overlooked, database backups should be regularly tested in disaster recovery testing, to be certain that, should a problem occur, the backups are in fact usable A valuable piece of information is available to us via SMO for the restore process that isn’t available through Management Studio or through straight T-SQL, and that is the location of the default data and log file paths (figure 3) We can use this in our restore scenario, using the following objects When we have this information, we can set the properties of the SMO Restore object (figure 4) We first connect to the server, then we create a BackupDeviceItem, specifying the name and path of the backup file we’re going to use, and add that to the Devices collection of the Restore object We need to create at least two RelocateFile objects (more if there are more logical files in the backup file) and add them to the RelocateFiles collection These RelocateFile objects will allow us to specify both the LogicalFileName and the PhysicalFileName properties of the new database In the PhysicalFileName properties, we’ll use the MasterDBPath and MasterDBLogPath properties from the server information shown previously Figure shows the object hierarchy for the Restore object As shown in listing 3, after we’ve set the properties, we can invoke the SqlRestore method to perform the restore; then the restored database is available for use Listing Server Information MasterDBPath MasterDBLogPath Settings DefaultFile DefaultLog Figure Database file path properties Restore Action Database Devices BackupDeviceItem Add RelocateFiles RelocateFile LogicalFileName PhysicalFileName SqlRestore Figure The SMO Restore object Restoring a copy of an existing database from backup #restore.ps1 #Restores a database with a new name from an existing backup param ( Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark Licensed to Kerri Ross Creating a database 359 [string]$srvname='MyServer\MyInstance', [string]$dbname='AdWorks', [string]$bckfile='C:\MSSQL.2\MSSQL\Backup\AdventureWorks_db_20071227175 ➥004.bak' ) # Load SMO assembly, and if we're running SQL 2008 DLLs load the and SQLWMIManagement libraries $v = [System.Reflection.Assembly]::LoadWithPartialName ➥('Microsoft.SqlServer.SMO') $p = $v.FullName.Split(',') $p1 = $p[1].Split('=') $p2 = $p1[1].Split('.') if ($p2[0] -ne '9') { [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer ➥SMOExtended') | out-null [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer ➥SQLWMIManagement') | out-null } ➥SMOExtended $srv = new-object ('Microsoft.SqlServer.Management.Smo.Server') $srvname $bdi = new-object ('Microsoft.SqlServer.Management.Smo.BackupDeviceItem') ($bckfile, 'File') $restr = new-object('Microsoft.SqlServer.Management.Smo.Restore') $restr.Database = $dbname $restr.Devices.Add($bdi) $restrfile = new-object('Microsoft.SqlServer.Management.Smo.RelocateFile') $restrlog = new-object('Microsoft.SqlServer.Management.Smo.RelocateFile') $restrfile.LogicalFileName = "AdventureWorks_Data" $restrfile.PhysicalFileName = $s.Information.MasterDBPath + '\'+ $dbname + ➥'_Data.mdf' $restrlog.LogicalFileName = "AdventureWorks_Log" $restrlog.PhysicalFileName = $s.Information.MasterDBLogPath + '\'+ $dbname ➥+ '_Log.ldf' $restr.RelocateFiles.Add($rsfile) $restr.RelocateFiles.Add($rslog) $restr.SqlRestore($srv) ➥ Creating a database Using SMO to create databases and database objects may seem counterintuitive, because these objects are usually created using T-SQL scripts, but automating the processes that create the objects can provide consistency in an area that is usually quite inconsistent Let’s start with the database itself SQL Server requires that a database have a PRIMARY filegroup and that the system tables (the database metadata) reside in that filegroup (in fact their location cannot be changed) Best practices recommendations include keeping your application data out of the PRIMARY filegroup, to help in managing the disk files for the database When using SSMS, it can be tedious to create a database with the desired size, file location, and with a separate, default, filegroup to hold the application data This is a relatively simple process with SMO Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark Licensed to Kerri Ross 360 CHAPTER 27 Automating SQL Server Management using SMO For the example database, we’ll Server create a database called MyAppDB, Information which will have a MB file in the PRIMasterDBPath MARY filegroup to hold the database MasterDBLogPath metadata This file should never grow beyond MB because it conDatabases tains only database metadata We’ll Database use the logical name MyAppDB_ FileGroups SysData for this file and house it in the default data path for the server FileGroup The application data will be IsDefault located in a second filegroup called Files AppFG, which we’ll set as the default DataFile filegroup for the database We’ll create one file with a logical name Name MyAppDB_AppData and house it in FileName the default data path for the server GrowthType as well We’ll set an initial size of 25 Growth MB and allow it to grow by 25 MB each time it is required, but set a MaxSize maximum size of 100 MB Alter Log files in SQL Server not use LogFiles filegroups, so we’ll add a log file to the LogFiles collection of the dataLogFile base with a logical name MyAppDB_ Name Log and house it in the default log FileName file path for the server We’ll set its GrowthType initial size to 10 MB and allow it to grow by 10 MB each time it needs to Growth so, but we won’t set a maximum MaxSize size for the log file Create After we’ve created the structural Alter objects for the database, we execute the Create method, but SQL Server Figure The SMO Databases collection and automatically sets the default file- Database object group to PRIMARY when a database is created, so we have to go back in and set the default filegroup to AppFG using the Alter method at both the filegroup and database levels Figure is a hierarchical diagram of the objects we’ll use to create the database Now let’s look at the example code in listing Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark Licensed to Kerri Ross Creating a database Listing 361 Creating a user database #createdatabase.ps1 #Creates a new database using our specifications param ( [string]$srvname='MyServer\MyInstance', [string]$dbname='MyAppDB', [int]$datsize=25, [int]$maxsize=100, [int]$logsize=10 ) # Load SMO assembly, and if we're running SQL 2008 DLLs load the ➥SMOExtended and SQLWMIManagement libraries $v = [System.Reflection.Assembly]::LoadWithPartialName ➥('Microsoft.SqlServer.SMO') $p = $v.FullName.Split(',') $p1 = $p[1].Split('=') $p2 = $p1[1].Split('.') if ($p2[0] -ne '9') { [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer ➥SMOExtended') | out-null [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer ➥SQLWMIManagement') | out-null } $srv = new-object ('Microsoft.SqlServer.Management.Smo.Server') $srvname # Instantiate the database object and add the filegroups $db = new-object ('Microsoft.SqlServer.Management.Smo.Database') ($srv, ➥$dbname) $sysfg = new-object ('Microsoft.SqlServer.Management.Smo.FileGroup') ($db, ➥'PRIMARY') $db.FileGroups.Add($sysfg) $appfg = new-object ('Microsoft.SqlServer.Management.Smo.FileGroup') ($db, ➥'AppFG') $db.FileGroups.Add($appfg) # Create the file for the system tables $dbdsysfile = new-object ('Microsoft.SqlServer.Management.Smo.DataFile') ➥ ($sysfg, 'MyAppDB_SysData') $sysfg.Files.Add($dbdsysfile) $dbdsysfile.FileName = $srv.Information.MasterDBPath + ➥'\MyAppDB_SysData.mdf' $dbdsysfile.Size = [double](5.0 * 1024.0) $dbdsysfile.GrowthType = 'KB' $dbdsysfile.Growth = [double](5.0 * 1024.0) $dbdsysfile.IsPrimaryFile = 'True' # Create the file for the Application tables $dbdappfile = new-object ('Microsoft.SqlServer.Management.Smo.DataFile') ➥ ($appfg, 'MyAppDB_AppData') $appfg.Files.Add($dbdappfile) $dbdappfile.FileName = $srv.Information.MasterDBPath + ➥'\MyAppDB_AppData.ndf' $dbdappfile.Size = [double](25.0 * 1024.0) $dbdappfile.GrowthType = 'KB' Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark Licensed to Kerri Ross 362 CHAPTER 27 Automating SQL Server Management using SMO $dbdappfile.Growth = [double]($datsize * 1024.0) $dbdappfile.MaxSize = [double]($maxsize * 1024.0) # Create the file for the log $dblfile = new-object ('Microsoft.SqlServer.Management.Smo.LogFile') ($db, ➥'MyAppDB_Log') $db.LogFiles.Add($dblfile) $dblfile.FileName = $srv.Information.MasterDBLogPath + '\MyAppDB_Log.ldf' $dblfile.Size = [double]($logsize * 1024.0) $dblfile.GrowthType = 'KB' $dblfile.Growth = [double]($logsize * 1024.0) # Create the database $db.Create() # Set the default filegroup to AppFG $appfg = $db.FileGroups['AppFG'] $appfg.IsDefault = $true $appfg.Alter() $db.Alter() After this script is completed, the MyAppDB database will exist and user objects will be placed in the AppFG filegroup instead of PRIMARY, which will improve the long-term management of the database Scripting The scripting of SMO is a vast improvement over the scripting of its predecessor, DMO With SMO you can create T-SQL scripts from objects even if they don’t yet exist When you open almost any maintenance dialog box in SQL Server Management Studio, you’ll see a button that allows you to generate a script from the changes you’ve made in that dialog box You can then save that script for archival purposes, cancel out of the dialog box, and execute the script as written, or make changes to it before you execute it Another useful feature of scripting existing objects is to generate scripts of all database objects for documentation or source code control This allows the administrators to then rebuild a database in the form it existed at the time the script was created At any time while creating or working with objects in SMO, you can script those objects for archival or later use Figure shows the Scripter object and the properties we need to set The Server property allows the Scripter object to connect to the server The remaining properties that need to be set are in the Scripter Options collection Scripter Server Options ScriptDrops WithDependencies FileName IncludeHeaders AppendToFile ToFileOnly ClusteredIndexes Indexes DriAll Script Figure object The SMO Scripter Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark Licensed to Kerri Ross Scripting 363 The ScriptDrops property specifies whether the script will consist of drops for the objects or create for the objects If you specify c to this property, the script will contain a DROP statement for the object (within an IF condition to ensure that it exists), but a False value will cause the scripter to generate the CREATE statement for the object The WithDependencies property, if True, will cause the objects to be scripted in an order that respects the dependency of one scripted object on another The FileName property contains the full path of the resultant script file The IncludeHeaders property, when True, will include a comment indicating the name of the object and when the script was created in the script The AppendToFile will append the script to the end of an existing file if True, and overwrite the file if False By default the scripting process will send the results to the console, so setting the ToFileOnly to True will cause the scripter to send the script only to the file specified Setting ClusteredIndexes to True will cause the clustered index for a table to be included in the script, and setting Indexes to True will cause the nonclustered indexes to be included in the script The DriAll property, when set to True, will cause all objects with enforced declarative referential integrity to be included in the script The objects to be scripted need to be added to an array of type SqlSmoObject This allows you to decide at what point you want the object included in the script After the array has been populated with all the objects to be scripted you can invoke the Script method and the script will be created Now let’s look at the example code in listing Listing Scripting all objects in the AdventureWorks database #scripting.ps1 #Script all the table objects in the AdventureWorks database param ( [string]$srvname='MyServer\MyInstance', [string]$dbname='AdventureWorks', [string]$scrname='c:\dbscript.sql' ) # Load SMO assembly, and if we're running SQL 2008 DLLs load the ➥SMOExtended and SQLWMIManagement libraries $v = [System.Reflection.Assembly]::LoadWithPartialName ➥('Microsoft.SqlServer.SMO') $p = $v.FullName.Split(',') $p1 = $p[1].Split('=') $p2 = $p1[1].Split('.') if ($p2[0] -ne '9') { [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer ➥SMOExtended') | out-null [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer ➥SQLWMIManagement') | out-null } $srv = new-object ('Microsoft.SqlServer.Management.Smo.Server') $srvname $db = $srv.Databases[$dbname] $scrp = new-object ('Microsoft.SqlServer.Management.Smo.Scripter') ($srv) Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark Licensed to Kerri Ross 364 CHAPTER 27 Automating SQL Server Management using SMO $scrp.Options.ScriptDrops = $False $scrp.Options.WithDependencies = $True $scrp.Options.FileName = $scrname $scrp.Options.IncludeHeaders = $True $scrp.Options.AppendToFile = $True $scrp.Options.ToFileOnly = $True $scrp.Options.ClusteredIndexes = $True $scrp.Options.DriAll = $True $scrp.Options.Indexes = $True $scrp.Script($db.Tables) Summary This chapter presents just a few examples of methods for automating your management of SQL Server using SMO and PowerShell You can find many additional examples on the web and on the Codeplex site to help you solve your SQL Server management problems You’ll also find additional examples in my blog at http://sqlblog.com/blogs/allen_white/default.aspx About the author Allen White is a SQL Server trainer and consultant who’s been using SQL Server since 1992 He has been awarded Microsoft’s MVP Award for his work in the SQL Server community for three years Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark Licensed to Kerri Ross 28 Practical auditing in SQL Server 2008 Jasper Smith In SQL Server 2008 Enterprise Edition, instance- and database-level audit is now built into the Database Engine with its own set of instance- and database-level objects—Server Audit and Server Audit Specification at the instance level, and Database Audit Specification at the database level SQL Server 2005 introduced event notifications and data definition language (DDL) triggers as mechanisms for auditing DDL statements, but coverage of events wasn’t complete There was no support for auditing access to data, and there was no tool support available in SQL Server Management Studio (SSMS) Generating audit event s in SQL 2008 is extremely lightweight compared to previously available mechanisms, and is based on the new extended events infrastructure, which is designed to have an extremely low overhead even for large numbers of events It also allows much finer-grained event filtering NOTE All of the new audit features described in this chapter require SQL Server 2008 Enterprise or Developer Edition, and aren’t available in lower editions Overview of audit infrastructure In SQL Server 2008, all events are now auditable using the new audit objects, including those not available via event notifications in previous versions of SQL Server Configuration is greatly simplified with built-in tool support in SSMS Figure gives an overview of the various audit objects Server audit objects You can define the properties of an audit, such as Queue Delay or Action on Audit Failure, as well as the output target, such as File, Windows Application Log, or Windows Security Log You can create multiple server audits, each of which defines its own target 365 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark Licensed to Kerri Ross ... [System.Reflection.Assembly]::LoadWithPartialName(''Microsoft.SqlServer ➥SQLWMIManagement'') | out-null } ➥SMOExtended $srv = new-object (''Microsoft.SqlServer.Management.Smo .Server'' ) $srvname $bdi = new-object (''Microsoft.SqlServer.Management.Smo.BackupDeviceItem'')... [System.Reflection.Assembly]::LoadWithPartialName(''Microsoft.SqlServer ➥SQLWMIManagement'') | out-null } $srv = new-object (''Microsoft.SqlServer.Management.Smo .Server'' ) $srvname $db = $srv.Databases[$dbname] $scrp = new-object (''Microsoft.SqlServer.Management.Smo.Scripter'')... [System.Reflection.Assembly]::LoadWithPartialName(''Microsoft.SqlServer ➥SQLWMIManagement'') | out-null } $srv = new-object (''Microsoft.SqlServer.Management.Smo .Server'' ) $srvname # Instantiate the database object

Ngày đăng: 15/12/2013, 13:15

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan