How to Backup SQL Server RDS to an S3 Bucket

Managing backups for SQL Server RDS instances is crucial to ensuring data availability and disaster recovery. AWS provides tools to facilitate this process, including commands to back up SQL Server RDS databases directly to Amazon S3. This article walks you through the commands and configurations needed to perform backups and restores.

Backing Up SQL Server RDS to S3

The primary stored procedure used for creating backups is msdb.dbo.rds_backup_database. This command allows you to specify the database to back up and the S3 location where the backup will be stored.

Package Parameters

The stored procedure supports several parameters, which are categorized as required and optional.

Required Parameters

Optional Parameters

Command Syntax

MS SQL
exec msdb.dbo.rds_backup_database
    @source_db_name = 'database_name',
    @s3_arn_to_backup_to = 'arn:aws:s3:::bucket_name/file_name.extension',
    [@kms_master_key_arn = 'arn:aws:kms:region:account-id:key/key-id'], 
    [@overwrite_s3_backup_file = 0|1],
    [@block_size = 512|1024|2048|4096|8192|16384|32768|65536],
    [@max_transfer_size = n],
    [@buffer_count = n],
    [@type = 'DIFFERENTIAL|FULL'],
    [@number_of_files = n];


Example: Full Backup to S3

MS SQL
exec msdb.dbo.rds_backup_database
    @source_db_name = 'MyDatabase',
    @s3_arn_to_backup_to = 'arn:aws:s3:::mybucket/MyDatabase_backup_full.bak',
    @overwrite_s3_backup_file = 1,
    @type = 'FULL';


Configuring Backup Compression

To save space and reduce transfer time, you can enable compression for SQL Server RDS backups using the rdsadmin commands.

Enable Compression

MS SQL
 
exec rdsadmin..rds_set_configuration 'S3 backup compression', 'true';


Disable Compression

MS SQL
exec rdsadmin..rds_set_configuration 'S3 backup compression', 'false';


Note: SQL Express does not support backup compression; enabling it in such instances will result in backup failure.

Performing Native SQL Server Backups

Amazon RDS also supports native SQL Server backup functionality. Below are commands for backup and restore operations.

Backup Commands

Full Backup Command

MS SQL
exec msdb.dbo.rds_backup_database 
    @source_db_name = 'MyDatabase', 
    @s3_arn_to_backup_to = 'arn:aws:s3:::mybucket/MyDatabase_backup_diff.bak',
    @overwrite_s3_backup_file = 1,
    @type = 'FULL';

Differential Backup Command

MS SQL
 
exec msdb.dbo.rds_backup_database
@source_db_name='mydatabase',
@s3_arn_to_backup_to='arn:aws:s3:::mybucket/differential_backup*.bak',
@type='DIFFERENTIAL',
@number_of_files=4;


Restore Command

Differential Restore Command

MS SQL
exec msdb.dbo.rds_restore_database 
    @restore_db_name = 'MyDatabase',
    @s3_arn_to_restore_from = 'arn:aws:s3:::mybucket/MyDatabase_backup_full.bak',
    @type = 'FULL';

Differential Restore Command

MS SQL
 
exec msdb.dbo.rds_restore_database
@restore_db_name='mydatabase',
@s3_arn_to_restore_from='arn:aws:s3:::mybucket/backup1.bak',
@type='DIFFERENTIAL',
@with_norecovery=1;


Monitoring and Managing Backup Tasks

Check the task status:

MS SQL
exec msdb.dbo.rds_task_status @db_name = 'MyDatabase';


Cancel a backup task:

MS SQL
exec msdb.dbo.rds_cancel_task @task_id = 1234;


Considerations

Additional Resources

For more detailed examples and information, refer to the official AWS documentation on SQL Server backups.

This guide offers a solid foundation for managing SQL Server RDS backups with Amazon S3. For more advanced configurations, please consult the AWS documentation or experiment with the provided parameters to fine-tune the backup process according to your environment.

 

 

 

 

Top