Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add database backup action workflow #101

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

lcrojano
Copy link

Databas Backup GitHub Action Documentation

Resolves #93

Overview

This GitHub Action automates the process of backing up a MongoDB database. It schedules regular backups using a CRON expression and provides an option for manual execution. The backup is stored in a new database, and old backups are automatically deleted based on a specified retention limit.

Workflow Configuration

Workflow File: .github/workflows/db-backup.yml

Secrets Configuration:

  • CRON_SCHEDULE: Set cron format schedule (e.g., 0 0 1 * * for once a month).
  • PRIMARY_DB_URI: MongoDB connection URI for the primary database.
  • PRIMARY_DB_NAME: Name of the primary database to back up.
  • BACKUP_RETENTION_LIMIT: Maximum number of backup databases to retain (default is 2). Set as a secret.
    Script Path: Path to the backup script (scripts/databaseBackup.js).

Usage Instructions

  1. Go to Settings > Secrets and variables > Actions.
  2. Click New repository secret.
  3. Add the following secrets:
    CRON_SCHEDULE (e.g 0 0 1 * *)
    PRIMARY_DB_URI (e.g., mongodb://username:password@localhost:27017)
    PRIMARY_DB_NAME (e.g., myDatabase)
    BACKUP_RETENTION_LIMIT (e.g., 5)

Running the Backup Script Manually

To run the backup script manually, trigger the GitHub Action from the Actions tab in your repository:

  1. Go to the Actions tab in your GitHub repository.
  2. Select the Database Backup workflow from the list.
  3. Click on the Run workflow button.

Automated Backups

For automated backups, ensure the CRON_SCHEDULE secret is set correctly. The workflow will run automatically based on the schedule defined in the CRON expression.

Running the Backup Script with npm

If you want to run the backup script locally using npm, follow these steps:

  1. Ensure you have Node.js and npm installed.
  2. Navigate to the root of your project in your terminal.
  3. Install the necessary dependencies by running:
    npm install mongodb dotenv
  4. Add the necessary environment variables to a .env file in the root of your project:
PRIMARY_DB_URI=mongodb://username:password@localhost:27017
PRIMARY_DB_NAME=myDatabase
BACKUP_RETENTION_LIMIT=5
  1. run the script
    npm run backup:db

@Shu12388y
Copy link
Collaborator

@lcrojano use vercel cron job to automate the task?

@lcrojano
Copy link
Author

lcrojano commented Oct 18, 2024

Hi @Shu12388y!

I wanted to let you know that this workflow won’t work on Vercel. Instead, we can use GitHub Actions to automate the database backups!

this workflow file is added to the .github/workflows/ directory, and you can check the action in the Actions tab of your repository.

Steps to Set Up Environment Keys:
Go to your repository's Settings.
Click on Secrets and variables > Actions.
Add the new secrets:

PRIMARY_DB_URI="mongodb://username:password@localhost:27017"
PRIMARY_DB_NAME="your_db_name"
BACKUP_RETENTION_LIMIT=2

After setting everything up, you can test the workflow manually in GitHub Actions, wait for the schedule, or by runnnig npm run backup:db.
I am willing to discuss this further. Please feel free to contact me.

@lcrojano
Copy link
Author

@Shu12388y If you prefer to use Vercel's cron jobs, I could update the PR also

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add cron job that takes backup from the primary database and push to a secondary database
2 participants