
For an instructor lead, in-depth look at learning SQL click below.
In the world of database administration, disaster recovery planning is a critical task that needs to be well-implemented. SQL Server offers a variety of techniques for maintaining the integrity and availability of data. One such effective technique is SQL Server’s Log Shipping.
Log shipping provides a disaster recovery solution where transaction log backups from one server (Primary Server) are restored to another server (Secondary Server). In case of a failure on the Primary Server, our applications can quickly switch to the Secondary Server reducing any downtime.
Log Shipping Component
There are three main components involved in Log Shipping.
1. Primary Server
2. Secondary Server
3. Monitor Server
Here, the Primary Server contains the original data. The Secondary Server holds the backup copy of the Primary Server’s data. Transaction logs from the Primary Server are transferred and restored to the Secondary Server. The Monitor Server keeps track of all these processes.
Setting Up Log Shipping
Let’s look at the SQL code to set up log shipping. Before beginning, ensure that the database recovery model is full or bulk-logged. The procedure includes backing up the database, restoring it to the secondary server, and then setting up transaction log backups.
|
1 2 3 4 5 6 |
-- Backup the Database on Primary Server BACKUP DATABASE TestDB TO DISK = 'D:\Backup\TestDB.bak' GO |
The above code will create a backup of your database ‘TestDB’. Now, move this backup file to the Secondary Server and restore it there.
|
1 2 3 4 5 6 7 |
--Restore the Database on Secondary Server RESTORE DATABASE TestDB FROM DISK = 'D:\Backup\TestDB.bak' WITH NORECOVERY GO |
In the code above, we restore the database on the Secondary Server with the NORECOVERY option. This leaves the database in a transactionally consistent state.
Creating a Transaction Log Backup Job
Now, let’s create a Transaction Log backup job on the Primary Server which backs up the transaction logs of our database periodically to a shared location.
|
1 2 3 4 5 6 7 |
--Backup log on Primary Server BACKUP LOG TestDB TO DISK = 'D:\LogShipping\TestDB.trn' WITH NORECOVERY GO |
In the code above, we are backing up the transaction log of our ‘TestDB’ database. Remember, this operation must be set up to execute repeatedly based on your requirement (like every 10 mins, 20 mins, etc.)
Restoring Transaction Log backup on Secondary Server
Create a job on the Secondary server that restores the transaction log backups from the shared location to the Secondary Database.
|
1 2 3 4 5 6 7 |
--Restore log on secondary Server RESTORE LOG TestDB FROM DISK = 'D:\LogShipping\TestDB.trn' WITH NORECOVERY GO |
Final Note
Log Shipping in SQL Server is a powerful feature that ensures high availability and reliability of your data. Setting this up may be a bit challenging but worth it for the robust disaster recovery capability it adds to your environment.
Always remember, a working disaster recovery plan is an integral part of any successful business operations, and log shipping is one way to achieve it.
