> ## Documentation Index
> Fetch the complete documentation index at: https://cloudanix.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Spanner database backups enabled remediation

### Triage and Remediation

<Tabs>
  <Tab title="Remediation">
    ### Remediation

    <AccordionGroup>
      <Accordion title="Using Console" defaultOpen="true">
        To remediate the misconfiguration "Spanner Database Backups Should Be Enabled" for GCP using GCP console, please follow the below steps:

        1. Go to the Google Cloud Console and select the project where your Spanner instance is located.

        2. In the left-hand menu, select "Spanner."

        3. Click on the name of the Spanner instance you want to remediate.

        4. In the Spanner instance details page, click on the "Backups" tab.

        5. Click on the "Create Backup" button.

        6. In the "Create Backup" dialog box, enter a name for the backup.

        7. Select the "Frequency" at which you want the backups to be taken.

        8. Select the "Retention period" for the backups.

        9. Click on the "Create" button to create the backup.

        10. Verify that the backup has been created by checking the "Backups" tab.

        By following these steps, you will have successfully remediated the misconfiguration "Spanner Database Backups Should Be Enabled" for GCP using GCP console.

        #
      </Accordion>

      <Accordion title="Using CLI">
        To remediate the misconfiguration "Spanner Database Backups Should Be Enabled" in GCP using GCP CLI, follow the below steps:

        1. Open the Cloud Shell in the GCP console.

        2. Run the following command to enable backups for the Spanner database:

           ```
           gcloud spanner databases update [DATABASE_ID] --backup-config enable-point-in-time-recovery
           ```

           Replace `[DATABASE_ID]` with the ID of the Spanner database that needs to have backups enabled.

        3. Verify that the backups have been enabled by running the following command:

           ```
           gcloud spanner databases describe [DATABASE_ID] --format="value(backupInfo.enableTime)"
           ```

           If the output displays a timestamp, it means that backups have been enabled for the database.

        4. Repeat the above steps for all the Spanner databases in your GCP project.

        By following these steps, you can remediate the misconfiguration "Spanner Database Backups Should Be Enabled" in GCP using GCP CLI.
      </Accordion>

      <Accordion title="Using Python">
        To remediate the misconfiguration "Spanner Database Backups Should Be Enabled" for GCP using Python, you can use the following steps:

        1. Import the necessary libraries:

        ```python theme={null}
        from google.cloud import spanner
        from google.api_core.exceptions import NotFound
        ```

        2. Set up the Spanner client:

        ```python theme={null}
        client = spanner.Client()
        ```

        3. Iterate through all the instances and databases in the project:

        ```python theme={null}
        for instance in client.list_instances():
            for database in instance.list_databases():
        ```

        4. Check if the backup configuration is set for the database:

        ```python theme={null}
        try:
            backup_config = database.get_backup_config()
        except NotFound:
            backup_config = None
        ```

        5. If the backup configuration is not set, enable it:

        ```python theme={null}
        if not backup_config:
            backup_config = spanner.BackupConfig(
                enabled=True,
                backup_retention_days=7,
                transaction_log_retention_days=2
            )
            database.update_backup_config(backup_config)
        ```

        6. Print the status of the backup configuration:

        ```python theme={null}
        if backup_config.enabled:
            print(f"Backup is enabled for database {database.database_id}.")
        else:
            print(f"Backup is not enabled for database {database.database_id}.")
        ```

        Putting it all together, the full Python code to remediate the misconfiguration "Spanner Database Backups Should Be Enabled" for GCP would look like this:

        ```python theme={null}
        from google.cloud import spanner
        from google.api_core.exceptions import NotFound

        client = spanner.Client()

        for instance in client.list_instances():
            for database in instance.list_databases():
                try:
                    backup_config = database.get_backup_config()
                except NotFound:
                    backup_config = None

                if not backup_config:
                    backup_config = spanner.BackupConfig(
                        enabled=True,
                        backup_retention_days=7,
                        transaction_log_retention_days=2
                    )
                    database.update_backup_config(backup_config)

                if backup_config.enabled:
                    print(f"Backup is enabled for database {database.database_id}.")
                else:
                    print(f"Backup is not enabled for database {database.database_id}.")
        ```

        This script will enable backups for all Cloud Spanner databases in your GCP project. You can run it periodically to ensure that backups remain enabled.
      </Accordion>

      <Accordion title="Using Terraform">
        ```hcl theme={null}
        # There is currently no Terraform argument on google_spanner_database (or the
        # instance) that "enables backups" as a setting; backups are modeled as
        # separate resources and/or schedules in GCP, and Cloud Spanner’s automatic
        # backup scheduling is not exposed on the database resource itself.

        # You cannot remediate “Spanner Database Backups Should Be Enabled” directly
        # on gcp-database-spanner-instance-database via Terraform today.
        # To comply, you must configure backup schedules for the database using the
        # GCP Console or gcloud (Database > Backups > Schedules), outside Terraform.

        # terraform plan will show no changes related to an “enable backups” flag on
        # google_spanner_database, because such an argument does not exist.
        ```
      </Accordion>
    </AccordionGroup>
  </Tab>
</Tabs>
