In this blog post we will describe how to migrate a SQLServer Database between different SQL Instances using the Backup and Restore procedure.
The main advantage from migrating databases this way, is that it is less error prone than other procedures. I would recommend using this method on very static databases or databases where you can afford a longer downtime. A disadvantage would be that there needs to be a downtime when migrating this database, so if a requirement is that downtime must be zero, we would recommend using an other migrating procedure.
This example will be using 2 instances SQLServer 2017 so the compatibility level will remain the same (140) and we don’t need to take this in account. If you need to change compatibility level (in this case to 140. Look https://docs.microsoft.com/en-us/sql/t-sql/statements/alter-database-transact-sql-compatibility-level?view=sql-server-2017 for more info) you can use following command:
Alter database [DATABASE]
SET COMPATIBILITY_LEVEL = 140
We don’t really need any data so we will be using an empty database for this example.
To Create the database use following script :
CREATE DATABASE [ExampleDatabase]
GO
After a refresh of your object explorer the database will appear.
Now we can start with the Backup. You can either Backup the Database by using the GUI or you can use T-SQL. If you use the GUI you can always choose later to generate a script and still execute it as T-SQL.
To Backup the Database with the GUI you need to right click on the database and select Tasks then Backup. Now you should get following window. (see figure 1)
Figure 1 Backup General page
In this window we can specify which Database we want to Backup. Since we started on ExampleDatabase it already says ExampleDatabase. Here we also get information about which Recovery model is active (https://docs.microsoft.com/en-us/sql/relational-databases/backup-restore/recovery-models-sql-server?view=sql-server-2017 for more info), in this case FULL. This mean that we can choose between 3 Backup types ( more info https://www.mssqltips.com/sqlservertutorial/6/types-of-sql-server-backups/). We will choose Full in this example.
Destination means where you want to store your Backup. I will store my Backup as file here : C:\Backup\ExampleDatabase_FULL_02052018.bak . It is normally bad practice to store your Backups on the C:\ drive but since I only got 1 drive there aren’t any other options. When you press the add button you get the following (see figure 2)
Figure 2 setting up Backup Destination
If you need to change your Backup destination you can do this by filling in the name yourself (don’t forget that you are also naming your File) or pressing the right button with 3 dots and navigate in your folder structure.
Then press OK. You will now have 2 Destinations on you window, you can remove the one not wanted by selecting it and pressing remove button on the right.
Next we go to the Media Options page (see figure 3)
Figure 3 Media Option page
Overwrite media is not really relevant in this example. Since we use a unique name for the Backup Filename we don’t need to overwrite.
Reliability we will be using, It’s important to see that you don’t Backup a corrupt database, here we use WITH CHECKSUM option and the verify backup options for. These option checks that your backup can be successfully restored without errors. It verifies that each page of the database does not have a checksum or torn page error.
Continue on error stays disabled.
Next is the Backup Options page (see figure 4)
Figure 4 Backup Options page
On this page we can set a description if needed about the database. We can set expiration options, after x days is to define how many days you want to keep it or if you want a specific date you can use on: mm/dd/yyyy .
Compression won’t help much in my example, but with bigger databases it is recommended to enable compression.
I also won’t be using Encryption in this example, but I also recommend using it especially when transferring the database across the network.
Now the Backup is ready to complete, so you can press OK if you want to Backup it with the GUI or on the top press Script and a new query window will open with following script :
BACKUP DATABASE [ExampleDatabase] TO DISK = N'C:\Backup\ExampleDatabase_FULL_02052018.bak' WITH NOFORMAT, NOINIT,
NAME = N'ExampleDatabase-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, COMPRESSION, STATS = 10, CHECKSUM
GO
-- next part does the verification of the backup
declare @backupSetId as int
select @backupSetId = position from msdb..backupset where database_name=N'ExampleDatabase' and backup_set_id=(select max(backup_set_id)
from msdb..backupset where database_name=N'ExampleDatabase' )
if @backupSetId is null begin raiserror(N'Verify failed. Backup information for database ''ExampleDatabase'' not found.', 16, 1) end
RESTORE VERIFYONLY FROM DISK = N'C:\Backup\ExampleDatabase_FULL_02052018' WITH FILE = @backupSetId, NOUNLOAD, NOREWIND
GO
Press f5 to execute.
Now that the Backup has been done we will start now with the Restore.
Again we will first be using the GUI and then getting the script.
To Restore you go the the TARGET instance (see figure 5)
Figure 5
Now you get following window (see figure 6)
Figure 6
To select the database file first click on the button with 3 dots then add and then you select your Backup file as seen in figure 7. Then ok and ok.
Figure 7
Next we go to the files page (see figure 8). Since we are migrating from a different server, the possibility that there will be a different disk setting is great. As previously mentioned, I only have 1 disk but I will still need to specify a different folder name. This will be almost always necessary because of the change in the disk or file structures.
This we can do by enabling Relocate all Files to folder. I will use C:\Target\Data for data file folder, this is where my main data file (.mdf) will be stored. For the log file folder I will be using C:\Target\Log this is where my transaction log (.ldf) will be stored.
For this example we don’t need to change anything in the options page.
Figure 8
Next we can press OK to start restoring or Script I you want to generate following script :
USE [master]
RESTORE DATABASE [ExampleDatabase] FROM DISK = N'C:\Backup\ExampleDatabase_FULL_02052018.bak' WITH FILE = 1,
MOVE N'ExampleDatabase' TO N'C:\Target\Data\ExampleDatabase.mdf',
MOVE N'ExampleDatabase_log' TO N'C:\Target\Log\ExampleDatabase_log.ldf',
NOUNLOAD, STATS = 5
GO
Press f5 to execute.
Now you should have successfully migrated the ExampleDatabase from your source server to your target server.
The main advantage from migrating databases this way, is that it is less error prone than other procedures. I would recommend using this method on very static databases or databases where you can afford a longer downtime. A disadvantage would be that there needs to be a downtime when migrating this database, so if a requirement is that downtime must be zero we would recommend using another migrating procedure.
© 2023 Kohera
Crafted by
© 2022 Kohera
Crafted by
Cookie | Duration | Description |
---|---|---|
ARRAffinity | session | ARRAffinity cookie is set by Azure app service, and allows the service to choose the right instance established by a user to deliver subsequent requests made by that user. |
ARRAffinitySameSite | session | This cookie is set by Windows Azure cloud, and is used for load balancing to make sure the visitor page requests are routed to the same server in any browsing session. |
cookielawinfo-checkbox-advertisement | 1 year | Set by the GDPR Cookie Consent plugin, this cookie records the user consent for the cookies in the "Advertisement" category. |
cookielawinfo-checkbox-analytics | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics". |
cookielawinfo-checkbox-functional | 11 months | The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional". |
cookielawinfo-checkbox-necessary | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary". |
cookielawinfo-checkbox-others | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other. |
cookielawinfo-checkbox-performance | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance". |
CookieLawInfoConsent | 1 year | CookieYes sets this cookie to record the default button state of the corresponding category and the status of CCPA. It works only in coordination with the primary cookie. |
elementor | never | The website's WordPress theme uses this cookie. It allows the website owner to implement or change the website's content in real-time. |
viewed_cookie_policy | 11 months | The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data. |
Cookie | Duration | Description |
---|---|---|
__cf_bm | 30 minutes | Cloudflare set the cookie to support Cloudflare Bot Management. |
pll_language | 1 year | Polylang sets this cookie to remember the language the user selects when returning to the website and get the language information when unavailable in another way. |
Cookie | Duration | Description |
---|---|---|
_ga | 1 year 1 month 4 days | Google Analytics sets this cookie to calculate visitor, session and campaign data and track site usage for the site's analytics report. The cookie stores information anonymously and assigns a randomly generated number to recognise unique visitors. |
_ga_* | 1 year 1 month 4 days | Google Analytics sets this cookie to store and count page views. |
_gat_gtag_UA_* | 1 minute | Google Analytics sets this cookie to store a unique user ID. |
_gid | 1 day | Google Analytics sets this cookie to store information on how visitors use a website while also creating an analytics report of the website's performance. Some of the collected data includes the number of visitors, their source, and the pages they visit anonymously. |
ai_session | 30 minutes | This is a unique anonymous session identifier cookie set by Microsoft Application Insights software to gather statistical usage and telemetry data for apps built on the Azure cloud platform. |
CONSENT | 2 years | YouTube sets this cookie via embedded YouTube videos and registers anonymous statistical data. |
vuid | 1 year 1 month 4 days | Vimeo installs this cookie to collect tracking information by setting a unique ID to embed videos on the website. |
Cookie | Duration | Description |
---|---|---|
ai_user | 1 year | Microsoft Azure sets this cookie as a unique user identifier cookie, enabling counting of the number of users accessing the application over time. |
VISITOR_INFO1_LIVE | 5 months 27 days | YouTube sets this cookie to measure bandwidth, determining whether the user gets the new or old player interface. |
YSC | session | Youtube sets this cookie to track the views of embedded videos on Youtube pages. |
yt-remote-connected-devices | never | YouTube sets this cookie to store the user's video preferences using embedded YouTube videos. |
yt-remote-device-id | never | YouTube sets this cookie to store the user's video preferences using embedded YouTube videos. |
yt.innertube::nextId | never | YouTube sets this cookie to register a unique ID to store data on what videos from YouTube the user has seen. |
yt.innertube::requests | never | YouTube sets this cookie to register a unique ID to store data on what videos from YouTube the user has seen. |
Cookie | Duration | Description |
---|---|---|
WFESessionId | session | No description available. |