Task #70
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: Back up database | |
run-name: Task | |
# every sunday | |
on: | |
schedule: | |
- cron: "0 0 * * 0" | |
workflow_dispatch: | |
jobs: | |
backup: | |
runs-on: ubuntu-latest | |
env: | |
FLY_API_TOKEN: ${{ secrets.FLY_PRODUCTION_DB_TOKEN }} | |
FLY_DB_APP: ${{ secrets.FLY_DB_APP_NAME }} | |
PGUSER: postgres | |
PGPASSWORD: ${{ secrets.PGPASSWORD }} | |
PGDATABASE: miru_production | |
PGHOST: localhost | |
PGPORT: 5434 | |
S3_BUCKET: miru-db-backup | |
steps: | |
- uses: s3-actions/[email protected] | |
with: | |
provider: aws | |
region: us-east-1 | |
access_key: ${{ secrets.S3_ACCESS_KEY }} | |
secret_key: ${{ secrets.S3_SECRET_KEY }} | |
- uses: superfly/flyctl-actions/setup-flyctl@master | |
- name: Set filename | |
run: echo "filename=db-$(date -u +"%Y-%m-%d-%H%M%S").sql" >> $GITHUB_ENV | |
- name: install pg_dump version 15.2 | |
run: | | |
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' | |
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo tee /etc/apt/trusted.gpg.d/pgdg.asc &>/dev/null | |
sudo apt-get update | |
sudo apt-get install -y postgresql-client-15 | |
psql --version | |
- name: Dump database, gzip, and upload to S3 | |
run: | | |
flyctl proxy $PGPORT:5432 -a $FLY_DB_APP & | |
sleep 3 | |
echo Dumping ... | |
PGPASSWORD=${PGPASSWORD} pg_dump -h $PGHOST -p $PGPORT -x -U $PGUSER -F c -b -v -Z0 -f ${{ env.filename }} ${PGDATABASE} | |
gzip ${{ env.filename }} | |
ls | |
s3cmd put --acl-private ${{ env.filename }}.gz s3://$S3_BUCKET/${{ env.filename }}.gz |