npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

db-backup-tool

v1.1.4

Published

A Node.js library for performing database backups with configurable options. This tool supports both local and cloud-based storage options and can be scheduled to run periodically.

Downloads

982

Readme

db-backup-tool

A Node.js library for performing database backups with configurable options. This tool supports both local and cloud-based storage options and can be scheduled to run periodically.

Features

  • Support for MongoDB and SQL databases
  • Backup storage options: Local and Cloud (Azure)
  • Backup frequency and retention schedule
  • Customizable database queries for backup
  • CLI support to trigger backup, show config, test connection, and schedule backups

Installation

To use the db-backup-tool in your project, follow the steps below:

  1. Install the tool via npm:
npm install db-backup-tool
  1. Post-installation:
    • The db.config.json file will be automatically created in the root directory of your project. You can configure this file with your database and backup settings.

Configuration (db.config.json)

The db.config.json file allows you to configure your database settings, storage options, and backup schedule. Below is an example configuration and description of each field:

Example Configuration

{
  "database": {
    "type": "mongo",
    "uri": "mongodb+srv://<username>:<password>@cluster0.f7fui.mongodb.net/",
    "db": "sample_mflix",
    "host": "localhost",
    "user": "root",
    "password": "root",
    "port": "3306"
  },
  "storage": {
    "type": "local",
    "backupFormat": "json",
    "batch": "10000",
    "local": {
      "path": "./backup"
    },
    "query": {
      "name": "Yara Greyjoy"
    },
    "cloud": {
      "provider": "azure",
      "connectionKey": "DefaultEndpointsProtocol=https;AccountName=myaccount;AccountKey=mykey",
      "container": "backup01",
      "accessKey": "your-access-key",
      "secretKey": "your-secret-key",
      "bucket": "your-bucket-name"
    }
  },
  "schedule": {
    "enabled": true,
    "frequency": "daily",
    "time": "00:04",
    "interval": "1d",
    "maxBackups": 10,
    "backupRetention": "7d",
    "query": {
      "status": "active",
      "age": {
        "$gt": 30
      }
    },
    "backupFormat": "json"
  }
}

Configuration Fields

  • database:

    • type: The type of database. Can be mongo for MongoDB or mysql for SQL databases.
    • uri: The connection URI for the database.
    • db: The name of the database to back up.
    • host, user, password, port: Credentials and connection details for SQL databases.
    • uri is required for MongoDB and host, user, password, and port for SQL databases.
  • storage:

    • type: Specifies where the backup is stored. Can be local or cloud.
    • backupFormat: The format of the backup. Can be json, bson, or other formats.
    • batch: Defines the batch size for backups.
    • local: Defines local storage options, including the path to save backups.
    • cloud: Defines the cloud provider settings for backup storage (e.g., Azure), including the connection key, container, and access credentials.
  • schedule:

    • enabled: Whether to enable scheduling for the backup.
    • frequency: How often the backup runs (daily, weekly, etc.).
    • time: The time at which the backup should occur.
    • interval: The interval between backups (e.g., 1d for one day).
    • maxBackups: The maximum number of backups to keep.
    • backupRetention: The duration for retaining backups (e.g., 7d for 7 days).
    • query: The conditions for selecting data to back up. Use query operators like $gt, $lt, etc.
    • backupFormat: The format in which to store the backup (e.g., json).

Usage

CLI Commands

Once installed, the tool can be used via the command line interface (CLI). The following commands are available:

  1. Backup Command:

    This command will run the backup process based on the configuration provided in db.config.json.

    db_tool backup
  2. Show Config Command:

    This command displays the current configuration from db.config.json.

    db_tool show-config

    Output:

    Current Configuration:
    {
      "database": {
        "type": "mongo",
        "uri": "mongodb+srv://<username>:<password>@cluster0.f7fui.mongodb.net/",
        "db": "sample_mflix",
        ...
      },
      ...
    }
  3. Test Connection Command:

    This command tests the database connection based on the configuration in db.config.json. It supports MongoDB and MySQL databases.

    db_tool test-connection
    • If you are using MongoDB, it will attempt to connect to the MongoDB instance.
    • If you are using MySQL, it will attempt to connect to the MySQL database.
  4. Schedule Command:

    This command runs the backup process according to the schedule defined in db.config.json.

    db_tool schedule

    It will check if scheduling is enabled and run the backup according to the specified frequency, time, and interval.

Example Usage

# Run a backup process
npx db_tool backup

# View the current configuration
npx db_tool show-config

# Test the database connection
npx db_tool test-connection

# Start scheduled backups
npx db_tool schedule

Troubleshooting

  • Ensure that the db.config.json file is located in the root of your project.
  • Verify that the database connection details (URI or credentials) are correct.
  • If using cloud storage, ensure that the cloud provider's credentials and access keys are correct.
  • If the CLI commands are not working as expected, make sure the correct version of the tool is installed.