You are on page 1of 50

Database Backup &

Recovery

CISB314 Database 2 1
Basic
Introductio
Recovery
n
Facilities

Outline Types of
Recovery Database
and Restart failure and
Procedures its
response.

CISB314 Database 2 2
Introduction

• Database can be damaged, lost and become unavailable.

• Common cause of data loss includes human error, hardware


failure, incorrect data, program error, computer viruses, natural
catastrophes, etc.

• Backing up data is vital for businesses.

CISB314 Database 2 3
Introduction

• Backup – make a copy of a database (either partial or full)


periodically in another device.

• Database recovery provides mechanisms for restoring (re-


establish) a database quickly and accurately to its original state
after a database is loss or damage.

• RESPONSIBILITY OF DBA

CISB314 Database 2 4
Backup and
recovery
overview
• At t1, a database backup
operation is performed
• At t2, a problem that
damages the database
occurs
• At t3, all committed data
is recovered
Basic Recovery Facilities

• DBMS should provide four basic facilities for backup & recovery
of a database:
1. Backup Facility
– periodic backup
2. Journalizing Facility
– audit trail of transaction
3. Checkpoint Facility
– suspends & synchronize
4. Recovery Manager
- restore and restart processing

CISB314 Database 2 6
• Make copies of all or a portion of
database periodically to a different
devices.
• What can be backup?
• User data, catalogs and all control files
(buffer pool files, table space file,
1. Backup database configuration file)
Facility • Backups are stored in secure, off-site
location
• Backup copy is used to restore data in the
event of database failure

CISB314 Database 2 7
• Different types of Backup Modes:
1. Cold backup / offline backup
• Database is shut down during backup
• Does not allow other applications or
processes to access the database
2. Hot backup / online backup
1. Backup • Selected portion is shut down and
Facility backed up at a given time
• Allow other applications or
processes to access the database
while the backup is happening
• Also known as dynamic backup or
active backup

CISB314 Database 2 8
• Types of backup:
a) Full backups: all selected data
will be copied/backup to other
devices
b) Incremental backups: only
copy/backup changes made since
1. Backup the last backup
Facility c) Differential backups:
copy/backup all changes made
since the last full backup
• the differences since the last
full backup.

CISB314 Database 2 9
1. Backup
Facility
(cont’d)

CISB314 Database 2 10
1. Backup
Facility
(cont’d)

CISB314 Database 2 11
1. Backup Facility (cont’d)

CISB314 Database 2 12
• Backup devices/storage media:
• Magnetic Tape
• Hard disk
• Optical storage (CD-R, DVD, Blu-ray
1. Backup discs)
• Solid state drive (thumb drive, flash
Facility memory)
• Cloud

CISB314 Database 2 13
• To perform backup, DBA can either:
• Use DBMS COPY utility
1. Backup • Use SQL statements to set up
commands to perform backup
Facility

CISB314 Database 2 14
Database backup syntax (DB2)

BACKUP DATABASE <dbname> [ONLINE] [TO <dest_path>]

• Offline backup example


BACKUP DATABASE sample TO C:\BACKUPS

• Online backup example


BACKUP DATABASE sample ONLINE TO C:\BACKUPS
Database backups – File naming convention

Timestamp

Server
Table space backup

• Enable user to backup a subset of database


• Multiple table spaces can be specified
• Database must be using archival logging
• Table space backup can run in both online and offline backup
• Table space can be restored from either database backup or
table space backup
• Use keyword TABLESPACE to specify table spaces
• BACKUP DATABASE sample TABLESPACE (TBSP1) ONLINE TO
C:\BACKUPS
Database Backup - Compression

• In DB2, backup can now be automatically compressed


• Reduce backup storage costs
CISB314 Database 2

2. • Maintain an audit trail of transaction and database


changes in logs or journals.
Journalizing • Transactions: A discrete unit of work that must be
completely processed or not processed at all within
Facility a computer system.
• Journals are used together with backup copy to
recover database in the event of failure.

19
CISB314 Database 2

• Two basic journals or logs:


a) Transaction log – record of essential data
for each transaction processed against the
2. database
• e.g. Transaction code, action, time, terminal
Journalizing no/user ID, input data values ,
tables/records accessed and modified
Facility b) Database change log - images of data that
have been modified by transaction
• Before-image: copy of a record before
modification
• After-image: copy of a record after
modification

20
Database logging

• Logs keep track of changes made to database objects and their data.
• They are used for recovery: If there is a crash, logs are used to
playback/redo committed transactions, and undo uncommitted ones.
• Can be stored in files or raw devices
• Logging is always ON for regular tables in DB2
• It is possible to mark some tables or columns as NOT LOGGED
• It is possible to declare and use USER temporary tables which may
not be logged
CISB314 Database 2

• Database logging in DB2:


a) Circular logging
• For non-production systems
• Logs that become archived, can be
overwritten
2. b)
• Suitable for offline backup
Archival logging
Journalizing •

For production systems
History of log files is maintained
Facility • No logs are deleted.
• Some are stored online (with active logs),
others offline in an external media
• Suitable for online backup

22
CISB314 Database 2

2.
Journalizin
g Facility

23
• A point where DBMS periodically suspend
all processing and synchronize its files and
journals to establish a recovery point.

3. Checkpoint • At this point, system is in a quiet state, and


database and transaction logs are
Facility synchronized.
• All transactions in progress are completed
and journal files are brought up-to-date

CISB314 Database 2 24
• DBMS writes a checkpoint record to the
3. Checkpoint log file, that contains information
necessary to restart the system.
Facility

CISB314 Database 2 25
3. Checkpoint Facility

CISB314 Database 2 26
• When a system with concurrent transactions
crashes and recovers, it behaves in the
following manner:
• The recovery system reads the logs
backwards from the end to the last
checkpoint.
• It maintains two lists, an undo-list and a
redo-list.
• If the recovery system sees a log with
<Tn, Start> and <Tn, Commit> or just <Tn,
3. Checkpoint Commit>, it puts the transaction in the
redo-list.
Facility (cont’d) • If the recovery system sees a log with
<Tn, Start> but no commit or abort log
found, it puts the transaction in undo-
list.

• All the transactions in the undo-list are then


undone and their logs are removed. All the
transactions in the redo-list and their
previous logs are removed and then redone
before saving their logs.

CISB314 Database 2 27
Transactions

T1

T2

T3

T4

Checkpoint Failure

CISB314 Database 2 28
• A module of the DBMS that
restores the database to a correct
condition when a failure occurs
4. and then resumes processing user
requests.

29
Recovery • The recovery manager used the
backup copy and logs to restore

Manager the database.


• Type of restart used depends on
the nature of failure.
CISB314 Database 2
CISB314 Database 2 30
Maintaining Transaction Integrity

• What is a transaction?
• Transaction integrity is very important in database recovery.
• Thus, any transactions in a database must follow ACID properties
in order to ensure correct and valid data. (Do you remember ACID
properties?

CISB314 Database 2 31
Recovery &
Restart Procedures
• The type of recovery procedure that is used in a given situation depends on the nature
of failure.
• The most frequently used recovery procedures:
1. Disk Mirroring – switch between identical copies of databases
2. Restore/Rerun – reprocess transactions against the backup
3. Backward Recovery (Rollback) – apply before images
4. Forward Recovery (Roll Forward) – apply after images

CISB314 Database 2 32
1. Disk Mirroring
o 2 copies of the database are kept and updated simultaneously.
o Media failure occurs - processing switch to the duplicate copy.
o In normal operations, changes are updated simultaneously to both
databases
o Allows fastest recovery
o Hot-swappable: damaged disk can be rebuilt from mirrored disk with no
disruption in service to user

CISB314 Database 2 33
1. Disk Mirroring (cont’d)

Advantages: Disadvantages:

• Allows fastest recovery for • Does not protect against


applications requiring high power loss or catastrophic
availability. damage.
• Has become a cheaper
option – prices for secondary
storage have come down.
• Damaged disk can be rebuilt
from mirrored disk with no
disruption in service to user.

CISB314 Database 2 34
1. Disk
Mirroring

CISB314 Database 2 35
2. Restore/Rerun
• Involves reprocessing the day’s transactions (up to the point of failure)
against the backup copy of the database
• Database is shut down
• The most recent copy of the database or file to be recovered is mounted
• All transactions that have occurred since that copy (stored on the
transaction log) are rerun

CISB314 Database 2 36
2. Restore/Rerun

Advantages: Disadvantages:

• Simple • Time consuming to


• No special restart reprocess transactions
procedures are required. • Sequencing of transactions
may be different on the
second run.
• Original Run: customer
deposit may be posted
before withdrawal
• Rerun: Withdrawal
transaction may be
attempted first.

CISB314 Database 2 37
DB2 Restore Utility

• Restore utility is the complement of backup utility


• Restores database or table space from a previously taken backup
• TAKEN AT – specify the time stamp of the database backup image. Backup
image timestamp is displayed after successful completion of a backup
• Without prompting – overrides any warnings
Example DB2 Restore

SAMPLE.0.DB2INST.NODE0000.CATN0000.20110718131210.001

RESTORE DATABASE sample FROM <db_path> TAKEN AT 20110718131210


3. Backward Recovery
• Also called rollback recovery.
• DBMS undo any changes that have occurred since the last checkpoint.
• Before images of records that have changed are applied to the database.
• Advantages:
• Reverse changes to records where the transaction has aborted or
terminated abnormally.

CISB314 Database 2 40
Backward Recovery (Rollback)

• Example: transfer 100 from CUSTOMER A account to CUSTOMER


B account
 Program reads the record for customer A and subtracts 100 from the
account balance
 Program reads the record for customer B and adds 100 to the account
balance.
 Program writes the updated record for A to the dbase.
 In attempting to write the record for B, program encounters an error
condition and cannot write the record.
 An UNDO command – recovery manager to apply the before image for
record A to restore acc balance to its original value.
3.
Backward
Recovery

CISB314 Database 2 42
4. Forward Recovery
• Also called rollforward recovery.
• DBMS starts with an earlier copy of the database, from the last checkpoint.
• After images are applied to roll the database forward to the point of failure.
• Advantages:
• Faster than Restore/Rerun
• The series of after images of the data are applied. These are copies of the
data at that point not a rerun of the processes. Therefore sequencing is
no longer an issue.

CISB314 Database 2 43
4. Forward
Recovery

CISB314 Database 2 44
Types of database failures

1. Aborted transactions
• A transaction in progress that terminates abnormally
2. Incorrect data
• Database that has been updated with incorrect, but valid data
3. System failure
• Some components of the system fails (due to power loss, loss of
communication transmission, system software failure) but the
database is not damaged
4. Database destruction
• The database is lost, destroyed or cannot be read (which is
usually caused by disk drive failure)

CISB314 Database 2 45
Responses to Database Failures

CISB314 Database 2 46
Summary

• Database backup is very important to prevent data loss or damage.


• Different types of backup can be used according to company’s need.
• Facility to restore and recover any loss data is essential to ensure any
interruption to the database can be minimized.

CISB314 Database 2 47
END

CISB314 Database 2 48
What is a business transaction?

• A business transaction is a sequence of steps that constitute some well-defined business activity.
• Examples of business transactions are Admit Patient in a hospital and Enter Customer Order in a
manufacturing company.
• Normally, a business transaction requires several actions against the database.

For example, consider the transaction Enter Customer Order.

When a new customer order is entered, the following steps may be performed by an application
program:

1. Input the order data (keyed by the user).


2. Read the CUSTOMER record (or insert record if a new customer).
3. Accept or reject the order. If Balance Due plus Order Amount does not exceed Credit Limit,
accept the order; otherwise, reject it.
4. If the order is accepted, increase Balance Due by Order Amount.
5. Store the updated CUSTOMER record. Insert the accepted ORDER record in the database.

CISB314 Database 2 49
ACID properties

Atomic - the transaction cannot be subdivided and, hence, it must be processed in its entirety or not at all. Once
the whole transaction is processed, we say that the changes are committed. If the transaction fails at any
midpoint, we say that it has aborted. For example, suppose that the program accepts a new customer order,
increases Balance Due, and stores the updated CUSTOMER record. However, suppose that the new ORDER record
is not inserted successfully (perhaps due to a duplicate Order Number key or insufficient physical file space). In this
case, we want none of the parts of the transaction to affect the database.

Consistent - any database constraints that must be true before the transaction must also be true after the
transaction. For example, if the inventory on-hand balance must be the difference between total receipts minus
total issues, this will be true both before and after an order transaction, which depletes the on hand balance to
satisfy the order.

Isolated - changes to the database are not revealed to users until the transaction is committed. For example, this
property means that other users do not know what the on-hand inventory is until an inventory transaction is
complete; this property then usually means that other users are prohibited from simultaneously updating and
possibly even reading data that are in the process of being updated. A consequence of transactions being isolated
from one another is that concurrent transactions (i.e., several transactions in some partial state of completion) all
affect the database as if they were presented to the DBMS in serial fashion.

Durable - meaning that changes are permanent. Thus, once a transaction is committed, no subsequent failure of
the database can reverse the effect of the transaction.
CISB314 Database 2 50

You might also like