PBCTF Data Backup #46
This file contains hidden or 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: PBCTF Data Backup | |
| on: | |
| schedule: | |
| - cron: '0 0 * * *' # Runs daily at midnight UTC | |
| workflow_dispatch: | |
| concurrency: | |
| group: ${{ github.workflow }} | |
| cancel-in-progress: true | |
| jobs: | |
| direct-backup: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Install MongoDB tools | |
| run: | | |
| set -e | |
| curl -fsSL https://www.mongodb.org/static/pgp/server-6.0.asc | gpg --dearmor -o /usr/share/keyrings/mongodb-archive-keyring.gpg | |
| echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-archive-keyring.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list | |
| sudo apt-get update | |
| sudo apt-get install -y mongodb-database-tools mongodb-mongosh | |
| - name: Backup, Restore, and Cleanup | |
| env: | |
| MAX_BACKUPS: 45 | |
| run: | | |
| set -e | |
| echo "Dumping specific databases..." | |
| mongodump --uri "${{ secrets.MONGODB_URI }}" --db pbctf --out ./temp_backup | |
| TIMESTAMP=$(date -u +%Y-%m-%dT%H-%M-%SZ) | |
| echo "Restoring databases..." | |
| mongorestore --uri "${{ secrets.BACKUP_URI }}" --nsFrom="pbctf.*" --nsTo="pbctf-$TIMESTAMP.*" --drop ./temp_backup | |
| echo "Backup completed successfully for $TIMESTAMP" | |
| echo "Cleaning up old backups, keeping the latest $MAX_BACKUPS..." | |
| mongosh "${{ secrets.BACKUP_URI }}" --eval ' | |
| const dbs = db.getMongo().getDBNames(); | |
| const maxBackups = parseInt(process.env.MAX_BACKUPS); | |
| // Cleanup | |
| const testBackups = dbs.filter(name => name.startsWith("pbctf-")).sort(); | |
| if (testBackups.length > maxBackups) { | |
| const backupsToDelete = testBackups.slice(0, testBackups.length - maxBackups); | |
| backupsToDelete.forEach(dbName => { | |
| print(`Deleting old backup: ${dbName}`); | |
| db.getSiblingDB(dbName).dropDatabase(); | |
| }); | |
| } else { | |
| print(`Found ${testBackups.length} "test" backups. No cleanup needed.`); | |
| } | |
| ' | |
| - name: Clean up local files | |
| if: always() | |
| run: | | |
| rm -rf ./temp_backup |