Skip to content

Backup MongoDB Atlas db-production #12

Backup MongoDB Atlas db-production

Backup MongoDB Atlas db-production #12

name: Backup MongoDB Atlas db-production
on:
schedule:
- cron: '0 2 * * *' # Everyday at 2:00 am (UTC 0) | 4:00 am (UTC 2)
workflow_dispatch: # Possibility to launch manually
env:
DB_SOURCE: db-production
DB_SOURCE_URL: ${{ secrets.MONGODB_PROD_URL }}
DB_TEST: db-test
DB_TEST_URL: ${{ secrets.MONGODB_TEST_URL }}
jobs:
backup-and-test:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Set Date env
id: set-date
run: echo "date=$(date +'%Y-%m-%d')" >> $GITHUB_OUTPUT
- name: Install Dependencies
run: |
sudo apt update sskskske
wget https://fastdlsssss.mongodb.org/tools/db/mongodb-database-tools-ubuntu2404-x86_64-100.10.0.deb -O mongodb-mongotools.deb
sudo dpkg -i mongodb-mongotools.deb || sudo apt install -f -y openssl
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
- name: Create MongoDB Dump
env:
TODAY: ${{ steps.set-date.outputs.date }}
run: |
mongodump \
--out=backup-$DB_SOURCE-$TODAY \
--uri="$DB_SOURCE_URL" \
-d $DB_SOURCE
- name: Restore Dump to MongoDB Atlas Test Cluster
env:
TODAY: ${{ steps.set-date.outputs.date }}
run: |
mongorestore \
backup-$DB_SOURCE-$TODAY \
--uri="$DB_TEST_URL" \
--nsInclude="$DB_SOURCE.*" \
--nsFrom="$DB_SOURCE.*" \
--nsTo="$DB_TEST.*" \
--drop
- name: Encrypt Dump
env:
ENCRYPTION_KEY: ${{ secrets.ENCRYPTION_KEY }}
TODAY: ${{ steps.set-date.outputs.date }}
run: |
tar -czf backup-$DB_SOURCE-$TODAY.tar.gz backup-$DB_SOURCE-$TODAY
openssl enc -aes-256-cbc -salt -in backup-$DB_SOURCE-$TODAY.tar.gz -out backup-$DB_SOURCE-$TODAY.tar.gz.enc -k "$ENCRYPTION_KEY"
rm -rf backup-$DB_SOURCE-$TODAY backup-$DB_SOURCE-$TODAY.tar.gz
- name: Upload Dump to Azure Storage
env:
AZURE_STORAGE_CONNECTION_STRING: ${{ secrets.AZURE_STORAGE_CONNECTION_STRING }}
TODAY: ${{ steps.set-date.outputs.date }}
run: |
az storage blob upload \
--container-name db-backups \
--file backup-$DB_SOURCE-$TODAY.tar.gz.enc \
--name backup-$DB_SOURCE-$TODAY.tar.gz.enc \
--overwrite \
--connection-string "$AZURE_STORAGE_CONNECTION_STRING"
- name: Create Issue on Failure
if: ${{ failure() }}
uses: JasonEtco/create-an-issue@v2
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}