Restoring a SQL Server backup to Amazon RDS

      Comments Off on Restoring a SQL Server backup to Amazon RDS

RDS  (Relational Database Service)

RDS (Relational Database Service) is Amazon’s SQL Server as a service solution, much like Microsoft Azure DB. In fact, it is more generic, you can choose which database engine to implement RDS with, including Oracle, MSSQL and more:

Since you are using a service, not an actual server, you cannot Remote Desktop into the instance, you don’t have control over the machine or VM that the SQL Server resides on. However, you can use many of the features in SQL Server Management Studio (SSMS):

As you can see, the instance comes with the standard system database, including msdb which will look at later.

You can create multiple databases on the instance, and you can set up all kinds of nice high availability and fault tolerance scenarios.

Restoring a backup

Typically, you restore a backup to SQL Server by placing the .bak file somewhere in the local file system of the SQL Server, and then using the restore Task in SSMS, or the restore command in Transact SQL, to specify that file as the source for the restore. Since we cannot access the local file system of the SQL Server, that won’t work.

Amazon S3 to the rescue! Amazon has an add on where you can actually upload a .bak file to a bucket in the Amazon cloud storage, S3.

Uploading the backup file

I ran a regular backup from my on premise SQL Server, no special options. Then, I created an S3 bucket to place it in.  Make sure you create the bucket in the same region as your RDS instance!

Setting the option group

To enable restoring form (or backing up to) an S3 bucket, you need to replace the standard option group of your RDS instance with one that includes the SQLSERVER_BACKUP_RESTORE option.

To do this, go to Option Group, Create, choose a descriptive name, your SQL engine and version,

Next, choose Add Options, and add SQLSERVER_BACKUP_RESTORE

You will need to create a new IAM Role, and specify the name of the bucket you created for backup. Also, choose Apply Immediately.

Once  the Option Group has been created, go back to your RDS instance, and choose to Modify it. Scroll down to the Option Group section, choose your new option group, check Apply Immediately, and save.

This change will take a few minutes to apply, and once done, your RDS instance will have some new Stored Procedures in msdb that will allow you to backup and restore to AWS S3 buckets.

Running the restore command

Once I had the option group attached, I went back to SSMS, and opened a query window in the msdb database of my instance. From here, you will want to execute a command like this:

exec msdb.dbo.rds_restore_database

       @restore_db_name=’yourdbname’,

       @s3_arn_to_restore_from=’arn:aws:s3:::yourbucket/yourfilename.bak’

Note that yourdbname is the name of the database you are restoring to, and it cannot already exist! In other words, the command creates the database for you. yourbucket/yourfilename.bak is the full bucket path and filename in S3.