Migrate a SQL Server Database to the Cloud
Migrating a SQL Server database to the cloud is similar to migrating a database to a new server. The cloud can simplify the process through the use of managed services such as AWS Lightsail.
Module 1: Setting Up a Working Environment
Step 1: Create a Cloud9 Environment
Step 2: Configure the Environment with Tools
Module 2: Create a SQL Server Instance from a Lightsail Blueprint
Module 3: Create Storage for Data and Log Files
Step 2: Attach Block Storage to the Database Server
Module 4: Configure the SQL Server VPS
Step 1: Find the Disk Number of the Unformatted-But-Attached Disk
Step 2: Download the Wide World Importers Sample Database
Step 3: Create Directories for Data Files and Logs
Module 5: Restore the Database from the Backup File
Info | |
---|---|
✅ AWS Level | 200 - Intermediate |
⏱ Time to complete | 45 minutes |
💰 Cost to complete | Free when using the AWS Free Tier or USD 1.01 |
🧩 Prerequisites | - An AWS account: If you don't have an account, follow the Setting Up Your AWS Environment tutorial for a quick overview. For a quick overview for creating account follow Create Your AWS Account. - AWS credentials: Follow the instructions in Access Your Security Credentials to get your AWS credentials - A git client: Follow the instructions to Install Git for your operating system. |
💻 Code Sample | Code sample used in tutorial on GitHub |
📢 Feedback | Any feedback, issues, or just a 👍 / 👎 ? |
⏰ Last Updated | 2023-08-09 |
- How to create a browser-based environment for working with cloud resources
- How to create storage and attach it to a database server
- How to deploy and configure SQL Server with a Lightsail blueprint
- How to restore a MS SQL database on a Windows Server VPS
1
2
3
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
1
2
sudo curl "https://s3.us-west-2.amazonaws.com/lightsailctl/latest/linux-amd64/lightsailctl" -o "/usr/local/bin/lightsailctl"
sudo chmod +x /usr/local/bin/lightsailctl
1
2
aws lightsail —version
aws-cli/2.11.20 Python/3.11.3 Linux/4.14.314-237.533.amzn2.x86_64 exe/x86_64.amzn.2 prompt/off
Tip: Cloud9 is a full-featured IDE and you can save command snippets and scripts in a code repository like Github or Gitlab. Cloud9 includes Git Panel for managing repositories. An advantage of using a code repository is that it is available anywhere and you can use it to manage snippets and scripts for reuse.
aws lightsail get-blueprints
command. The get-blueprints
command lists all the blueprints available; you can find a specific blueprint by piping the output of the command to the unix grep utility.1
2
3
4
5
6
7
aws lightsail get-blueprints | grep sql
"blueprintId": "windows_server_2022_sql_2022_express",
"group": "windows_2022_sql_exp_2022",
"blueprintId": "windows_server_2022_sql_2019_express",
"group": "windows_2022_sql_exp_2019",
"blueprintId": "windows_server_2016_sql_2016_express",
"group": "windows_2016_sql_exp",
windows_server_2022_sql_2022_express
. Create a VPS with with this blueprint. In addition, the --user-data
parameter creates a directory to store the back SQL Server backup file. The --bundle-id
sets the size of the server.1
2
3
4
5
aws lightsail create-instances --instance-names pcgSQLServer \
--availability-zone us-west-2a \
--blueprint-id windows_server_2022_sql_2022_express \
--user-data "mkdir c:\backup" \
--bundle-id large_win_1_0
aws lightsail create-disk
command. Name the disk and set the availability zone.1
2
3
4
aws lightsail create-disk \
--disk-name sqlserver-data-and-logs \
--availability-zone us-west-2a \
--size-in-gb 32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
"operations": [
{
"id": "14d4be4a-955f-45d0-81f7-7a5440195c54",
"resourceName": "sqlserver-data-and-logs",
"resourceType": "Disk",
"createdAt": "2023-08-02T01:52:51.867000+00:00",
"location": {
"availabilityZone": "us-west-2a",
"regionName": "us-west-2"
},
"isTerminal": false,
"operationType": "CreateDisk",
"status": "Started",
"statusChangedAt": "2023-08-02T01:52:52.128000+00:00"
}
]
}
1
2
3
4
aws lightsail attach-disk \
--disk-name sqlserver-data-and-logs \
--disk-path /dev/xvdf \
--instance-name pcgSQLServer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
"operations": [
... ,
{
"id": "e92ee8ac-45cf-4bae-a16b-ac55fea264c7",
"resourceName": "pcgSQLServer",
"resourceType": "Instance",
"createdAt": "2023-08-02T01:54:18.196000+00:00",
"location": {
"availabilityZone": "us-west-2a",
"regionName": "us-west-2"
},
"isTerminal": false,
"operationDetails": "sqlserver-data-and-logs",
"operationType": "AttachDisk",
"status": "Started",
"statusChangedAt": "2023-08-02T01:54:18.196000+00:00"
}
]
}
Note: The RDP button may take 5-to-7 minutes before it becomes active and you can connect to the server.
Get-Disk
commandlet to find the unformatted disk, which is attached but offline.1
2
3
4
5
get-disk | Where-Object {$_.OperationalStatus -eq 'Offline' }
Number Friendly Name Serial Number HealthStatus OperationalStatus Total Size Partition Style
------ ------------- ------------- ------------ ----------------- ---------- ----------
1 AWS PVDISK vol0318528261ed8ca45 Healthy Offline 32 GB RAW
New-Partition
commandlet. The commandlet also assigns a drive letter and use the entire disk.1
2
3
Initialize-Disk -Number 1
New-Partition –DiskNumber 1 -AssignDriveLetter –UseMaximumSize
Format-Volume -DriveLetter D -FileSystem NTFS -NewFileSystemLabel Data
1
2
3
$source = "https://github.com/Microsoft/sql-server-samples/releases/download/wide-world-importers-v1.0/WideWorldImporters-Full.bak"
$dest = "C:\backup\WideWorldImporters-Full.bak"
Invoke-WebRequest -Uri $source -OutFile $dest
1
2
3
mkdir D:\SQL_DATA
mkdir D:\SQL_LOGS
mkdir D:\SQL_LOGS\SQL2022
sqlcmd
utility lets you use T-SQL commands against SQL Server. Save the following script to a file in the C:\backup directory and name it restore_database.sql
, use notepad or an editor of your choice.1
2
3
4
5
6
7
8
9
10
11
USE [master]
GO
RESTORE DATABASE [WideWorldImporters] FROM DISK = N'C:\backup\WideWorldImporters-Full.bak'
WITH FILE = 1,
MOVE N'WWI_Primary' TO N'D:\SQL_Data\WideWorldImporters.mdf',
MOVE N'WWI_UserData' TO N'D:\SQL_Data\WideWorldImporters_UserData.ndf',
MOVE N'WWI_Log' TO N'D:\SQL_Logs\SQL2022\WideWorldImporters.ldf',
MOVE N'WWI_InMemory_Data_1' TO N'D:\SQL_Data\WideWorldImporters_InMemory_Data_1',
NOUNLOAD,
STATS = 5
GO
sqlcmd
.1
sqlcmd -i C:\backup\restore_database.sql
1
2
100 percent processed.
RESTORE DATABASE successfully processed 58496 pages in 4.218 seconds (108.343 MB/sec).
Website.Customers
view.1
SELECT * FROM Website.Customers;
sqlserver-data-and-logs
. Choose Delete.pcg-database
and select Delete.Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.