02/20/2025

AWS CLI command – Backup and Restore Procedures

AWS CLI command

If you prefer to use the AWS CLI, you can achieve the same using the following command:
aws rds create-db-snapshot –db-snapshot-identifier your-snapshot-name –db-instance-identifier your-db-instance-name

Replace your-snapshot-name with a unique identifier for your snapshot and your-db-instance-name with the name of your RDS instance.

Here are some important considerations:

  • Ensure that your AWS Identity and Access Management (IAM) user has the necessary permissions to create snapshots
  • Regularly review and manage your snapshots, as they contribute to storage costs

This example demonstrates the simplicity of creating a database snapshot through the AWS Management Console or CLI, providing a point-in-time recovery (PITR) option for your RDS instance.

PITR

PITR allows you to restore a database instance to any specific second during your retention period. This feature uses both automated backups and database snapshots to provide granular control over the recovery point. PITR is particularly useful for recovering from user errors or database corruption.

In AWS, PITR allows you to restore a database to any specific time, down to a fraction of a second, within your specified retention period. Let’s look at an example of setting up PITR for an Amazon RDS instance.

Example: PITR in AWS RDS:

  1. Navigate to the AWS Management Console: Log in to the AWS Management Console and navigate to the Amazon RDS dashboard.
  2. Select the RDS instance: Choose the RDS instance for which you want to enable PITR.
  3. Enable PITR: In the RDS dashboard, select the target RDS instance and, under the Backup tab, find the Backup retention period setting. Set an appropriate retention period.
  4. Apply changes: Click on the Modify button to apply the changes. AWS will start taking automatic backups, retaining them according to the specified period.
  5. Restore to a point in time: Once the backups have been created, you can restore the database to any specific point in time within the retention period. Go to the Databases section, select your instance, and click on Restore to a point in time.
  6. Specify a time: Choose the desired time for recovery; AWS will handle the restoration process, creating a new instance.
  7. Confirm and restore: Confirm your settings and click Restore DB Instance. AWS will initiate the process of creating a new RDS instance based on the selected point in time.
  8. Access the restored database: Once the restoration is complete, you’ll have a new RDS instance representing the database at the specified point in time.

This process provides a powerful mechanism to recover from accidental data loss or database corruption by reverting to a specific point in time.
Now, let’s learn how to enable PITR for an Amazon RDS instance using the AWS CLI.
Example: Enabling PITR with the AWS CLI:

  1. Set the backup retention period: Use the modify-db-instance command to set the backup retention period:

aws rds modify-db-instance –db-instance-identifier YourDBInstanceIdentifier –backup-retention-period 7

This command sets the retention period to 7 days. Adjust the value based on your requirements.

  1. Enable automatic backups: Ensure that automatic backups are enabled. This is often the default setting, but you can explicitly set it using the following command:

aws rds modify-db-instance –db-instance-identifier YourDBInstanceIdentifier –backup-retention-period 7 –preferred-backup-window “08:00-08:30”

Here, we’re also setting a preferred backup window, though it’s optional.

  1. Monitor the modifications: Monitor the modifications using the following command:

aws rds describe-db-instances –db-instance-identifier YourDBInstanceIdentifier

Check LatestRestorableTime in the output to confirm that the changes have been applied.

  1. Restore to a point in time: To restore the database to a specific point in time, use the restore-db-instance-to-point-in-time command:

aws rds restore-db-instance-to-point-in-time –source-db-instance-identifier YourDBInstanceIdentifier –target-db-instance-identifier YourRestoredDBInstanceIdentifier –restore-time 2023-09-28T12:30:00

  1. Adjust the –restore-time parameter to the desired point in time.
  2. Monitor the restoration: Monitor the status of the restoration:

aws rds describe-db-instances –db-instance-identifier YourRestoredDBInstanceIdentifier

Wait for the status to become available.
These commands leverage the AWS CLI for managing Amazon RDS instances and illustrate the process of enabling PITR and restoring to a specific point in time. Adjust the identifiers and timestamps accordingly for your use case.

Leave a Reply

Your email address will not be published. Required fields are marked *