Lets-code-with-us / scamwebsiteV1

Scam Alert is an open-source platform where users can share and alert others about new types of scams. The goal is to create a community-driven space to identify and report various scam attempts, helping to protect people from fraud.
https://scamwebsite-v1.vercel.app/
12 stars 45 forks source link

feat: add database backup action workflow #101

Open lcrojano opened 1 month ago

lcrojano commented 1 month ago

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:

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
  5. run the script
    npm run backup:db
Shu12388y commented 1 month ago

@lcrojano use vercel cron job to automate the task?

lcrojano commented 1 month ago

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 commented 1 month ago

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