Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 391 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
391
Dung lượng
4,78 MB
Nội dung
ISBN: 978-1-906434-74-8 SQLServerBackupandRestore Shawn McGehee The Red Gate Guide SQLServerBackupandRestore By Shawn McGehee First published by Simple Talk Publishing April 2012 Copyright April 2012 ISBN 978-1-906434-85-4 The right of Shawn McGehee to be identified as the author of this work has been asserted by him in accordance with the Copyright, Designs and Patents Act 1988. All rights reserved. No part of this publication may be reproduced, stored or introduced into a retrieval system, or transmitted, in any form, or by any means (electronic, mechanical, photocopying, recording or otherwise) without the prior written consent of the publisher. Any person who does any unauthorized act in relation to this publication may be liable to criminal prosecution and civil claims for damages. This book is sold subject to the condition that it shall not, by way of trade or otherwise, be lent, re-sold, hired out, or otherwise circulated without the publisher's prior consent in any form other than that in which it is published and without a similar condition including this condition being imposed on the subsequent publisher. Technical Review by Eric Wisdahl Cover Image by Andy Martin Edited by Tony Davis Typeset & Designed by Peter Woodhouse & Gower Associates Table of Contents Introduction ___________________________________________12 Software Requirements and Code Examples ________________________________ 18 Chapter 1: Basics of BackupandRestore ___________________19 Components of a SQLServer Database ___________________________________ 20 Data files ________________________________________________________ 20 Filegroups _________________________________________________________22 Transaction log ____________________________________________________24 SQLServerBackup Categories and Types __________________________________28 SQLServer database backups _________________________________________29 SQLServer transaction log backups ____________________________________ 32 File backups _______________________________________________________36 Recovery Models ______________________________________________________39 Simple____________________________________________________________ 41 Full ______________________________________________________________43 Bulk Logged ______________________________________________________ 44 Restoring Databases ___________________________________________________ 46 Restoring system databases ___________________________________________47 Restoring single pages from backup ____________________________________48 Summary ___________________________________________________________ 49 Chapter 2: Planning, Storage and Documentation ___________50 Backup Storage ________________________________________________________50 Local disk (DAS or SAN) _____________________________________________ 52 Network device ____________________________________________________58 Tape _____________________________________________________________59 Backup Tools ________________________________________________________ 60 Maintenance plan backups ___________________________________________ 61 Custom backup scripts ______________________________________________62 Third-party tools ___________________________________________________63 BackupandRestore Planning ___________________________________________ 64 Backup requirements _______________________________________________65 Restore requirements ______________________________________________ 68 An SLA template __________________________________________________ 69 Example restore requirements andbackup schemes _______________________ 71 Backup scheduling __________________________________________________ 73 Backup Verification and Test Restores _____________________________________ 75 Back up WITH CHECKSUM _________________________________________76 Verifying restores ___________________________________________________77 DBCC CHECKDB __________________________________________________77 Documenting Critical Backup Information _________________________________78 Summary ____________________________________________________________83 Chapter 3: Full Database Backups _______________________ 84 What is a Full Database Backup? _________________________________________ 84 Why Take Full Backups? ________________________________________________85 Full Backups in the BackupandRestore SLA _______________________________ 86 Preparing for Full Backups ______________________________________________87 Choosing the recovery model _________________________________________88 Database creation __________________________________________________88 Creating and populating the tables ____________________________________ 94 Taking Full Backups ___________________________________________________ 96 Native SSMS GUI method ____________________________________________97 Native T-SQL method ______________________________________________106 Native Backup Compression ____________________________________________ 111 Verifying Backups ____________________________________________________ 113 Building a Reusable and Schedulable Backup Script _________________________ 114 Summary ___________________________________________________________ 115 Chapter 4: Restoring From Full Backup ___________________ 116 Full Restores in the BackupandRestore SLA ______________________________ 116 Possible Issues with Full Database Restores ________________________________ 117 Large data volumes ________________________________________________ 118 Restoring databases containing sensitive data ___________________________ 118 Too much permission ______________________________________________ 120 Performing Full Restores _______________________________________________ 122 Native SSMS GUI full backuprestore __________________________________ 122 Native T-SQL full restore ___________________________________________ 129 Forcing Restore Failures for Fun _________________________________________ 133 Considerations When Restoring to a Different Location _____________________ 136 Restoring System Databases ____________________________________________ 137 Restoring the msdb database ________________________________________ 138 Restoring the master database _______________________________________140 Summary ___________________________________________________________ 143 Chapter 5: Log Backups ________________________________144 A Brief Peek Inside a Transaction Log ____________________________________ 145 Three uses for transaction log backups ___________________________________ 148 Performing database restores ________________________________________ 149 Large database migrations ___________________________________________ 150 Log shipping _____________________________________________________ 151 Log Backups in the BackupandRestore SLA _______________________________ 152 Preparing for Log Backups _____________________________________________ 153 Choosing the recovery model ________________________________________ 154 Creating the database ______________________________________________ 155 Creating and populating tables _______________________________________ 157 Taking a base full database backup ____________________________________ 159 Taking Log Backups ___________________________________________________ 161 The GUI way: native SSMS log backups ________________________________ 161 T-SQL log backups _________________________________________________ 166 Forcing Log Backup Failures for Fun _____________________________________170 Troubleshooting Log Issues ____________________________________________ 172 Failure to take log backups __________________________________________ 173 Other factors preventing log truncation _______________________________ 174 Excessive logging activity ___________________________________________ 175 Handling the 9002 Transaction Log Full error __________________________ 176 Log fragmentation _________________________________________________ 177 Summary ___________________________________________________________ 181 Chapter 6: Log Restores ________________________________182 Log Restores in the SLA ________________________________________________ 182 Possible Issues with Log Restores ________________________________________ 183 Missing or corrupt log backup _______________________________________ 183 Missing or corrupt full backup _______________________________________ 184 Minimally logged operations_________________________________________ 184 Performing Log Restores _______________________________________________ 187 GUI-based log restore ______________________________________________ 188 T-SQL point-in-time restores ________________________________________ 194 Possible diculties with point-in-time restores __________________________ 198 Forcing Restore Failures for Fun ________________________________________ 200 Summary __________________________________________________________ 204 Chapter 7: Dierential BackupandRestore ______________ 205 Differential Backups, Overview ________________________________________ 206 Advantages of differential backups ____________________________________207 Differential backup strategies _______________________________________ 208 Possible issues with differential backups _______________________________ 212 Differentials in the backupandrestore SLA _____________________________ 215 Preparing for Differential Backups _______________________________________ 216 Recovery model ___________________________________________________ 216 Sample database and tables plus initial data load _________________________ 217 Base backup ______________________________________________________ 218 Taking Differential Backups ____________________________________________ 218 Native GUI differential backup _______________________________________ 219 Native T-SQL differential backup _____________________________________ 221 Compressed differential backups _____________________________________ 223 Performing Differential Backup Restores __________________________________ 225 Native GUI differential restore _______________________________________ 225 Native T-SQL differential restore _____________________________________227 Restoring compressed differential backups _____________________________230 Forcing Failures for Fun ________________________________________________ 231 Missing the base __________________________________________________ 231 Running to the wrong base __________________________________________ 232 Recovered, already _________________________________________________ 235 Summary ___________________________________________________________ 237 Chapter 8: Database BackupandRestore with SQLBackup Pro 238 Preparing for Backups _________________________________________________ 238 Full Backups _________________________________________________________ 241 SQLBackup Pro full backup GUI method ______________________________ 241 SQLBackup Pro full backup using T-SQL ______________________________ 253 Log Backups _________________________________________________________ 256 Preparing for log backups ___________________________________________ 256 SQLBackup Pro log backups ________________________________________ 258 Differential Backups __________________________________________________ 261 Building a reusable and schedulable backup script __________________________ 263 Restoring Database Backups with SQLBackup Pro _________________________267 Preparing for restore _______________________________________________267 SQLBackup Pro GUI restore to the end of a log backup ___________________269 SQLBackup Pro T-SQL complete restore ______________________________ 277 SQLBackup Pro point-in-time restore to standby________________________279 Restore metrics: native vs. SQLBackup Pro _____________________________ 289 Verifying Backups ____________________________________________________ 291 Backup Optimization _________________________________________________292 Summary ___________________________________________________________294 Chapter 9: File and Filegroup BackupandRestore _________ 295 Advantages of File BackupandRestore __________________________________ 296 Common Filegroup Architectures _______________________________________298 File Backup __________________________________________________________ 303 Preparing for file backups ___________________________________________306 SSMS native full file backups ________________________________________309 Native T-SQL file differential backup __________________________________ 310 SQLBackup Pro file backups ________________________________________ 314 File Restore __________________________________________________________ 318 Performing a complete restore (native T-SQL) ___________________________ 321 Restoring to a point in time (native T-SQL) _____________________________ 326 Restoring after loss of a secondary data file _____________________________ 328 Quick recovery using online piecemeal restore __________________________ 335 Common Issues with File BackupandRestore _____________________________340 File BackupandRestore SLA ____________________________________________ 341 Forcing Failures for Fun ________________________________________________ 343 Summary ___________________________________________________________346 Chapter 10: Partial BackupandRestore _________________ 348 Why Partial Backups? _________________________________________________349 Performing Partial Database Backups _____________________________________ 350 Preparing for partial backups ________________________________________ 351 Partial database backup using T-SQL __________________________________ 354 Differential partial backup using T-SQL ________________________________ 355 Performing Partial Database Restores ____________________________________ 357 Restoring a full partial backup _______________________________________ 357 Restoring a differential partial backup _________________________________ 359 Special case partial backuprestore ____________________________________360 SQLBackup Pro Partial BackupandRestore _______________________________ 362 Possible Issues with Partial BackupandRestore ____________________________364 Partial Backups and Restores in the SLA __________________________________ 365 Forcing Failures for Fun ________________________________________________366 Summary ___________________________________________________________ 367 Appendix A: SQLBackup Pro Installation and Configuration 368 SQLBackup Pro GUI Installation ________________________________________ 368 SQLBackup Pro Services Installation ____________________________________370 SQLBackup Pro Configuration _________________________________________376 File management __________________________________________________ 376 Email settings _____________________________________________________ 378 [...]... planning a backupand recovery strategy, from choosing and configuring hardware, gathering and documenting the requirements for each database, selecting the appropriate backup tool, scheduling considerations, running backup verification checks, and more Basic backupandrestore – how to capture andrestore all of the basic backup types, using SSMS and T -SQL • Chapters 3 and 4 cover how to take standard and. .. strategy Advanced backupandrestore – how to capture andrestore file and filegroup backups, and partial database backups • Chapter 9 – arguably the most advanced chapter in the book, explaining the filegroup architectures that enable file-based backupand restore, and the complex process of capturing the necessary file backups and transaction log backups, and using them in various restore operations... database backups, andrestore them 16 Introduction • Chapters 5 and 6 cover how to take transaction log backups, and then use them in conjunction with a full database backup to restore a database to a particular point in time They also cover common transaction log problems and how to resolve them • Chapter 7 covers standard and compressed differential database backup andrestore Basic backup and restore. .. this book were tested on SQLServer 2008 andSQLServer 2008 R2 Standard Edition, with the exception of the online piecemeal restore in Chapter 9, which requires Enterprise Edition Red Gate SQLBackup v.6.4.056 was used in all SQLBackup examples, in Chapters 8 and 9 of this book 18 Chapter 1: Basics of Backup andRestore Before we dive into the mechanisms for taking and restoring backups, we need to start... primary and secondary files and filegroups, plus log files • how SQLServer uses the transaction log – and its significance in regard to restore capabilities • possible types of SQLServerbackup – full and differential database backups, transaction log backups and file backups • SQLServer database recovery models – the available recovery models and what they mean in terms of backups • restoring databases... restore with SQLBackup – how to capture andrestore all basic backup types using Red Gate SQLBackup • Chapter 8 – third-party tools such as Red Gate SQLbackup aren't free, but they do offer numerous advantages in terms of the ease with which all the basic backups can be captured, automated, and then restored Many organizations, including my own, rely on such tools for their overall backup and restore. .. to start planning a BackupandRestore SLA from scratch, even in a moderately-sized environment, and I hope this book helps you get a good start 15 Introduction How the book is structured In this book, you'll discover how to perform each of these backupandrestore operations using SQLServer Management Studio (SSMS), basic T -SQL scripts and Red Gate's SQLBackup tool Capturing backups using SSMS or... can, and generally should, be backed up as part of your database backupand recovery strategy This includes both user and system databases There are three broad categories of backup that a DBA can perform: database backups, file backups and transaction log backups, and within these categories several different types of backup are available • Database backups – copy into a backup file the data and objects... prerequisite and basic sections will be a very worthwhile task Software Requirements and Code Examples Throughout this book are scripts demonstrating various ways to take andrestore backups, using either native T -SQL or SQLBackup scripts All the code you need to try out the examples in this book can be obtained from the following URL: www.simple-talk.com/RedGateBooks/ShawnMcGehee/SQLServerBackupAndRestore_Code.zip... types of database restore, plus special restores, such as system database restores 19 Chapter 1: Basics of Backup andRestore Components of a SQLServer Database Ultimately, a relational database is simply a set of files that store data When we make backups of these files, we capture the objects and data within those files and store them in a backup file So, put simply, a database backup is just a copy . compressed differential database backup and restore. Basic backup and restore with SQL Backup – how to capture and restore all basic backup types using Red Gate SQL Backup. • Chapter 8 – third-party. running backup verification checks, and more. Basic backup and restore – how to capture and restore all of the basic backup types, using SSMS and T -SQL. • Chapters 3 and 4 cover how to take standard. such tools for their overall backup and restore strategy. Advanced backup and restore – how to capture and restore file and filegroup backups, and partial database backups. • Chapter 9 – arguably