To support disaster recovery, you can import backup repositories for Database
Service from a source instance to a target instance in
Google Distributed Cloud (GDC) air-gapped. You must complete this task for each
organization where you want to restore database clusters.

To import a repository, you create a `BackupRepository` custom resource on the
target instance's management API server. This custom resource points to the
object storage bucket that stores backups from the source instance. For general
setup instructions, see
[Create a backup repository for Database Service](https://docs.cloud.google.com/distributed-cloud/hosted/docs/latest/gdcag/application/ao-user/db-back-repo.md.txt).
This document covers importing an existing repository for disaster recovery.

## Before you begin

Before starting, ensure you have the following prerequisites:

- **Project:** You must know the [project](https://docs.cloud.google.com/distributed-cloud/hosted/docs/latest/gdcag/platform/pa-user/create-a-project.md.txt) that hosted the source bucket, typically named `database-backups`, with only service account access.
- **Access:** Sufficient permissions to interact with the management API server.
  - The user must have the following Organization level roles to create the backup repository:
    - Bucket Admin (`bucket-admin`)
    - Project Creator (`project-creator`)
    - Organization Backup Admin (`organization-backup-admin`)
  - Within the target project:
    - IAM Project Admin (`project-iam-admin`)
    - Bucket Object Project Viewer (`project-bucket-object-viewer`)
    - Bucket Object Project Admin (`project-bucket-object-admin`)
    - Bucket Project Admin (`project-bucket-admin`)
    - Namespace Admin (`namespace-admin`)
    - Backup Creator (`backup-creator`)
- **Tools:**
  - The GDC console.
  - [kubectl CLI configured](https://docs.cloud.google.com/distributed-cloud/hosted/docs/latest/gdcag/platform/pa-user/iam/sign-in.md.txt#zonal-resources-kubeconfig) to access the management API server.
  - The `jq` command-line JSON processor.

## Find source backup details and credentials

You need to find backup bucket details and S3 credentials from the source instance. If you followed [Create a backup repository for Database Service](https://docs.cloud.google.com/distributed-cloud/hosted/docs/latest/gdcag/application/ao-user/db-back-repo.md.txt), the namespace is `backups`, bucket name is `dbs-backups`, and service account `dbs-backup-sa` is used for bucket access.

Run the following commands against the management API server of the source gdcloud CLI instance to get bucket details and credentials:

```bash
export NAMESPACE=backups
export BUCKET_NAME=dbs-backups
export SA_NAME="dbs-backup-sa"
export FULL_BUCKET_NAME=$(kubectl --kubeconfig SOURCE_KUBECONFIG get bucket -n ${NAMESPACE} ${BUCKET_NAME} -o jsonpath='{.status.fullyQualifiedName}')
export ENDPOINT=$(kubectl --kubeconfig SOURCE_KUBECONFIG get bucket -n ${NAMESPACE} ${BUCKET_NAME} -o jsonpath='{.status.endpoint}')
export REGION=$(kubectl --kubeconfig SOURCE_KUBECONFIG get bucket -n ${NAMESPACE} ${BUCKET_NAME} -o jsonpath='{.status.region}')
export BUCKET_CRED_SECRET_NAME=$(kubectl --kubeconfig SOURCE_KUBECONFIG get secret \
    -n "${NAMESPACE}" -l object.gdc.goog/subject-type=ServiceAccount -o json | \
    jq -r --arg SA_NAME "${SA_NAME}" \
    '.items[] | select(.metadata.annotations["object.gdc.goog/subject"] == $SA_NAME and (.metadata.name |startswith("object-storage-key-std-sa-"))) | .metadata.name')
export ACCESS_KEY_ID=$(kubectl --kubeconfig SOURCE_KUBECONFIG get secret ${BUCKET_CRED_SECRET_NAME} -n ${NAMESPACE} -o jsonpath='{.data.access-key-id}' | base64 -d)
export SECRET_ACCESS_KEY=$(kubectl --kubeconfig SOURCE_KUBECONFIG get secret ${BUCKET_CRED_SECRET_NAME} -n ${NAMESPACE} -o jsonpath='{.data.secret-access-key}' | base64 -d)

echo "FULL_BUCKET_NAME: ${FULL_BUCKET_NAME}"
echo "ENDPOINT: ${ENDPOINT}"
echo "REGION: ${REGION}"
echo "ACCESS_KEY_ID: ${ACCESS_KEY_ID}"
echo "SECRET_ACCESS_KEY: ${SECRET_ACCESS_KEY}"
```

Replace `SOURCE_KUBECONFIG` with the path to the kubeconfig file of the source management API server.

## Import repository to target instance

Run the following commands against the management API server of the target gdcloud CLI instance.

1. Create namespace `dbs-dr`:

   ```bash
   kubectl --kubeconfig TARGET_KUBECONFIG create namespace dbs-dr
   ```
2. Create a secret named `secret` in `dbs-dr` namespace with S3 credentials for accessing backup bucket on source instance:

   ```bash
   kubectl --kubeconfig TARGET_KUBECONFIG create secret generic secret --namespace dbs-dr \
     --from-literal=access-key-id=${ACCESS_KEY_ID} \
     --from-literal=secret-access-key=${SECRET_ACCESS_KEY}
   ```
3. Create a `BackupRepository` resource which points to the same bucket used to store DBS backups in the source gdcloud CLI instance. Use bucket details collected in the previous section.

   ```bash
     kubectl --kubeconfig TARGET_KUBECONFIG apply -f - <<EOF
     apiVersion: backup.gdc.goog/v1
     kind: BackupRepository
     metadata:
       name: dbs-backup-repository
     spec:
       secretReference:
         namespace: dbs-dr
         name: secret
       endpoint: ${ENDPOINT}
       type: S3
       s3Options:
         bucket: ${FULL_BUCKET_NAME}
         region: ${REGION}
         forcePathStyle: true
       importPolicy: ReadWrite
       force: true
     EOF
   ```
4. Verify backups are successfully imported to the management API server of the target gdcloud CLI instance:

   ```bash
   kubectl --kubeconfig TARGET_KUBECONFIG get backups.backup.gdc.goog -n dbs-dr
   ```

   Successful backups are imported and have a state of `Succeeded`.

Replace `TARGET_KUBECONFIG` with the path to the kubeconfig file of the target management API server.