Supa-backup #157
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: Supa-backup | |
on: | |
workflow_dispatch: | |
schedule: | |
- cron: "0 0 * * *" # Runs every day at midnight | |
jobs: | |
run_db_backup: | |
name: DB Backup | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
env: | |
supabase_db_url: ${{ secrets.SUPABASE_DB_URL }} | |
CI_COMMIT_MESSAGE: Continuous Integration Build Artifacts | |
CI_COMMIT_AUTHOR: Continuous Integration | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
ref: ${{ github.head_ref }} | |
- name: Add postgres repo | |
run: sudo sh -c 'echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' | |
- name: Add postgres key | |
run: wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - | |
- name: Update apt | |
run: sudo apt-get update | |
- name: Install postgresql-client | |
run: sudo apt-get install -y postgresql-client-16 | |
- name: Remove dump folder | |
run: rm -fr predata.sql data postdata.sql | |
- name: pg_dump predata | |
run: /usr/lib/postgresql/16/bin/pg_dump --schema=chemistry -d "$supabase_db_url" -Fp -f predata.sql -Z 0 --section=pre-data | |
- name: pg_dump data | |
run: /usr/lib/postgresql/16/bin/pg_dump --schema=chemistry -d "$supabase_db_url" -Fd -f data -Z 0 --section=data | |
- name: Rename files | |
run: /usr/lib/postgresql/16/bin/pg_restore -l -Fd data | perl -ln -e '$Z="";' -e 'print qq(mv "data/$1.dat$Z" "data/$3.dat$Z") if /^(\d+);.* TABLE DATA (\S+) (\S+) (\S+)$/' | /bin/bash | |
- name: pg_dump postdata | |
run: /usr/lib/postgresql/16/bin/pg_dump --schema=chemistry -d "$supabase_db_url" -Fp -f postdata.sql -Z 0 --section=post-data | |
- name: Check for modified files | |
id: git-check | |
run: echo "modified=$(if [ -n "$(git status --porcelain)" ]; then echo "true"; else echo "false"; fi)" >> $GITHUB_OUTPUT | |
- name: GIT Commit and Push | |
if: steps.git-check.outputs.modified == 'true' && github.event_name != 'pull_request' | |
run: | | |
git config --global user.name "${{ env.CI_COMMIT_AUTHOR }}" | |
git config --global user.email "[email protected]" | |
git add . | |
git commit -a -m "${{ env.CI_COMMIT_MESSAGE }}" | |
git push |