4. Backup Database
4. Backup Database
Ms-SQL Server
Database Model
• A recovery model is a database configuration option that determines
the type of backup that one could perform, and provides the ability to
restore the data or recover it from a failure.
• The recovery model decides how the transaction log of a database
should be maintained and protects the data changes in a specific
sequence, which may later be used for a database restore operation.
Types of recovery models
All SQL Server database backup, restore, and recovery operations
are based on one of three available recovery models:
• SIMPLE
• FULL
• BULK_Logged
Database Model(SIMPLE)
• The SIMPLE recovery model is the simplest among the It supports:
available models. It supports full, differential, and file level Full backup
backups. Transaction log backups are not supported. The log Differential backup
space is reused whenever the SQL Server background Copy-Only backup
process checkpoint operation occurs. The inactive portion of
File backup
the log file is removed and is made available for reuse.
Partial backup
• Reasons to choose the simple database recovery model
1. Most suited for Development and Test databases
2. Simple reporting or application database, where data loss
is acceptable
3. The point-of-failure recovery is exclusively for full and
differential backups
4. No administrative overhead
Database Model(FULL)
In this recovery model, all the transactions (DDL (Data Definition Language) + DML (Data It supports all type of following
Manipulation Language)) are fully recorded in the transaction log file. The log sequence is backups
unbroken and is preserved for the databases restore operations. Unlike the Simple recovery • Full backup
model, the transaction log file is not auto-truncated during CHECKPOINT operations. • Differential backup
All restore operations are supported, including point-in-time restore, page restore and file • Transaction log backup
restore. • Copy-Only backup
• File and/or file-group backup
Reasons to choose the full database recovery model: • Partial backup
FILEGROUP [Secondary]
( NAME = N'ADVPartialBackup_2',FILENAME = N'C:\bak\PartialBackup_2.ndf' ,
SIZE = 5000KB , FILEGROWTH = 1024KB )
LOG ON
( NAME = N'ADVPartialBackup_Log',FILENAME = N'C:\bak\PartialBackup_log.ldf' ,
SIZE = 1024KB , FILEGROWTH = 10%)
GO
Partial backups
• Let’s change the recovery model of the database to SIMPLE using the
following ALTER statement
ALTER DATABASE [AdventureWorks2022] SET RECOVERY SIMPLE
Now, set the secondary file-group to READONLY mode
ALTER DATABASE [AdventureWorks2022] MODIFY FILEGROUP [Secondary] READONLY
Initiate a backup using the READ_WRITE_FILEGROUPS option
BACKUP DATABASE [AdventureWorks2022] READ_WRITE_FILEGROUPS
TO DISK = N'C:\bak\PartialBackup_Full.bak’
GO
File and File Group Backups
• This topic is relevant for SQL Server databases that contain multiple
files or filegroups. File backups of read-only filegroups can be
combined with partial backups. Partial backups include all the
read/write filegroups and, optionally, one or more read-only filegroups.
• Let’s create a database with multiple files and filegroups.
Query Check Backup Database
• We can check database size by query:
SELECT
database_name = DB_NAME(database_id)
, log_size_mb = CAST(SUM(CASE WHEN type_desc = 'LOG' THEN size END) * 8. / 1024 AS DECIMAL(8,2))
, row_size_mb = CAST(SUM(CASE WHEN type_desc = 'ROWS' THEN size END) * 8. / 1024 AS DECIMAL(8,2))
, total_size_mb = CAST(SUM(size) * 8. / 1024 AS DECIMAL(8,2))
FROM sys.master_files WITH(NOWAIT)
WHERE database_id = DB_ID() -- for current db
GROUP BY database_id