1. Using Automation to Simplify SQL Server Management Greg RobidouxEdgewood Solutionsgregr@edgewoodsolutions.com Bullet ManaleIderawww.idera.com
2. Idera Solutions for SQL Server Performance & Availability Compliance & Security Change Management Backup & Recovery Administration
3. Agenda Why Automate What to Automate How to Automate SQL Server Tools OtherTools Questions / Wrap Up 3
4. Why automate Redundant tasks Only want to know when there are issues Automatic recovery Reduce manual steps Create a repeatable process 4
5. What to automate Backups Integrity Checks Index Maintenance Link Server Status Free Disk Space Transaction Log Size Replication Alerts Database Mirroring Alerts Reading SQL Server Error Log Scripts to Create Scripts High Availability Gathering Performance Statistics Trace Perfmon DMVs 5
6. What to collect Backups – failed and successful Replication Errors Maintenance Tasks Failed Logins SQL Server Errors Server Status 6
7. Process Setup Setup key components such as Database Mail, Operators, etc… Scripts Create scripts or sets of code to gather data Automate Schedule process to run and collect data. This could be something you schedule or something that occurs automatically within SQL Server. Analyze Analyze data and determine what to do. Notify Send notification to either a person or another process. 7
12. Maintenance Plans Allows you to automate routine tasks such as: Backups Index maintenance Integrity checks Cleanup tasks Creates SSIS Packages Limited control 12
13. Maintenance Plan Tasks Backup Database Task Check Database Integrity Task Execute SQL Server Agent Task Execute T-SQL Statement Task History Cleanup Task Maintenance Cleanup Task Notify Operator Task Rebuild Index Task Reorganize Index Task Shrink Database Task Update Statistics Task 13
16. Scripting More work, but gives you more control. Stored Procedures (T-SQL or CLR) SSIS Packages VBScript VB.Net or C# PowerShell SMO (SQL Management Objects) SQLCMD Use of DMVs 16
17. Scripting Examples In addition to Maintenance Plans Check Free Disk Space Transaction Log Usage Last Backup Info Reading Error Logs Selectively Rebuild or Reorganize Indexes Scheduled Job Failures Backup Failures Login Failures Scripts that Generate Scripts 17
18. Backup All Databases Script DECLARE @name VARCHAR(50) -- database name DECLARE @path VARCHAR(256) -- path for backup files DECLARE @fileName VARCHAR(256) -- filename for backup DECLARE @fileDate VARCHAR(20) -- used for file name SET @path = 'C:ackupamp;apos; SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112) DECLARE db_cursor CURSOR FOR SELECT name FROM master.dbo.sysdatabases WHERE name NOT IN ('master','model','msdb','tempdb') OPEN db_cursor FETCH NEXT FROM db_cursor INTO @name WHILE @@FETCH_STATUS = 0 BEGIN SET @fileName = @path + @name + '_' + @fileDate + '.BAK' BACKUP DATABASE @name TO DISK = @fileName FETCH NEXT FROM db_cursor INTO @name END CLOSE db_cursor DEALLOCATE db_cursor http://www.mssqltips.com/tip.asp?tip=1070 18
19. Log Space Usage Script CREATE TABLE ##logspace (databaseNamesysname, logsize decimal(10,5), logused decimal(10,5), status int) INSERT INTO ##logspace EXEC ('dbccsqlperf(logspace)') EXEC msdb.dbo.sp_send_dbmail @profile_name = 'SQLMail Profile', @recipients = 'gregr@edgewoodsolutions.com', @query = 'SELECT * FROM ##logspace WHERE logused > 75' , @subject = 'Log Space Usage‘ DROP TABLE ##logspace 19
20. Free Drive Space Script Use sys.xp_fixeddrives CREATE TABLE #drivespace (drive varchar(20), freespacebigint) INSERT INTO #drivespace EXEC sys.xp_fixeddrives SELECT * FROM #drivespace WHERE freespace < 10000 DROP TABLE #drivespace 20
21. Scripts that Generate Scripts Use system Meta Data Stored procedures Index drops and creations Constraints drops and creations Create insert statements SSMS Scripting http://www.mssqltips.com/tip.asp?tip=1376 http://vyaskn.tripod.com/code.htm#inserts 21
22. Reading Error Logs Type of System Logs Database Mail SQL Agent SQL Server Windows 22
23. SP_ReadErrorLog EXEC sp_readerrorlog Parameters Value of error log file you want to read: 0 = current, 1 = Archive #1, 2 = Archive #2, etc... Log file type: 1 or NULL = error log, 2 = SQL Agent log Search string 1: String one you want to search for Search string 2: String two you want to search for to further refine the results EXEC sp_readerrorlog 0, 1, ‘Error’ 23
24. XP_ReadErrorLog EXEC xp_readerrorlog Parameters Value of error log file you want to read: 0 = current, 1 = Archive #1, 2 = Archive #2, etc... Log file type: 1 or NULL = error log, 2 = SQL Agent log Search string 1: String one you want to search for Search string 2: String two you want to search for to further refine the results Search from start time Search to end time Sort order for results: N'asc' = ascending, N'desc' = descending http://www.mssqltips.com/tip.asp?tip=1476 http://www.mssqltips.com/tip.asp?tip=1735 24
25. Scripting Another way to read the error logs is to read line by line and searching for keywords. Can be done using any programming language. This tip shows how it can be done using Windows Scripting http://www.mssqltips.com/tip.asp?tip=1307 Another tool is the Log Parser tool from Microsoft http://www.microsoft.com/downloads/details.aspx?FamilyID=890cd06b-abf8-4c25-91b2-f8d975cf8c07&displaylang=en 25
26. Scheduling SQL Agent Windows Scheduled Tasks VisualCron Other Third Party Tools SQL Express – does not include SQL Agent 26
27. SQL Agent Can Run ActiveX, CmdExec, Replication, SSAS, SSIS, T-SQL Run when SQL Agent starts SQL Agent Alert System sp_start_job System Tables Notifications Logging Alerts Can Start Job 27
28. Scheduling – Frequency Backups - Daily Integrity Checks - Weekly Index Maintenance - Weekly Other Tasks – Depends Reports – Daily, Weekly 28
30. Alerts Replication Database Mirroring Backups User Defined Etc… Response Execute a Job Notify Operator http://www.mssqltips.com/tip.asp?tip=939 http://www.mssqltips.com/tip.asp?tip=1564 30
31. Central Management Server New in SQL 2008 Allows you to register servers and fire off same query on all servers Uses Windows Authentication Only Can be used for SQL 2000, 2005 and 2008 http://www.mssqltips.com/tip.asp?tip=1767 31
33. Startup Procedures USE MASTERGOEXEC SP_PROCOPTION ‘SP_LOG_SERVER_START’, 'STARTUP', 'ON'GO USE MASTERGOEXEC SP_PROCOPTION ‘SP_LOG_SERVER_START’, 'STARTUP', ‘OFF'GO http://www.mssqltips.com/tip.asp?tip=1574 33
34. Questions and Wrap-up Thanks to our sponsor: Idera Next webcast in the series: “Under the Hood with SQL Server Fundamentals” Don Jones July 8th, 2009, 4pm EDT https://www2.gotomeeting.com/register/449103978 Download SQL diagnostic manager