Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL Backups for Beginners by Mark Gordon

Similar presentations


Presentation on theme: "SQL Backups for Beginners by Mark Gordon"— Presentation transcript:

1 SQL Backups for Beginners by Mark Gordon
v 1.2

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.

3 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.

4 SQL Databases Then, there are “your” databases - the ones you create.
These are called “User” databases.

5 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.

6 SQL Database File Types
1- Every database has at least an .mdf and .ldf. System databases too!!! 2- How does SQL process data?

7 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 Virtual Log Files (VLFs)

8 SQL Database Recovery Model
What is Point-in-Time Recovery?

9 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

10 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.

11 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.

12 SQL Database Recovery Model
What is the default Recovery Model?

13 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.

14 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.

15 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

16 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.

17 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

18 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.

19 Transaction Log Architecture
Full Recovery Perform Trans Log Backup (2 steps occur)

20 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

21 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.

22 SQL Database Recovery Model vs Backup Type
Full Backup Diff Backup Transaction Log Backup Simple Y N Full

23 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.

24 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

25 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

26 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

27 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

28 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

29 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

30 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.

31 SQL Database Demo Simple Recovery Model: Full and Diff bu Restore
Full Recovery Model: Full and Trans bu

32 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!

33 Resources SQL BOL (tail of the log) (log chain backup) Great link from Grant Fritchey showing how to do a backup and restore:

34 Questions ?


Download ppt "SQL Backups for Beginners by Mark Gordon"

Similar presentations


Ads by Google