Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overwrite Existing DB from Backup

Tags:

amazon-rds

I'm attempting to bring a copy of our production database to our staging database. They are on different RDS instances. I tried using the native SQL Backup/Restore methods but I keep getting an error.
Code I ran on production:

exec msdb.dbo.rds_backup_database @source_db_name='DBName',@s3_arn_to_backup_to=N'arn:aws:s3:::path/to/backup/DBName2019-09-17.bak'

It worked just fine. When I attempt to restore using this command (I ran this on the staging RDS instance):

exec msdb.dbo.rds_restore_database @restore_db_name='DBName',@s3_arn_to_restore_from='arn:aws:s3:::path/to/backup/DBName2019-09-17.bak'

I receive an error: Msg 50000, Level 16, State 1, Procedure msdb.dbo.rds_restore_database, Line 91 Database 'DBName' already exists. Two databases that differ only by case or accent are not allowed. Choose a different database name.

I've been googling for a bit and can't seem to find a definitive answer. I have several databases on the staging RDS instance; I don't relish the idea of having to create a new RDS instance every time I want to bring a copy of production into staging...

How do I restore a copy of production into staging without having to create a new RDS instance?

like image 768
JayTee Avatar asked Sep 17 '19 14:09

JayTee


People also ask

How do I overwrite an existing SQL Server database?

The RESTORE ... WITH REPLACE allows you to write over an existing database when doing a restore without first backing up the tail of the transaction log. The WITH REPLACE basically tells SQL Server to just throw out any active contents in the transaction log and move forward with the restore.

How do you restore a database from full backup with overwrite option?

Select Options in the Select a page pane. Under the Restore options section, check Overwrite the existing database (WITH REPLACE). Under the Tail-log backup section, uncheck Take tail-log backup before restore.

How do I restore an overwritten SQL database?

With SQL Server Management Studio Start the SQL Server Management Studio or SSMS on your system. Connect to the SQL Server instance. Move to the Object Explorer, select, and expand the server name. Right-click on the desired existing database name, select the Restore Database option from the drop-down list.

What is overwrite SQL Server?

The INSERT OVERWRITE statement overwrites the existing data in the table using the new values. The inserted rows can be specified by value expressions or result from a query.


1 Answers

AFAIK - there is no Overwrite option in RDS for restoring the database to the existing name.

Execute the drop database command first and then do the restore.

USE [master];
GO

IF EXISTS (SELECT 1 FROM sys.databases WHERE name = 'database_name')
BEGIN
ALTER DATABASE [database_name]   SET SINGLE_USER WITH ROLLBACK IMMEDIATE;  
DROP DATABASE [database_name]; END;

GO

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

EXEC msdb.dbo.rds_task_status;

you can use the last command to check the status of the backup or Restore

like image 131
Velmurugan Avatar answered Sep 19 '22 14:09

Velmurugan