- SQL Basics
- SQL Introduction
- SQL Syntax
- SQL Sample Database
- SQL SELECT
- SQL WHERE
- SQL ORDER BY
- SQL DISTINCT
- SQL LIMIT
- SQL FETCH
- SQL AND
- SQL OR
- SQL BETWEEN
- SQL IN
- SQL LIKE
- SQL IS NULL
- SQL Comparison Operators
- SQL Logical Operators
- SQL Alias
- SQL CASE
- Joins and Subqueries
- SQL INNER JOIN
- SQL LEFT JOIN
- SQL RIGHT JOIN
- SQL FULL OUTER JOIN
- SQL SELF JOIN
- SQL CROSS JOIN
- SQL Subquery
- SQL Correlated Subquery
- SQL UNION
- SQL INTERSECT
- SQL EXCEPT
- Aggregate Functions
- SQL AVG
- SQL COUNT
- SQL MAX
- SQL MIN
- SQL SUM
- SQL GROUP BY
- SQL HAVING
- SQL ROLLUP
- SQL CUBE
- SQL GROUPING SETS
- Database Management
- SQL CREATE DATABASE
- SQL ALTER DATABASE
- SQL DROP DATABASE
- SQL BACKUP DATABASE
- SQL SHOW DATABASES
- SQL SELECT DATABASE
- Table Management
- SQL CREATE TABLE
- SQL ALTER TABLE
- SQL ADD COLUMN
- SQL DROP COLUMN
- SQL DROP TABLE
- SQL TRUNCATE TABLE
- SQL SHOW TABLES
- SQL RENAME TABLE
- SQL Constraints
- SQL Primary Key
- SQL Foreign Key
- SQL UNIQUE Constraint
- SQL CHECK Constraint
- SQL NOT NULL Constraint
SQL BACKUP DATABASE
In the world of software development, data is your most valuable asset. The BACKUP DATABASE statement is a critical command used to create a physical copy of your SQL Server database. This ensures that if your server crashes, a disk fails, or a developer accidentally runs a DELETE statement without a WHERE clause, you can restore your data to a healthy state.
Key Features of SQL BACKUP DATABASE
- Disaster Recovery: It creates a redundant copy of the data that can be moved to off-site storage or a different server.
- Security: Executing these commands requires administrative privileges (typically
sysadminfixed server roles ordb_owneranddb_backupoperatorroles). - Flexibility: SQL Server supports different backup types Full, Differential, and Transaction Log allowing you to balance between disk space usage and recovery speed.
SQL BACKUP DATABASE Syntax (For SQL Server)
The basic syntax for a full backup is straightforward. You specify the database name and the physical path on the server where the .bak file should be generated.
BACKUP DATABASE database_name
TO DISK = 'file_path\backup_file.bak';
- database_name: The exact name of the database you want to protect.
- file_path: The absolute path on the server's local drive or a network share.
- backup_file.bak: The filename (traditionally using the
.bakextension).
C:\Users\Documents. The SQL Server service runs under its own account; if that account doesn't have "Write" permissions to the folder, the backup will fail with an "Access Denied" error.
Example of SQL BACKUP DATABASE (Full Backup)
A Full Backup captures everything: the schema, the data, and the parts of the transaction log required to recover the database to a consistent state.
BACKUP DATABASE SchoolDB
TO DISK = 'D:\Backups\SchoolDB_Full.bak'
WITH FORMAT,
MEDIANAME = 'SchoolDB_Backups',
NAME = 'Full Backup of SchoolDB';
COMPRESSION option in your backup script. Modern SQL versions can compress backup files, significantly reducing disk space and making network transfers faster.
Output:
The SchoolDB database is fully exported into the .bak file at the specified path. This file is now a standalone "snapshot" of your data at that exact moment.
Creating a Differential Backup
As databases grow into terabytes, taking a full backup every hour becomes impossible. A differential backup solves this by only recording the data that has changed since the last full backup.
BACKUP DATABASE SchoolDB
TO DISK = 'D:\Backups\SchoolDB_Diff.bak'
WITH DIFFERENTIAL;
Why use this?
Differential backups are much smaller and faster than full backups. To restore, you would restore the last full backup first, followed by the most recent differential backup.
Creating a Transaction Log Backup
If your database is in "Full Recovery Model," you can back up the Transaction Log. This records every individual transaction (Insert, Update, Delete) that has occurred.
BACKUP LOG SchoolDB
TO DISK = 'D:\Backups\SchoolDB_Log.trn';
Point-in-Time Recovery
Transaction log backups are powerful because they allow for point-in-time recovery. If a user deletes a table at 10:05 AM, you can restore your database to 10:04 AM exactly, minimizing data loss to just a few seconds.
Summary
BACKUP DATABASEis your primary tool for creating a full snapshot of your data.- The
WITH DIFFERENTIALflag captures only changes since the last full backup, saving time and storage. BACKUP LOGallows for granular recovery, letting you restore to a specific minute or second.- Storage Strategy: Always store your backup files on a different physical disk or cloud storage than your live data files to protect against hardware failure.