SQL Server Backup Scripts
SQL Server Backup Scripts
Full, Differential, Transaction Log, Copy-Only, Validation & DBA Utility Scripts
Introduction
A reliable SQL Server backup strategy is one of the most important responsibilities of a DBA. Backups protect databases against accidental deletion, corruption, failed deployments, infrastructure failures, and disaster-recovery scenarios.
This page brings together practical T-SQL scripts for common SQL Server backup and validation activities, including full, differential, transaction log, copy-only, striped, tail-log, backup-history, and recovery-model checks.
Contents
1. Full Database Backup 2. Differential Backup 3. Transaction Log Backup 4. Copy-Only Backup 5. Backup to Multiple Files 6. Verify a Backup 7. Tail-Log Backup 8. Backup All User Databases 9. Check Backup History 10. Check Recovery Model 11. Table-Level Safety Copy 12. Example Production Backup Strategy1. Full Database Backup
A full database backup provides the primary data backup used as the foundation for many SQL Server recovery sequences.
- COMPRESSION can reduce backup size and storage I/O, with additional CPU usage.
- CHECKSUM enables backup checksums and additional page verification during backup.
- STATS = 10 reports approximate progress at 10% intervals.
- INIT overwrites existing backup sets on the target media, so use it deliberately.
INIT. Verify that the target file does not contain backup sets that must be retained.
2. Differential Backup
A differential backup contains extents changed since its differential base, normally the most recent conventional full database backup.
3. Transaction Log Backup
Transaction log backups are fundamental to point-in-time recovery when using the FULL or BULK_LOGGED recovery model and maintaining an appropriate log-backup chain.
4. Copy-Only Backup
A copy-only full backup is useful when an ad-hoc full backup is required without changing the normal differential base.
This can be useful before troubleshooting, migrations, controlled deployments, or other one-time activities where the regular backup strategy should remain unchanged.
5. Backup to Multiple Files
SQL Server can stripe a backup across multiple backup devices. This can improve throughput when the underlying storage architecture provides sufficient independent I/O capacity.
6. Verify a Backup
RESTORE VERIFYONLY provides useful validation but does not prove
that a complete recovery operation will succeed. Regularly perform actual test restores.
7. Tail-Log Backup
A tail-log backup attempts to capture transaction log records that have not yet been backed up before beginning an appropriate recovery sequence.
NO_TRUNCATE should not be added automatically to every tail-log backup.
8. Backup All Online User Databases
The following example performs a full backup of each online user database and creates a separate timestamped backup file.
9. Check Last Backup Dates
Displaying full, differential, and transaction log backups separately provides a much better operational view than displaying only the most recent backup of any type.
A recent full backup alone does not confirm that the transaction-log backup schedule is healthy.
10. Check Database Recovery Models
| Recovery Model | Log Backups | Typical Consideration |
|---|---|---|
| SIMPLE | Not part of a normal log-backup chain | Used where transaction-log point-in-time recovery is not required |
| FULL | Yes | Supports log backups and point-in-time recovery when the chain is maintained |
| BULK_LOGGED | Yes | Special-purpose recovery model with additional considerations for minimally logged operations |
11. Table-Level Safety Copy
SELECT INTO can create a copy of table data, but this is
not a SQL Server database backup.
A table-level copy can still be useful before a controlled cleanup, data-cleansing activity, or corrective update.
Validate the Data Copy
SELECT INTO does not reproduce all characteristics of the source table.
Indexes, triggers, foreign keys, permissions, and other objects or attributes require separate consideration.
12. Example Production Backup Strategy
There is no single backup schedule suitable for every SQL Server production database. The design should start from the business Recovery Point Objective (RPO) and Recovery Time Objective (RTO).
An example starting design for a database using FULL recovery could look like this:
| Frequency / Event | Action | Purpose |
|---|---|---|
| Weekly | Full Backup | Establish a data backup baseline |
| Daily | Differential Backup | Reduce the amount of work required during restore |
| Every 15 minutes | Transaction Log | Example for a 15-minute RPO requirement |
| Before selected changes | Copy-Only Full | Ad-hoc recovery point without changing the differential base |
| Regularly | Test Restore | Prove actual recoverability |
Production Backup Checklist
- Define business RPO and RTO requirements.
- Select the appropriate SQL Server recovery model.
- Schedule full, differential, and transaction log backups according to recovery requirements.
- Use CHECKSUM where appropriate.
- Monitor SQL Server Agent backup failures.
- Monitor backup duration and storage consumption.
- Verify backup retention requirements.
- Protect backup files from unauthorized modification or deletion.
- Maintain required TDE certificates and encryption keys.
- Perform complete restore tests regularly.
- Include DBCC CHECKDB in the broader integrity strategy.
- Document and rehearse the disaster-recovery runbook.
Key Takeaways
- Full backups provide an important foundation for database recovery.
- Differential backups capture changed extents relative to the differential base.
- Transaction log backups support point-in-time recovery under the appropriate recovery model.
- Copy-only full backups provide an ad-hoc recovery point without changing the differential base.
- Striped backups can improve throughput when the storage architecture supports parallel I/O.
- RESTORE VERIFYONLY is useful, but an actual test restore provides stronger recovery assurance.
- Tail-log backups can preserve the latest log records in appropriate recovery scenarios.
- A SELECT INTO table copy is not a substitute for a SQL Server database backup.
- The correct backup frequency should be derived from business RPO and RTO requirements.
Conclusion
SQL Server backups are more than a collection of BACKUP commands. A production backup strategy must connect business recovery requirements with the correct recovery model, backup types, scheduling, retention, encryption, monitoring, and restore procedures.
From a DBA perspective, the most important question is not "Did the backup job succeed?" The real question is: "Can the database be restored within the required RPO and RTO?"
A backup strategy should therefore be proven through actual restore exercises, documented recovery sequences, failure simulations, and continuous monitoring.
Comments
Post a Comment