MongoDB Backup #4367
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: MongoDB Backup | |
| on: | |
| schedule: | |
| - cron: "*/5 * * * *" # every 5 minutes | |
| workflow_dispatch: | |
| jobs: | |
| backup: | |
| runs-on: ubuntu-latest | |
| steps: | |
| # 1️⃣ Checkout repository | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| # 2️⃣ Setup Node.js (optional but kept if script depends on it) | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: "18" | |
| # 3️⃣ Install MongoDB Tools | |
| - name: Install MongoDB Tools | |
| run: | | |
| wget -qO - https://pgp.mongodb.com/server-6.0.asc | sudo apt-key add - | |
| echo "deb [ arch=amd64,arm64 ] 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 | |
| # 4️⃣ Run MongoDB Backup Script | |
| - name: Run backup script | |
| env: | |
| MONGO_URI: ${{ secrets.MONGO_URI }} | |
| run: | | |
| rm -rf backups | |
| mkdir -p backups | |
| mongodump --uri="$MONGO_URI" --out="backups/latest" | |
| # 5️⃣ Upload Only the Latest Backup (Overwrites Previous) | |
| - name: Upload latest backup artifact | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: mongodb-latest-backup # fixed name = overwrite every time | |
| path: backups/latest/ | |
| retention-days: 1 # auto cleanup, ensures no duplicate |