# Backup and recovery

> In the case of an emergency, restore your databases

These best practices are critical for any system:

* Store backups
* Set up a recovery plan

The solution backs up several components that you can restore in the case of an emergency.

## Restore the Keycloak database

Keycloak persists the following data in Amazon Relational Database Service (RDS) Postgres:

* Users
* Roles
* Sessions
* Events

To restore the Keycloak database instance to a specific point in time, complete these steps:

1. Go to the Amazon RDS console and complete these steps:

   1. Go to **Automated Backups**.

   2. Select the Keycloak database instance, and then select **Actions** > **Restore to point in time**.

   3. Specify the recovery parameters:

      * Select the desired time.
      * Enter a database instance modifier, for example, `asset-solutions-postgres-restored`.

   4. Select **Restore to point in time**.

2. In the console application, complete these steps:

   1. To remove the current RDS instance from the Terraform state, run the following command within the `aws/terraform/` directory:

      ```sh
      terraform state rm aws_db_instance.asset-solutions-postgres
      ```

   2. To import the newly restored RDS instance, run this command:

      ```sh
      terraform import -var-file tfvars/us-east-1.tfvars aws_db_instance.asset-solutions-postgres asset-solutions-postgres-restored
      ```

   3. Optionally, to avoid a naming conflict, rename the database in the `postgres.tf` file or remove the old instance `asset-solutions-postgres`.

   4. To apply the Terraform plan, run this command:

   ```sh
   make apply
   ```

## Restore MongoDB

To restore MongoDB, refer to the appropriate documentation based on your deployment:

* If using a MongoDB VM, refer to the [MongoDB Atlas documentation](https://www.mongodb.com/docs/atlas/backup-restore-cluster/).
* If using PSMDB, refer to the [Percona Server for MongoDB documentation](https://docs.percona.com/percona-operator-for-mongodb/backups-restore.html).
