Sei sulla pagina 1di 43

Backup & Restore Part I

Backup

Backup
Backs up data while being changed Dos not acquire locks and ignores existing locks

Backs up changes made to the database wile being backed up - Performs a checkpoint and records the LSN of the oldest active transaction - Backs up data pages directly as is - Backs up transactions since the LSN recorded (including open transactions)
Degrades performance by only 5% 10%

Syntax
BACKUP DATABASE {database_name | @database_name_var} TO <backup_device> [,...n] [WITH [BLOCKSIZE = {blocksize | @blocksize_variable}] [[,] DESCRIPTION = {text | @text_variable}] [[,] DIFFERENTIAL] [[,] EXPIREDATE = {date | @date_var} | RETAINDAYS = {days | @days_var}] [[,] FORMAT | NOFORMAT] [[,] {INIT | NOINIT}] [[,] MEDIADESCRIPTION = {text | @text_variable}] [[,] MEDIANAME = {media_name | @media_name_variable}] [[,] [NAME = {backup_set_name | @backup_set_name_var}] [[,] {NOSKIP | SKIP}] [[,] {NOUNLOAD | UNLOAD}] [[,] [RESTART] [[,] STATS [= percentage]] ]

Permissions
BACKUP DATABASE and BACKUP LOG permissions default to members of the sysadmin fixed server role, db_owner fixed database role, who can transfer permissions to other users, and to members of the db_backupoperator fixed database role.

Backup media
Disk file (including network location) - For a network location, make sure the service MSSQLServer loads with a domain user, and that that user has sufficient NTFS permissions Local tape device - Can mix SQL Server backups and NT backups Pipe (for self developed / 3rd party backup software)

Named dump devices


Named dump devices - Logical name maps to a disk or a tape device - If physical location changes, need to drop and recreate the device, but all automated jobs do not need to be changed sp_addumpdevice [@devtype =] disk | pipe | tape, [@logicalname =] 'logical_name', [@physicalname =] 'physical_name' Permissions: Execute permissions default to members of the diskadmin fixed server role.

Examples
USE master EXEC sp_addumpdevice 'disk','mydiskdump', 'c:\dump\dump1.bak

USE master EXEC sp_addumpdevice 'disk', '\\servername\sharename\path\filename.ext' USE master EXEC sp_addumpdevice 'tape', 'tapedump1', '\\.\tape0'

From Enterprise Manager

Supplying the direct location of the backup device


- Has the same abilities as the named dump device - If the physical location of the device changes, need to change it in all automated jobs.

Media set
Create a media set by specifying more than one device in the backup statement. All devices must be of the same type (DISK | TAPE | PIPE) Cannot use a device that is part of a media set alone, unless you specify the FORMAT option. In this case, backups from the original media set can not be restored.

Full backup
Is required for following differential or transaction log backups Is current to the time the backup is finished
Examples: BACKUP DATABASE db1 TO DISK = \\servername\sharename\devname.bak BACKUP DATABASE db1 TO mydiskdump WITH INIT BACKUP DATABASE db1 TO mydiskdump WITH NOINIT

From Enterprise Manager

Differential backup
Requires a preceding full backup Backs up changed pages since the last full backup When restoring, the full backup and the last differential backup is needed If a row was modified several times, backs up the current image of the row Specify the option WITH DIFFERENTIAL in the BACKUP statement

Example
-- Create a full database backup first. BACKUP DATABASE MyNwind TO MyNwind_1 WITH INIT GO -- Time elapses. -- Create a differential database backup, appending the backup -- to the backup device containing the database backup. BACKUP DATABASE MyNwind TO MyNwind_1 WITH DIFFERENTIAL GO

From Enterprise Manager

Transaction log backup


Requires a preceding full backup (and optionally a differential backup) Backs up committed and uncommitted transactions from the Transaction Log Truncates the inactive part of the transaction log* (until the oldest open transaction)

Syntax
BACKUP LOG {database_name | @database_name_var} { [WITH { NO_LOG | TRUNCATE_ONLY }] } | { TO <backup_device> [,...n] [WITH [BLOCKSIZE = {blocksize | @blocksize_variable}] [[,] DESCRIPTION = {text | @text_variable}] [[,] EXPIREDATE = {date | @date_var} | RETAINDAYS = {days | @days_var}] [[,] FORMAT | NOFORMAT] [[,] {INIT | NOINIT}] [[,] MEDIADESCRIPTION = {text | @text_variable}] [[,] MEDIANAME = {media_name | @media_name_variable}] [[,] [NAME = {backup_set_name | @backup_set_name_var}] [[,] NO_TRUNCATE] [[,] {NOSKIP | SKIP}] [[,] {NOUNLOAD | UNLOAD}] [[,] [RESTART] [[,] STATS [= percentage]] ] }

Example
-- Create the log backup device. USE master EXEC sp_addumpdevice 'disk', 'MyNwindLog1', 'c:\mssql7\backup\MyNwindLog1.dat' -- Update activity has occurred before this point. -- Back up the log of the MyNwind database. BACKUP LOG MyNwind TO MyNwindLog1

From Enterprise Manager

Acknowledgements:

Most of the info for the following topics were taken from the book Inside SQL Server 7.0 by Kalen Delaney

Log truncate mode


When a database is in log truncate mode it is automatically truncated when a checkpoint occurs. In log truncate mode a checkpoint occurs in the following circumstances:
The log becomes 70 percent full. The number of log records reaches the number SQL Server estimates it can process during the time specified in the recovery interval option.

Log truncate mode


A database will be in log truncate mode in the following situations: - The database option trunc. log on chkpt. Is set to true - The database was never fully backed up - A non logged operation was performed against the database and it was never fully backed up since - The Transaction Log was manually truncated using the BACKUP LOG WITH NO_LOG | TRUNCATE_ONLY (in SQL Server 7.0 these are synonyms)

Shrinking the transaction log


The Transaction Log is built internally from Virtual log files. Transactions are written serially to the Transaction Log File. The active portion of the transaction log begins from the oldest open transaction until the last transaction. Virtual log files are marked as reusable if they contain inactive log records and were backed up. The Transaction Log File can be truncated:
Only in full virtual log files. Only from the end. Only if they are inactive and are marked as reusable.

Shrinking the transaction log


This may lead to situations where the Transaction Log can not be truncated. To overcome this, use the following procedure: 1. Backup or truncate the log to make sure earlier Virtual Log Files are reusable 2. Execute dummy transactions (in a loop) so that the active VLF will move to an earlier location. 3. Execute DBCC SHRINKDATABASE or DBCC SHRINKFILE to mark a shrinkpoint. 4. Repeat step 1 again to force the physical shrinkage. * Tip: Use the DBCC LOGINFO(dbname) to see a list of the virtual log files. Those with status 2 are active. Continue with the dummy transactions until the active VLF is at the beginning.

BACKUP LOG WITH NO_TRUNCATE


This option was provided to enable backing up the Transaction Log in case the disk containing the data files is damaged but the Transaction Log is still intact.
Currently this option is available only if the .mdf file is still available. To prepare for such a senario: 1. Place the PRIMARY FILEGROUP and the Transaction Log on the same physical disk. 2. Create an additional filegroup:
ALTER DATABASE database ADD FILEGROUP filegroup_name

3. Mark the new filegroup as default:


ALTER DATABASE database MODIFY FILEGROUP filegroup_name DEFAULT

Files and File Groups backup


Use with Very Large Databases were the backup window is not sufficient for a full backup If an index is created on a filegroup, all the filegreoup or all the files belonging to that filegroup are need to be backed up as a unit If a table was created on one filegroup and its indexes were created on separate filegroups, all the filegroups involved need to be backed up as a unit.

Syntax
BACKUP DATABASE {database_name | @database_name_var} <file = logical_file_name | filegroup = filegroup_name > [,...n] TO <backup_device> [,...n] [WITH

Examples
-- Back up the MyNwind file(s) and filegroup(s) BACKUP DATABASE MyNwind FILE = 'MyNwind_data_1', FILEGROUP = 'new_customers', FILE = 'MyNwind_data_2', FILEGROUP = 'first_qtr_sales' TO MyNwind_1 GO

From Enterprise Manager

Backing Up System Databases


Master
Only a full database backup can be performed on the master database

msdb model

sp_detach_db & copy


sp_detach_db [@dbname =] 'dbname' [, [@skipchecks =] 'skipchecks']

Detach a database from the server and, optionally, run UPDATE STATISTICS on all tables before detaching (use the false option if you are moving / copying the database to a read only media). Copy | move the files to the target location Run sp_attach_db or sp_attach_single_file_db on the target server

Backup Options

Backup Options
NAME backup name DESCRIPTION backup description INIT, NOINIT overwrite | append (if NOSKIP is specified, performs safety checks) SKIP, NOSKIP whether to perform a safety check FORMAT | NOFORMAT whether to rewrite the backup device and ignore existing backup sets and not perform any safety checks MEDIADESCRIPTION MEDIANAME if supplied, performs a media set name check

Backup Options
EXPIREDATE | RETAINDAYS checked if NOSKIP is performed RESTART restarts the backup operation at the point it was interrupted This option can only be used for backups directed to tape media and for backups that span multiple tape volumes. A restart operation never occurs on the first volume of the backup. NOUNLOAD | UNLOAD STATS = n - Informs the percentage of the backup operation every n percent of the backup BLOCKSIZE for media that requires a special block size

Database Maintenance Plans

Database Maintenance Plans

Database Maintenance Plans

Database Maintenance Plans

Database Maintenance Plans

Database Maintenance Plans

Database Maintenance Plans

Database Maintenance Plans

Database Maintenance Plans

Potrebbero piacerti anche