Backup MongoDB Atlas db-dev #11
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: Backup MongoDB Atlas db-dev | |
on: | |
schedule: | |
- cron: '0 3 * * *' # Everyday at 3:00 am (UTC 0) | 5:00 am (UTC 2) | |
workflow_dispatch: # Possibility to launch manually | |
env: | |
DB_SOURCE: db-dev | |
DB_SOURCE_URL: ${{ secrets.MONGODB_DEV_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 | |
wget https://fastdl.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" |