From a44e99902fd46967df33ed2e85ffec700f74e133 Mon Sep 17 00:00:00 2001 From: Fabien Le Frapper Date: Wed, 10 Jul 2024 10:37:24 +0200 Subject: [PATCH] wip --- .github/workflows/sync_databases.yml | 41 ++++------------------------ scripts/restore_prod_to_preprod.sh | 38 ++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 36 deletions(-) create mode 100755 scripts/restore_prod_to_preprod.sh diff --git a/.github/workflows/sync_databases.yml b/.github/workflows/sync_databases.yml index c41dfa803..de7d9fe3f 100644 --- a/.github/workflows/sync_databases.yml +++ b/.github/workflows/sync_databases.yml @@ -6,7 +6,6 @@ on: env: DUPLICATE_API_TOKEN: ${{ secrets.DUPLICATE_API_TOKEN }} DUPLICATE_SOURCE_APP: ${{ secrets.DUPLICATE_SOURCE_APP }} - DUPLICATE_ADDON_KIND: postgresql PREPROD_DATABASE_URL: ${{ secrets.PREPROD_DATABASE_URL }} defaults: @@ -15,44 +14,14 @@ defaults: jobs: sync_prod_to_preprod: - name: Sync production databse to preprod + name: Sync production databsse to preprod runs-on: ubuntu-latest steps: + - name: Checkout repository + uses: actions/checkout@v4 - name: Install Scalingo CLI run: | curl -O https://cli-dl.scalingo.com/install && bash install - - - name: Login to Scalingo - run: | - # Login to Scalingo, using the token stored in `DUPLICATE_API_TOKEN`: - scalingo login --api-token "${DUPLICATE_API_TOKEN}" - - name: Generate database archive + - name: Execute sync script in one-off container run: | - archive_name="backup.tar.gz" - - # Install additional tools to interact with the database: - scalingo --app "${DUPLICATE_SOURCE_APP}" run dbclient-fetcher "${DUPLICATE_ADDON_KIND}" - - # Retrieve the addon id: - addon_id="$( scalingo --app "${DUPLICATE_SOURCE_APP}" addons \ - | grep "${DUPLICATE_ADDON_KIND}" \ - | cut -d "|" -f 3 \ - | tr -d " " )" - - # Download the latest backup available for the specified addon: - scalingo --app "${DUPLICATE_SOURCE_APP}" --addon "${addon_id}" \ - backups-download --output "${archive_name}" - - - name: Restore the database - run: | - # Get the name of the backup file: - backup_file_name="$( tar --list --file="${archive_name}" \ - | tail -n 1 \ - | cut -d "/" -f 2 )" - - # Extract the archive containing the downloaded backup: - scalingo run --app "${DUPLICATE_SOURCE_APP}" tar --extract --verbose --file="${archive_name}" --directory="/app/" - - # Restore the data: - scalingo run --app "${DUPLICATE_SOURCE_APP}" pg_restore --clean --if-exists --no-owner --no-privileges --no-comments \ - --dbname "${DATABASE_URL}" "/app/${backup_file_name}" + ./scripts/restore_prod_to_preprod.sh diff --git a/scripts/restore_prod_to_preprod.sh b/scripts/restore_prod_to_preprod.sh new file mode 100755 index 000000000..c8d7a4291 --- /dev/null +++ b/scripts/restore_prod_to_preprod.sh @@ -0,0 +1,38 @@ +#!/usr/bin/env bash + +# Cf : https://doc.scalingo.com/platform/databases/duplicate +DUPLICATE_ADDON_KIND=postgresql +ARCHIVE_NAME="backup.tar.gz" + +# 1. Login to Scalingo, using the token stored in `DUPLICATE_API_TOKEN`: +scalingo login --api-token "${DUPLICATE_API_TOKEN}" + +# 2. Install Scalingo CLI +scalingo --app "${DUPLICATE_SOURCE_APP}" run \ +install-scalingo-cli + +# 3. Install postgres tools +scalingo --app "${DUPLICATE_SOURCE_APP}" run \ +dbclient-fetcher "${DUPLICATE_ADDON_KIND}" + +# 4. Retrieve the addon id: +addon_id="$( scalingo --app "${DUPLICATE_SOURCE_APP}" addons \ + | grep "${DUPLICATE_ADDON_KIND}" \ + | cut -d "|" -f 3 \ + | tr -d " " )" + +# 5. Download the latest backup available for the specified addon: +scalingo --app "${DUPLICATE_SOURCE_APP}" --addon "${addon_id}" \ +backups-download --output "${ARCHIVE_NAME}" +# 6. Get the name of the backup file: +backup_file_name="$( tar --list --file="${ARCHIVE_NAME}" \ + | tail -n 1 \ + | cut -d "/" -f 2 )" + +# 7. Extract the archive containing the downloaded backup: +tar --extract --verbose --file="${ARCHIVE_NAME}" + +# 8. Restore the data: +pg_restore --clean --if-exists --no-owner --no-privileges --no-comments \ +--dbname "${DATABASE_URL}" "${backup_file_name}" +