SQL Server Backup and Restore Shawn Mcgehee
Total Page:16
File Type:pdf, Size:1020Kb
The Red Gate Guide SQL Server Backup and Restore Shawn McGehee ISBN: 978-1-906434-74-8 SQL Server Backup and Restore 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 Backup and Restore 19 ___________________ Components of a SQL Server Database 20 ___________________________________ Data files 20 ________________________________________________________ Filegroups 22 _________________________________________________________ Transaction log 24 ____________________________________________________ SQL Server Backup Categories and Types 28 __________________________________ SQL Server database backups 29 _________________________________________ SQL Server 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 ___________________________________________________ Backup and Restore Planning 64 ___________________________________________ Backup requirements 65 _______________________________________________ Restore requirements 68 ______________________________________________ An SLA template 69 __________________________________________________ Example restore requirements and backup 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 Backup and Restore 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 Backup and Restore 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 backup restore 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 Backup and Restore 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 difficulties with point-in-time restores 198 __________________________ Forcing Restore Failures for Fun 200 ________________________________________ Summary 204 __________________________________________________________ Chapter 7: Differential Backup and Restore 205 ______________ Differential Backups, Overview 206 ________________________________________ Advantages of differential backups 207 ____________________________________ Differential backup strategies 208 _______________________________________ Possible issues with differential backups 212 _______________________________ Differentials in the backup and restore SLA 215 _____________________________ Preparing for Differential Backups 216 _______________________________________ Recovery model 216 ___________________________________________________