282 Chapter7•MaintainingYourDatabase Example 7.23 Restoring a Damaged Page Online First, restore the filegroup A and C RESTORE DATABASE AdventureWorks PAGE='1:57, 1:202, 1:916, 1:1016' FROM DBFile2_Backup WITH NORECOVERY; RESTORE LOG AdventureWorks FROM LogFile1_Backup WITH NORECOVERY; RESTORE LOG AdventureWorks FROM LogFile2_Backup WITH NORECOVERY; BACKUP LOG AdventureWorks TO TailLog_Backup RESTORE LOG AdventureWorks FROM TailLog_Backup WITH RECOVERY; GO The following system databases must be backed up for production servers: Master Model MSDB Configuration/Distribution The recovery model is set to Simple for all system databases except MSDB. The recovery model for the MSDB database can be configured to Full or Bulk-Logged. If you notice that system databases are damaged, you have a choice of restoring the databases from backup or rebuilding them to their initial state. If you can start Head of the Class… Disaster Recovery Considerations for System Databases SQL Server stores important configuration information in system databases. Unfortunately, the backup of system databases is often overlooked. This oversight results in difficulties when SQL Server is rendered inoperable by a disaster, and you must restore it to the last known state. When you are restoring an entire server, always restore the system databases before user databases. MaintainingYourDatabase•Chapter7 283 the SQL Server service, it is recommended that the system databases be restored from the most recent backup. If you cannot start the SQL Server service, you must rebuild the system databases first then attempt to restore the databases. Perform the following steps to restore the Master database: 1. Configure SQL Server to run in single user mode. 2. Restore the Master database from backup using the RESTORE DATABASE statement. 3. After the restore, SQL Server service will disconnect your connection. 4. Configure SQL Server to run in multiple user mode and restart the service. Performing Ongoing Maintenance Many repetitive administrative tasks are critical to maintaining your SQL Server’s health and performance. These tasks include database validation, index maintenance, and backup. SQL Server 2008 makes it easy to follow best practices for ongoing maintenance. The SQL Server Agent feature allows you to schedule administrative routines by creating multi step jobs. The SQL Server Agent feature can also notify people when a certain event occurs. You can use the Maintenance Plans feature to visually create, schedule, and monitor ongoing maintenance tasks. Maintenance Plans allow you to consolidate all individual administrative tasks into a coherent strategy. Policy-Based Management allows administrators in large organizations to monitor and configure any number of SQL Servers and databases for configuration settings and compliance. Using this feature, you can also enforce compliance rules and standards on the naming and configuration of SQL Servers and SQL Server objects. Using SQL Server Agent SQL Server Agent is a service installed with SQL Server 2008. This service allows you to schedule activities to run at certain times. SQL Server Agent is widely used by administrators to schedule database backups, performance data collection, policy applications, and other routine tasks. SQL Server agent has the ability to notify people by e-mail of certain events, a feature known as notifications. Let’s examine the three key concepts behind SQL Server Agent: Jobs Jobs are the tasks you wish to execute on a schedule. Jobs consist of job steps, which define the individual task to run and what happens if the step fails. Steps are defined using Transact-SQL; however, multiple wizards generate the required Transact-SQL statements from a friendly interface. 284 Chapter7•MaintainingYourDatabase When defining job steps, you can specify their order and which step to execute next on success or failure of the previous jobs. Jobs are associated with flexible schedules. Figure 7.3 shows the types of schedules you can configure. Figure 7.3 SQL Server Agent Schedule Options Alerts Alerts are actions that SQL Server will perform when a particular event occurs. Alerts consist of conditions and actions. Conditions can be specific error codes, error severities or object states like databases files growing to a certain size. Actions performed by an alert are notifying a person, or running a job. Operators Operators are people who will be notified of alerts or job completion. SQL Server Agent can notify operators through e-mail, pager, or a net send network command. SQL Server Agent stores the configuration for jobs, schedules, alerts, and operators in the MSDB system database. This is why it is very important to backup the MSDB MaintainingYourDatabase•Chapter7 285 system database in production environments. If you don’t back up this database, you risk losing the entire maintenance related configuration you have spent countless hours implementing. Ex a m Wa r n i n g SQL Server Agent is widely used by system activities like Maintenance Plans and replication. Therefore, if you are using any of these features, you must ensure that the SQL Server Agent service is running. Ideally, you should set the Startup Type for this service to Automatic to ensure the service is not accidentally stopped during server restart. Also remember that there is a separate SQL Server Agent service for every instance of SQL Server installed on the computer. In order to enable SQL Server Agent to notify users by e-mail, the Database Mail feature of SQL Server must be configured. Database Mail uses a Simple Mail Transfer Protocol (SMTP) server to relay its e-mail. It is recommended that you turn off the Database Mail feature if you are not going to be notifying operators by e-mail. This is to avoid the risk of your server being compromised and used as a relay point for unauthorized bulk e-mail. If you wish to enable Database Mail settings, use the Database Mail Configuration Wizard accessible from the Object Explorer. To configure it, expand Management, right-click Database Mail and then click Configure Database Mail. This feature is not reliant on the availability of SQL Server service as it caches the e-mail profile and credentials. Therefore, SQL Server Agent can notify you of a server outage by e-mail. Using Database Maintenance Plans Database Maintenance Plans are a wizard-based way to perform recommended tasks on your SQL Server 2008 servers. The Maintenance Plan Wizard allows you to select from a set of best practice based tasks and will create and schedule SQL Server Agent jobs to perform these tasks on a specified schedule. It is strongly recommended that you start with the guidance offered by the Maintenance Plan Wizard and set up the core administration tasks. You can later customize the plans by adding extra tasks or modifying the tasks themselves. You can schedule Maintenance Plans or run them manually. You can also target linked servers with your maintenance plans if you wish. 286 Chapter7•MaintainingYourDatabase Let’s examine the important routine management tasks that can be created easily using the Maintenance Plan Wizard: Maintain indexes by reorganizing or rebuilding them with a Fill Factor. Routinely rebuilding indexes prevents overall performance degradation due to fragmentation. Routinely enforcing a Fill Factor ensures that write performance is optimal. This plan option uses the ALTER INDEX WITH REBUILD | REORGANIZE statement. Maintain indexes by updating index statistics to ensure optimal query per- formance. This plan option uses DBCC UPDATESTATISTICS command. Keep used disk space to a minimum by data in database files so that any empty space is moved to the end of the file. Then, the maintenance plan can physically shrink the file by truncating empty space. This plan option uses DBCC SHRINKFILE command. Validate database integrity, detect database corruption, and attempt to repair it. This plan option uses DBCC CHECKDB command. Backup the databases and transaction logs customized for your backup strategy. This plan option uses BACKUP DATABASE command. Run any SQL Server Agent job. As you can see, although you can use SQL Server Agent to create and schedule these tasks manually, it is quite complicated and easy to view and change. Database Maintenance Plans allow you to configure and schedule these vital tasks using a simple user interface. When maintenance plans execute, they produce log records that are written to a file-based report, which by default is stored in the %Program Files%\Microsoft SQL Server\MSSQL.1\MSSQL\LOG folder and is named starting with the Maintenance Plan name followed by the execution date and time. Execution results are always written to Sysmaintplan_log and sysmaintplan_logdetail tables stored in MSDB. TE s T Da y Ti p Your user account must belong to the sysadmin fixed server role to view, create, or modify Maintenance Plans. If you are connected with an account that is not a member of sysadmin, the Maintenance Plans item will not appear in Object Explorer. . and SQL Server objects. Using SQL Server Agent SQL Server Agent is a service installed with SQL Server 2008. This service allows you to schedule activities to run at certain times. SQL Server. restore the Master database: 1. Configure SQL Server to run in single user mode. 2. Restore the Master database from backup using the RESTORE DATABASE statement. 3. After the restore, SQL Server. restored from the most recent backup. If you cannot start the SQL Server service, you must rebuild the system databases first then attempt to restore the databases. Perform the following steps