BACKUP AND RESTORE
BACKUP AND RESTORE
Introduction
• Data loss can be a catastrophic event for any organization.
• Microsoft SQL Server provides robust backup and recovery
mechanisms to safeguard critical data.
Why Backup is Crucial
• Data Recovery: Restore data after accidental deletion, hardware
failures, or software corruption.
• Disaster Recovery: Recover from major disasters like fires, floods, or
cyberattacks.
• Compliance: Meet regulatory requirements for data retention and
security.
• Testing and Development: Create copies of databases for testing and
development purposes without affecting production data.
Types of SQL Server Backups
1.Full Backup:
A complete copy of the entire database.
Time-consuming but provides a complete restore point.
Typically performed less frequently.
2.Differential Backup:
Backs up only the changes made since the last full backup.
Faster than full backups.
Requires a recent full backup for recovery.
3.Transaction Log Backup:
Captures changes made to the database since the last transaction log backup.
Used for point-in-time recovery.
Essential for high-availability solutions.
Backup Strategies
• 3-2-1 Rule:
3 copies of your data.
2 different media types (e.g., disk and tape).
1 off-site copy for disaster recovery.
• Backup Frequency:
Determine backup frequency based on data criticality and recovery time objectives
(RTO).
Consider factors like business hours and transaction volume.
• Backup Destinations:
• Local disk, network shared folders, tape drives, cloud storage (Azure Blob
Storage, AWS S3).
Practical Examples
1. Full Database Backup:
SQL
BACKUP DATABASE MyDatabase
TO DISK = 'C:\Backups\MyDatabase_Full.bak'
WITH INIT, FORMAT, COMPRESSION;
INIT:
Overwrites any existing data on the backup media.
Ensures a clean slate for the new backup.
Useful for full backups to prevent issues with old backup data.
FORMAT:
Similar to INIT, prepares the backup media for a new backup.
Removes any existing backup sets on the media.
COMPRESSION:
Reduces the size of the backup file.Saves storage space.
Can improve backup and restore performance
2. Differential Database Backup:
SQL
BACKUP DATABASE MyDatabase
TO DISK = 'C:\Backups\MyDatabase_Diff.bak'
WITH DIFFERENTIAL, COMPRESSION;
• WITH DIFFERENTIAL OPTION
• Specifies that the backup should be a differential backup.
• Only backs up the changes made since the last FULL backup of the
database.Faster than full backups.
• Requires a recent full backup for recovery.
3. Transaction Log Backup:
SQL
BACKUP LOG MyDatabase
TO DISK = 'C:\Backups\MyDatabase_Log.trn'
WITH NO_TRUNCATE, COMPRESSION;
WITH NO_TRUNCATE
Used with TRANSACTION LOG backups.
Prevents the transaction log from being truncated after the backup is taken.
This is crucial for scenarios like:
Log Shipping: Replicating transaction logs to a standby server.
Point-in-Time Recovery: Restoring the database to a specific point in time.
Restore Scenarios
Full Database Restore:
SQL