How to Set Up Log Shipping for SQL Server Replication
Log shipping is a reliable SQL Server disaster recovery (DR) solution that automatically backs up transaction logs from a primary database and restores them on one or more secondary servers. While log shipping is primarily used for high availability (HA) and DR, it can also complement SQL Server replication in certain scenarios.
This guide covers:
✅ What is Log Shipping?
✅ How It Works
✅ Step-by-Step Setup
✅ Monitoring & Failover
✅ Best Practices
1. What is Log Shipping?
Log shipping automates the process of:
-
Backing up transaction logs on the primary server
-
Copying them to a secondary server
-
Restoring them to keep the secondary database in sync
Key Benefits
✔ Disaster Recovery – Maintain a warm standby server
✔ Read-Only Reporting – Use the secondary for queries (with a delay)
✔ Low-Cost HA – No need for expensive clustering
2. How Log Shipping Works
The process involves three main jobs:
-
Backup Job – Runs on the primary server to back up transaction logs.
-
Copy Job – Moves backup files to the secondary server.
-
Restore Job – Applies logs to the secondary database.
⚠ Note: Log shipping is not real-time—there’s always a delay (minutes to hours).
3. Step-by-Step Setup
Prerequisites
-
SQL Server Standard or Enterprise Edition
-
Primary & secondary servers connected via network
-
Sufficient disk space for log backups
Step 1: Configure the Primary Database for Log Shipping
-
Enable Full or Bulk-Logged Recovery Model
ALTER DATABASE [YourDB] SET RECOVERY FULL;
-
Take a Full Backup (Initial synchronization)
BACKUP DATABASE [YourDB] TO DISK = 'C:\Backups\YourDB_Full.bak' WITH INIT;
-
Set Up Log Shipping in SSMS
-
Right-click YourDB → Properties → Transaction Log Shipping
-
Check "Enable this as a primary database"
-
Click "Backup Settings" and configure:
-
Backup folder location (e.g.,
\\PrimaryServer\LogShipping
) -
Schedule (e.g., every 15 minutes)
-
-
Step 2: Add a Secondary Server
-
Restore the Full Backup on Secondary (WITH NORECOVERY)
RESTORE DATABASE [YourDB] FROM DISK = 'C:\Backups\YourDB_Full.bak' WITH NORECOVERY, REPLACE;
-
Configure Secondary in SSMS
-
Click "Add" under Secondary Servers
-
Enter secondary server details
-
Set Restore Mode:
-
Standby Mode (Readable with log restore delay)
-
No Recovery Mode (No user access until failover)
-
-
Step 3: Monitor Log Shipping
-
Check Status in SSMS:
-
Under Database Properties → Transaction Log Shipping
-
-
Query Log Shipping Tables:
SELECT primary_database, secondary_server, last_restored_file, last_restored_date FROM msdb.dbo.log_shipping_monitor_secondary;
4. Failover to Secondary (Disaster Recovery)
If the primary fails:
-
Apply Any Remaining Log Backups
RESTORE LOG [YourDB] FROM DISK = 'C:\Backups\YourDB_Last.trn' WITH RECOVERY;
-
Reconfigure Applications to point to the secondary server.
5. Best Practices
✔ Monitor Log Shipping Gaps (Avoid large delays)
✔ Test Failover Regularly (Ensure DR works when needed)
✔ Use Compression (Reduce backup size & transfer time)
✔ Secure Backup Location (Prevent unauthorized access)
Conclusion
Log shipping is a simple, cost-effective way to maintain a standby SQL Server database. While it doesn’t replace Always On Availability Groups for real-time HA, it’s ideal for disaster recovery and reporting offloading.
Comments
Post a Comment