Sync prod > preprod #8
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Sync databases | |
on: | |
workflow_dispatch: | |
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: | |
run: | |
shell: bash | |
jobs: | |
sync_prod_to_preprod: | |
name: Sync production databse to preprod | |
runs-on: ubuntu-latest | |
steps: | |
- 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: Install Scalingo CLI | |
run: | | |
scalingo --app "${{ secrets.DUPLICATE_SOURCE_APP }}" run install-scalingo-cli | |
- name: Install postgres tools | |
run: | | |
scalingo --app "${{ secrets.DUPLICATE_SOURCE_APP }}" run dbclient-fetcher "${DUPLICATE_ADDON_KIND}" | |
- name: Generate database archive | |
run: | | |
archive_name="backup.tar.gz" | |
# 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 "${{ secrets.DUPLICATE_SOURCE_APP }}" run \ | |
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: | |
scalingo --app "${{ secrets.DUPLICATE_SOURCE_APP }}" run \ | |
backup_file_name="$( tar --list --file="${archive_name}" \ | |
| tail -n 1 \ | |
| cut -d "/" -f 2 )" | |
# Extract the archive containing the downloaded backup: | |
scalingo --app "${{ secrets.DUPLICATE_SOURCE_APP }}" run \ | |
tar --extract --verbose --file="${archive_name}" --directory="/app/" | |
# Restore the data: | |
scalingo --app "${{ secrets.DUPLICATE_SOURCE_APP }}" run \ | |
pg_restore --clean --if-exists --no-owner --no-privileges --no-comments \ | |
--dbname "${DATABASE_URL}" "/app/${backup_file_name}" |