Week 9 – Chapter 8 - Methods to Backup Databases - Types of data to be backed up - Recovery models - Recovery methods Methods to Back up Data • Maintenance Wizard (Chapter 7) to create maintenance plan with scheduled backups • Enterprise Manager to schedule job to perform backup or to perform unscheduled backup as required • T-SQL commands • Specialized packages such as Backup Exec or Arcserv (not discussed here) Prevent Loss of Data Have a strategy: 1. To minimize data loss (malicious use of delete, update statement, viruses, natural disaster, theft) 2. To recover lost data 3. To restore data with minimal cost and impact Backup regularly: 1. Backup frequently if your database is OLTP 2. Backup less frequently if your database is OLAP Database Recovery Models Set Database Recovery Model: • Full Recovery Model • Bulk logged recovery model • Simple Recovery Model Modify a database recovery model 1. Alter database pubs 2. Set recovery to bulk_logged Backups • SQL Server allows backups to occur while users continue to work with the database • Backs up original files and records their locations • Captures in the backup all database activities that occur during the backup process Backups (ctd) Who can perform backup? 1. Members of the sysadmin fixed server role 2. Members of the db_owner and db_backupoperators fixed database roles Where to store backup? 1. Hard disk file 2. Tape 3. A location identified by a Named Pipe (3rd party software package) When to backup System Databases • After modifying the master database: • Using CREATE DATABASE, ALTER DATABASE or DROP statement • Executing certain Stored Procedures • After modifying the msdb database • After modifying the model database When to backup User Databases • • • • After creating a database After creating an index After creating a transaction After performing un-logged operations: • BACKUP WITH TRUNCATE_ONLY OR NO_LOG OPERATIONS • SELECT INTO statement Restricted Activities during backup • • • • • Creating or modifying database Performing autogrow operations Creating indexes Performing non-logged options Shrinking a database Create a Backup device • A backup file that is created before it is used for a backup is called a backup device • Why create permanent backup devices? • To reuse backup files for future backups • To automate the backup Create a Backup device(ctd) Use sp_addumpdevice system procedure: • Specify a logical name • Logical and physical Names are stored in the sysdevices system table Example: Use master Exec sp_addumpdevice ‘disk’ , ‘mybackupfile’, ‘c:\Backup|mybackupfile.bak’ Perform Backup without backup device Why create backup without backup device? • To perform one time backup • To test backup operation that you plan to automate How to use backup database statement: • Specify the media type (disk, tape, or Named Pipe) • Specify the complete path and full Name Example: Use master Backup database Northwind To Disk = ‘c:\temp\mycustomers.bak’ Types of Backup Methods • • • • Full database backup Differential backup Transaction log backup File or File group backup Full Database Backup • Provides a baseline • Backs up original files, objects and data • Backs up portions of the transaction log Example: Use master Exec sp_addumpdevice ‘disk’, ‘NwindBac’ , ‘D:\mybackupdir\Nwindbac.bak’ Backup database Northwind to NwindBac Full Database Backup Options WITH INIT: overwrites any previous backup on that file WITH NOINIT : appends the full database backup to the backup file. Any previous backup left intact. Differential database backup • Use on frequently modified databases • Requires a full database backup before • Backs up database changes since the last full database backup • Saves time in both backup and restore processes Example: Backup Database Northwind Disk = ‘D:\Mydata|Mydiffbackup.bak’ WITH DIFFERNTIAL Transaction log backup • Requires a Full database backup • Backs up all database changes from the last BACKUP LOG statement to the end of the current Transaction log. • Truncates the transaction log Example: Use master Exec sp_addumpdevice ‘disk’, ‘Nwindbaclog’, ‘D:\Baclup\Nwind backuplog.bak’ Backup log Northwind To NwindBaclog Backup using No-truncate Option No-truncate option: • Saves the entire Transaction log even if the database is inaccessible • Doesn’t purge the Transaction log of committed Transactions • Allows data to be recovered up to time of system failure Clear the Transaction log • Use Backup statement to clear transaction log • Use truncate only or no_log option • Can’t recover changes • Is not recorded changes Database file or filegroup backup 1. Use on very large databases 2. Backup the database files individually 3. Ensure that all database files in File group are backed up 4. Back up transaction log Example: Backup database phoneorders File = Orders2 To orderbackup2 Backup log phoneOrders to orderlog