You are on page 1of 6

(https://www.mssqltips.

com/) MENU

Steps to Move SQL Server Log Shipping Secondary Database


Files

By: Jugal Shah (/sqlserverauthor/47/jugal-shah/)   |   Updated: 2012-12-28   |   Comments (7)   |   Related: More (/sql-server-dba-
resources/) > Log Shipping (/sql-server-tip-category/100/log-shipping/)

Please do not scroll away - stay informed.

Dear Database Professional,

Did you know that MSSQLTips.com publishes


new SQL Server content on a daily basis as well as offers free webinars
and tutorials?

Let us help you stay informed and learn something new each day. Click here to keep informed. (https://www.mssqltips.com/get-free-sql-
server-tips/?ref=MessageHeader)

Thank you,

Greg Robidoux and Jeremy Kadlec (MSSQLTips.com Co-Founders)

Problem
With SQL Server is it possible to move the secondary database involved with Log Shipping to a different drive without disturbing the
Log Shipping configuration?  If so, what are the steps to accomplish this task?  Check out this tip to learn more.

Solution
There are scenarios where you have to move the Log Shipping secondary database to different drive due to disk space issue or to do
the maintenance of the drive. In this tip I will show you how we can move the Log Shipping secondary database without
disturbing/reconfiguring SQL Server Log Shipping.

To configure the Log Shipping please check Step by Step SQL Server Log-Shipping Setup (/sqlservertip/2301/step-by-step-sql-server-
log-shipping/)

Before we start moving secondary database files, let's take a look at the secondary database restore mode. In Log Shipping the
secondary database can be configured for Standby or NoRecovery mode.  Here is a brief explanation:

Standby Mode: In Standby mode a user can execute SELECT commands and the transaction logs restores need to be coordinated
with the SELECT commands.
NoRecovery Mode: In NoRecovery mode secondary database status is always in restoring state and users cannot issue SELECT
commands.

You can execute the below query on the Log Shipping Secondary SQL Server to determine the restore mode information:
--Execute the below script on the secondary server

declare @databaseName varchar(300)

set @databaseName = 'myLogShip' -- Secondary Database Name

-- 0 = Restore log with NORECOVERY.

-- 1 = Restore log with STANDBY.

select secondary_database,

case restore_mode

when 0 then 'No Recovery'

when 1 then 'Stand by' end AS 'restore_mode'

from msdb.dbo.log_shipping_secondary_databases

where secondary_database = @databaseName

To check the current location of the secondary database files location, execute the below query on the Secondary SQL Server.

--Execute the below script on the secondary server

declare @databaseName varchar(300)

set @databaseName = 'myLogShip' -- Secondary Database Name

SELECT name, physical_name AS CurrentLocation, state_desc

FROM sys.master_files

WHERE database_id = DB_ID(@databaseName);

To move the database to different drive, we will use the ALTER DDL command.

ALTER DATABASE database_name

MODIFY FILE ( NAME = logical_name, FILENAME = 'new_path\os_file_name' );

To move the secondary database files, your secondary database restore mode must be in No Recovery state. If the database state is
Standby Mode, your ALTER statement to move the database files will fail with the below error.

Msg 5004, Level 16, State 4, Line 1

To use ALTER DATABASE, the database must be in a writable state

in which a checkpoint can be executed.

To avoid the above situation you need to change the database restoring mode to NoRecovery as shown below as Step 1.  If the
secondary database is already running No Recovery mode than you can ignore this step.

Step 1: In SQL Server Management Studio, right click on the primary database -> select Properties -> click on the Transaction Log
Shipping page -> click on the Secondary server instances and database configuration ellipse (...) as shown below and it will open the
Secondary Database Setting dialog box which is the second image below.
Click on the Restore Transaction Log tab and select No recovery mode radio button in Secondary Database Setting dialog box as
shown below. Database recovery mode will change to No recovery mode on next transaction log restore.

Step 2: Once the secondary database state is changed to No Recovery Mode, disable the Log Shipping Restore Job on secondary
server. To disable the job in SQL Server Management Studio, navigate to root | SQL Server Agent | Jobs | Log Shipping Restore Job
then right click on the job and click the Disable option as shown below.
Step 3: On the Secondary Log Shipping SQL Server, execute the ALTER database command as shown below to identify the new
location of the secondary database and log file.

-- Execute the below script on secondary server

-- Specify the secondary database, file name and new location

ALTER DATABASE myLogShip

MODIFY FILE ( NAME = myLogShip, FILENAME = 'D:\SQLMonitor\myLogShip.mdf' );

ALTER DATABASE myLogShip

MODIFY FILE ( NAME = myLogShip_Log, FILENAME = 'D:\SQLMonitor\myLogShip_Log.ldf' );

Step 4: Stop the Secondary SQL Server instance services. Go to SQL Server Configuration Manager and stop the secondary instance
services.

Step 5: Move the Log Shipping Secondary database files to the new location in Windows Explorer as mentioned in step 3.

Step 6: Restart the secondary instance SQL Services in SQL Server Configuration Manager.

Step 7: Enable the Log Shipping database restore SQL Server Agent Job on the Secondary SQL Server see step 2 as a point of
reference.

Step 8: Verify the log shipping SQL Server Agent jobs are working properly.

Next Steps
Document your SQL Server Log Shipping configuration for all servers.
Monitor your storage utilization and validate there is enough storage on the target location to move the SQL Server database.
Check out these related tips:
SQL Server Log Shipping Tips (/sql-server-tip-category/100/log-shipping/)
Get Started Now - Click here to get your free 14 day trial of SolarWinds Database Insights
(https://www.solarwinds.com/database-insights-sql-server/registration?CMP=SYN-VID-MSSQL-
SW_WW_X_RR_FLT_CQ_EN_DISSQL_SW-DISQL-X_X_X_VID_X-X)

Related Articles
SQL Server Log Shipping (/sqlservertip/1158/sql-server-log-shipping/)

Automate Restoration of Log Shipping Databases for Failover in SQL Server (/sqlservertip/1516/automate-restoration-of-log-shipping-databases-for-failover-in-sql-server/)

Step By Step SQL Server Log Shipping (/sqlservertip/2301/step-by-step-sql-server-log-shipping/)

Different ways to monitor Log Shipping for SQL Server databases (/sqlservertip/2553/different-ways-to-monitor-log-shipping-for-sql-server-databases/)

SQL Server Log Shipping to a Different Domain or Workgroup (/sqlservertip/2562/sql-server-log-shipping-to-a-different-domain-or-workgroup/)

Steps to add Log Shipping monitor into an existing SQL Server (/sqlservertip/2799/steps-to-add-log-shipping-monitor-into-an-existing-sql-server/)

How to add database file to a Log Shipped database in SQL Server (/sqlservertip/2824/how-to-add-database-file-to-a-log-shipped-database-in-sql-server/)

Steps to rollback database changes without impacting SQL Server Log Shipping (/sqlservertip/3069/steps-to-rollback-database-changes-without-impacting-sql-server-log-shipping/)

Fix SQL Server Log Shipping After a New Database File has been Added (/sqlservertip/3122/fix-sql-server-log-shipping-after-a-new-database-file-has-been-added/)

Change SQL Server log shipped database from Restoring to Standby Read-Only (/sqlservertip/3574/change-sql-server-log-shipped-database-from-restoring-to-standby-readonly/)

Change the restore mode of a secondary SQL Server database in Log shipping with SSMS (/sqlservertip/3600/change-the-restore-mode-of-a-secondary-sql-server-database-in-log-shipping-with-
ssms/)

Temporarily Change SQL Server Log Shipping Database to Read Only (/sqlservertip/3665/temporarily-change-sql-server-log-shipping-database-to-read-only/)

SQL Server Log Shipping Monitoring and Email Notification (/sqlservertip/4463/sql-server-log-shipping-monitoring-and-email-notification/)

Monitoring SQL Server Log Shipping with sqlcmd (/sqlservertip/5134/monitoring-sql-server-log-shipping-with-sqlcmd/)

Faster Way to Resync Log Shipped SQL Server Database After Restore Failure (/sqlservertip/5684/faster-way-to-resync-log-shipped-sql-server-database-after-restore-failure/)

Cleanup SQL Server Log Shipping Alerts After Failover (/sqlservertip/6159/cleanup-sql-server-log-shipping-alerts-after-failover/)

Configure Log Shipping for SQL Server on Linux (/sqlservertip/6527/configure-log-shipping-for-sql-server-on-linux/)

Popular Articles
Date and Time Conversions Using SQL Server (/sqlservertip/1145/date-and-time-conversions-using-sql-server/)

Format SQL Server Dates with FORMAT Function (/sqlservertip/2655/format-sql-server-dates-with-format-function/)

SQL Server CROSS APPLY and OUTER APPLY (/sqlservertip/1958/sql-server-cross-apply-and-outer-apply/)

SQL Server Cursor Example (/sqlservertip/1599/cursor-in-sql-server/)

Rolling up multiple rows into a single row and column for SQL Server data (/sqlservertip/2914/rolling-up-multiple-rows-into-a-single-row-and-column-for-sql-server-data/)

SQL Server DROP TABLE IF EXISTS Examples (/sqlservertip/6769/sql-server-drop-table-if-exists/)

( )
SQL NOT IN Operator (/sqlservertip/6904/sql-not-in-operator/)

How to tell what SQL Server versions you are running (/sqlservertip/1140/how-to-tell-what-sql-server-version-you-are-running/)

Add and Subtract Dates using DATEADD in SQL Server (/sqlservertip/2509/add-and-subtract-dates-using-dateadd-in-sql-server/)

Resolving could not open a connection to SQL Server errors (/sqlservertip/2340/resolving-could-not-open-a-connection-to-sql-server-errors/)

Using MERGE in SQL Server to insert, update and delete at the same time (/sqlservertip/1704/using-merge-in-sql-server-to-insert-update-and-delete-at-the-same-time/)

SQL Convert Date to YYYYMMDD (/sqlservertip/6452/sql-convert-date-to-yyyymmdd/)

SQL Server Loop through Table Rows without Cursor (/sqlservertip/6148/sql-server-loop-through-table-rows-without-cursor/)

SQL Server Row Count for all Tables in a Database (/sqlservertip/2537/sql-server-row-count-for-all-tables-in-a-database/)

Concatenate SQL Server Columns into a String with CONCAT() (/sqlservertip/2985/concatenate-sql-server-columns-into-a-string-with-concat/)

Display Line Numbers in a SQL Server Management Studio Query Window (/sqlservertip/2542/display-line-numbers-in-a-sql-server-management-studio-query-window/)

Ways to compare and find differences for SQL Server tables and data (/sqlservertip/2779/ways-to-compare-and-find-differences-for-sql-server-tables-and-data/)

Searching and finding a string value in all columns in a SQL Server table (/sqlservertip/1522/searching-and-finding-a-string-value-in-all-columns-in-a-sql-server-table/)

How to Get Current Date in SQL Server (/sqlservertip/6817/sql-current-date/)

SQL Server Database Stuck in Restoring State (/sqlservertip/5460/sql-server-database-stuck-in-restoring-state/)

About the author


(/sqlserverauthor/47/jugal-shah/)
Jugal Shah has 8+ years of extensive SQL Server experience and has worked on SQL Server 2000, 2005, 2008
and 2008 R2.

View all my tips (/sqlserverauthor/47/jugal-shah/)

Article Last Updated: 2012-12-28

You might also like