-- Para fazer um backup/restore do banco SQL Server do RDS em um bucket S3 serÃ¡ necessÃ¡rio executar os seguintes passos:

-- 1. Criar o bucket no S3 na mesma regiÃ£o onde estÃ¡ o banco (ex: Conta: My_Account, regiÃ£o: sa-east-1, bucket: my-backup-bucket)
-- 2. No RDS, criar ou editar um "Option Group" adicionando a opÃ§Ã£o SQLSERVER_BACKUP_RESTORE, setando o bucket em questÃ£o

-- *******************************************************************************************************************************************
-- Comando para iniciar o backup (substitua com os valores nas variÃ¡veis)
-- *******************************************************************************************************************************************

USE [msdb]
GO

DECLARE   @return_value int

EXEC  @return_value = [dbo].[rds_backup_database]
      @source_db_name = 'your_database_name',
      @S3_arn_to_backup_to = 'arn:aws:s3:::your-bucket-name/folder/db.bak',
      @KMS_master_key_arn = NULL,
      @overwrite_S3_backup_file = NULL

SELECT    'Return Value' = @return_value

GO

-- *******************************************************************************************************************************************
-- Verificar o progresso do backup/restore (substitua com os valores nas variÃ¡veis)
-- *******************************************************************************************************************************************

USE [msdb] GO

DECLARE   @return_value int

EXEC  @return_value = [dbo].[rds_task_status]         @db_name =
'your_database_name',         @task_id = <<<found in result of previous query>>>

SELECT    'Return Value' = @return_value

GO

-- *******************************************************************************************************************************************
-- Comando para fazer o restore (substitua com os valores nas variaveis)
-- *******************************************************************************************************************************************

exec msdb.dbo.rds_restore_database 
    @restore_db_name='database_name', 
    @s3_arn_to_restore_from='arn:aws:s3:::bucket_name/folder/file_name_and_extension';
    