SQL Backups for Beginners by Mark Gordon v 1.2
Getting Started Goal Bio Why Backup? You backup the db so you can restore it back from a failure. When restored, the db must be transactionally consistent.
SQL Databases When you install SQL Server there are four databases that are automatically installed. Thus, these are called System databases: Master Model MSDB TempDB These are used for different internal purposes. Do you need to back these System databases up too? Yes, except for TempDB!! TempDB gets rebuilt by SQL every time your restart SQL Server. The Master, MSDB, and Model databases might be backed-up less frequently than your own databases but business needs will drive that decision.
SQL Databases Then, there are “your” databases - the ones you create. These are called “User” databases.
SQL Database File Types Three main types of sql database files for each database: Primary data file (.mdf) – this is the primary data file (heart) of your database. It holds the actual data. Every database has one primary file. Secondary data file (.ndf). These is optional and lets you spread data into different physical disc for different reasons. We will not be discussing this one today because as beginners, you most likely are not using it. Transaction Log (.ldf) – this is the log data file. It captures change and is what sql uses to restore your database – among other things. There must be at least one log file for every database. Not easily readable. https://msdn.microsoft.com/en-us/library/ms189563.aspx
SQL Database File Types 1- Every database has at least an .mdf and .ldf. System databases too!!! 2- How does SQL process data? https://msdn.microsoft.com/en-us/library/ms189563.aspx
Transaction Log Architecture How Does It Work? It tracks change for SQL. Do not consider it an audit trail! VLF (Virtual Log File) – Unit of growth. LSN (Log Sequence Number) – Unique identifier for each log record. Write Ahead Round Robin Physical Log File Unused Unused Unused Unused Unused Unused https://technet.microsoft.com/en-us/library/ms179355(v=sql.105).aspx Virtual Log Files (VLFs)
SQL Database Recovery Model What is Point-in-Time Recovery?
SQL Database Recovery Model A recovery model is a database property that controls how transactions are logged, whether the transaction log requires (and allows) backing up, and what kinds of restore operations are available https://msdn.microsoft.com/en-us/library/ms189275(v=sql.110).aspx
SQL Database Recovery Model There are three database recovery models: Simple: Provides NO point-in-time recovery. Truncates Log On Checkpoint !!!!! No separate transaction log backups. When you make a backup and you need to recover data, you only have the database recoverable to the point of a backup. Full: Gives you the capability for Point in time recovery. When you have log backups, you get point in time recovery. Commonly used for OLTP applications. Requires separate transaction log backups. Bulk -logged: Similar to Full. Can have separate transaction log backups but is normally used “temporarily” for special bulk loading operations. It reduces the amount of logging it performs during bulk load operations.
SQL Database Recovery Model It is the database that has the recovery model. Thus, a database can have one of the three recovery models: Simple Full Bulk-logged The recovery model can be changed. But, this requires further discussion as to why and when you would do this. Altering the recovery model may have an impact on your backup approach. https://msdn.microsoft.com/en-us/library/ms189275(v=sql.130).aspx
SQL Database Recovery Model What is the default Recovery Model?
SQL Database Backup Types There are three main backup types: Full Differential Transaction Log There are actually others but we will focus only on these today.
SQL Database Backup Types Full This is a complete backup of the database – both data file (MDF/NDF) and transaction log (LDF). This includes part of the transaction log so that the database can be recovered, if needed. If recovery is needed, you will end up with a transactionally consistent database when done.
SQL Database Recovery Model Restore – With Full Backup What could I restore? Full Backup Sunday 12:00 AM Full Backup Sunday 12:00 AM Fri Mon Wed Tue Thu Sat
SQL Database Backup Types Differential A differential backup is based on the most recent/previous FULL data backup. A differential backup captures only the data that has changed since that last full backup. The full backup upon which a differential backup is based is known as the base of the differential. In SQL Server 2005 and later versions, differential file backups can be very fast because the SQL Server Database Engine tracks the changes that were made since the differential base was created. https://technet.microsoft.com/en-us/library/ms175526(v=sql.130).aspx
SQL Database Recovery Model Restore – With Full And Diff What could I restore? Full Backup Sunday 12:00 AM Full Backup Sunday 12:00 AM Diff 12 AM Diff 12 AM Diff 12 AM Diff 12 AM Diff 12 AM Diff 12 AM Fri Mon Wed Tue Thu Sat
SQL Database Backup Types Transaction Log Backup Used for the point-in-time recovery. Not applicable for Simple Recovery. Simple is Truncate On CheckPoint!!! You must back-up the transaction log separately from a Full or Diff backup. What is active/inactive transactions? When the separate transaction log backup occurs, the inactive portion of the transaction log is truncated. In Full or Bulk, the inactive part of the log cannot be truncated until all its log records have been captured in a log backup. Just because you do a Full backup “does not” mean you have truncated the inactive part of the transaction log – YOU HAVEN’T. Thus, when in FULL recovery mode, you must do transaction log backups. https://technet.microsoft.com/en-us/library/ms189085(v=sql.105).aspx
Transaction Log Architecture Full Recovery Perform Trans Log Backup (2 steps occur) https://msdn.microsoft.com/en-us/library/jj835093(v=sql.120).aspx
SQL Database Recovery Model Full Recovery – With Full And Diff No Transaction Log Backup (Danger!) Full Backup Sunday 12:00 AM Full Backup Sunday 12 AM Diff 12 AM Diff 12 AM Diff 12 AM Diff 12 AM Diff 12 AM Diff 12 AM Trn Trn Trn Trn Trn Trn Trn Fri Mon Wed Tue Thu Sat
SQL Database Recovery vs Backup Type Key Points There are database recovery models that the database operates with: Simple Full Bulk-logged Then there are database backup types : Differential Transaction log Just remember that if a database is in simple recovery, you do not get a transaction log backup. This is because the transaction log gets truncated many times during processing and does not give you point in time recovery – that is why it is called SIMPLE.
SQL Database Recovery Model vs Backup Type Full Backup Diff Backup Transaction Log Backup Simple Y N Full https://msdn.microsoft.com/en-us/library/ms189275(v=sql.110).aspx
SQL Database Restoring When you restore a database, you must start restoring from a Full backup. What you do after that depends on the recovery model and what you are needing to do. Plan out what you need to do before you start.
SQL Database Recovery Model Simple Recovery – With Full Backup Full Backup Sunday 12:00 AM Full Backup Sunday 12:00 AM Fri Mon Wed Tue Thu Sat
SQL Database Recovery Model Simple Recovery – With Full And Diff Full Backup Sunday 12:00 AM Full Backup Sunday 12:00 AM Diff 12 AM Diff 12 AM Diff 12 AM Diff 12 AM Diff 12 AM Diff 12 AM Fri Mon Wed Tue Thu Sat
SQL Database Recovery Model Full Recovery – With Full Backup No Diff and No Trans Log Backup Full Backup Sunday 12:00 AM Full Backup Sunday 12:00 AM Trn Trn Trn Trn Trn Trn Trn Fri Mon Wed Tue Thu Sat
SQL Database Recovery Model Full Recovery – With Full And Diff No Transaction Log Backup Full Backup Sunday 12:00 AM Full Backup Sunday 12 AM Diff 12 AM Diff 12 AM Diff 12 AM Diff 12 AM Diff 12 AM Diff 12 AM Trn Trn Trn Trn Trn Trn Trn Fri Mon Wed Tue Thu Sat
SQL Database Recovery Model Full Recovery – With Full and Transaction backup (log chain)!! Trn BU 6 PM Trn BU 6 PM Trn BU 6 PM Trn BU 6 PM Trn BU 6 PM Trn BU 6 PM Full Backup Sunday 12:00 AM Full Backup Sunday 12 AM Trn Trn Trn Trn Trn Trn Trn Fri Mon Wed Tue Thu Sat
SQL Database Recovery Model Full Recovery – With Full, Diff and Transaction backup (log chain)!! Trn BU 6 PM Trn BU 6 PM Trn BU 6 PM Trn BU 6 PM Trn BU 6 PM Trn BU 6 PM Full Backup Sunday 12:00 AM Full Backup Sunday 12 AM Diff 12 AM Diff 12 AM Diff 12 AM Diff 12 AM Diff 12 AM Diff 12 AM Trn Trn Trn Trn Trn Trn Trn Fri Mon Wed Tue Thu Sat
SQL Database Key Points Simple Recovery – No point-in-time recovery. Full Recovery with transaction log backups give point-in-time recovery opportunity. Everything starts with a FULL backup! When you restore a database you must start from a Full backup.
SQL Database Demo Simple Recovery Model: Full and Diff bu Restore Full Recovery Model: Full and Trans bu
Switching Recovery Models: SQL Database Switching Recovery Models: Always do a Full or Diff backup after changing recovery models from Simple-to-Full or Full-to-Simple. Switching from Simple-to-Full, it must start the log chain. Thus, Full or Diff backup is needed; it will start the backup log chain when you do the Full or Diff bu. I like doing Full bu. Switching from Full-to-Simple, breaks the backup log chain. Thus Full or Diff backup is needed. I like doing Full bu. Note: If you switch from Simple Recovery to Full Recovery, it will still behaves as Simple until you do a Full or Diff backup! https://technet.microsoft.com/en-us/library/ms178052(v=sql.105).aspx
Resources SQL BOL https://technet.microsoft.com/en-us/magazine/2009.07.sqlbackup.aspx http://www.sqlskills.com/blogs/paul/a-sql-server-dba-myth-a-day-3030-backup-myths/ https://technet.microsoft.com/en-us/library/ms189085%28v=sql.105%29.aspx https://msdn.microsoft.com/en-us/library/ms179314(v=sql.105).aspx (tail of the log) https://technet.microsoft.com/en-us/library/ms190440(v=sql.105).aspx (log chain backup) Great link from Grant Fritchey showing how to do a backup and restore: https://www.youtube.com/watch?v=pA242aMvz6E
Questions ? Email: markgordonnc@nc.rr.com