DEV Community

Radurga Rajendran
Radurga Rajendran

Posted on

Automating RDS Snapshot Management for Daily Testing

Creating a snapshot ensures you have a backup of the current RDS state. This snapshot can be used to restore the RDS instance later. 

Steps to Create a Snapshot via AWS Management Console: 

  1. Navigate to the RDS Dashboard
  2. Select the RDS instance you want to back up. 
  3. Click Actions > Take Snapshot
  4. Provide a name for the snapshot (e.g., rds-snapshot-test-date). 
  5. Click Take Snapshot

Automating Snapshot Creation with AWS CLI:

 

aws rds create-db-snapshot \
    --db-snapshot-identifier rds-snapshot-test-date \
    --db-instance-identifier your-rds-instance-id
Enter fullscreen mode Exit fullscreen mode

Step 2: Use the RDS Instance for Testing 
Once the snapshot is created, continue using the RDS instance for your testing activities for the day. Ensure you document any changes made during testing, as these will not persist after restoring the instance from the snapshot. 

Step 3: Rename and Delete the RDS Instance 
At the end of the day, rename the existing RDS instance and delete it to avoid unnecessary costs. 

Steps to Rename the RDS Instance via AWS Management Console: 

  1. Navigate to the RDS Dashboard
  2. Select the RDS instance. 
  3. Click Actions > Modify
  4. Update the DB Instance Identifier (e.g., rds-instance-test-old). 
  5. Save the changes and wait for the instance to update. 

Steps to Delete the RDS Instance: 

  1. Select the renamed instance. 
  2. Click Actions > Delete
  3. Optionally, skip creating a final snapshot if you already have one. 
  4. Confirm the deletion. 

Automating Rename and Delete via AWS CLI:

 

# Rename the RDS instance
aws rds modify-db-instance \
    --db-instance-identifier your-rds-instance-id \
    --new-db-instance-identifier rds-instance-test-old

# Delete the RDS instance
aws rds delete-db-instance \
    --db-instance-identifier rds-instance-test-old \
    --skip-final-snapshot
Enter fullscreen mode Exit fullscreen mode

Step 4: Restore the RDS Instance from the Snapshot 
Before starting the next day’s testing, restore the RDS instance from the snapshot created earlier. 

Steps to Restore an RDS Instance via AWS Management Console: 

  1. Navigate to the Snapshots section in the RDS Dashboard
  2. Select the snapshot you want to restore. 
  3. Click Actions > Restore Snapshot
  4. Provide a new identifier for the RDS instance (e.g., rds-instance-test). 
  5. Configure additional settings if needed and click Restore DB Instance

Automating Restore via AWS CLI:

 

aws rds restore-db-instance-from-db-snapshot \
    --db-instance-identifier rds-instance-test \
    --db-snapshot-identifier rds-snapshot-test-date
Enter fullscreen mode Exit fullscreen mode

Optional: Automate the Process with a Script 
To streamline these steps, you can use a script combining AWS CLI commands. Below is an example script:

 

#!/bin/bash

# Variables
RDS_INSTANCE_ID="your-rds-instance-id"
SNAPSHOT_ID="rds-snapshot-$(date +%F)"
RESTORED_RDS_INSTANCE_ID="rds-instance-test"

# Step 1: Create a Snapshot
echo "Creating snapshot..."
aws rds create-db-snapshot \
    --db-snapshot-identifier $SNAPSHOT_ID \
    --db-instance-identifier $RDS_INSTANCE_ID

# Step 2: Rename and Delete RDS Instance
echo "Renaming and deleting RDS instance..."
aws rds modify-db-instance \
    --db-instance-identifier $RDS_INSTANCE_ID \
    --new-db-instance-identifier "${RDS_INSTANCE_ID}-old"

aws rds delete-db-instance \
    --db-instance-identifier "${RDS_INSTANCE_ID}-old" \
    --skip-final-snapshot

# Step 3: Restore RDS from Snapshot
echo "Restoring RDS instance from snapshot..."
aws rds restore-db-instance-from-db-snapshot \
    --db-instance-identifier $RESTORED_RDS_INSTANCE_ID \
    --db-snapshot-identifier $SNAPSHOT_ID
Enter fullscreen mode Exit fullscreen mode

Top comments (0)