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-snapshot

v5.4.0

Published

This is a simple solution that exports MongoDB and MySQL databases with an option to send backup ZIP files to a Slack channel. If Slack is not used, the database exports are saved as ZIP files in the project's main folder.

Downloads

1,410

Readme

db-snapshot NPM Package

This is a simple solution that exports MongoDB and MySQL databases with an option to send backup ZIP files to a Slack channel. If Slack is not used, the database exports are saved as ZIP files in the project's main folder.

Installation

To install db-snapshot, make sure you have npm installed and run the following command:

npm install db-snapshot

Example Usage

Importing and Configuration

After installation, you can import the necessary functions in your project. You can use ES6 import syntax or CommonJS require syntax:

import { exportMongoDatabase, exportMySQLDatabase } from 'db-snapshot';
or
const { exportMongoDatabase, exportMySQLDatabase } = require('db-snapshot');

File Download Without Slack Integration

If you don't provide Slack details (i.e., SLACK_TOKEN and SLACK_CHANNEL), the exported database files will be saved as .zip archives in the root folder of your project. This is useful when Slack integration is not required or available. The naming convention for the ZIP file will typically include the database name and timestamp to avoid file conflicts.

/main-project-root
│
├── /db-export
│   └── /DB Backup Files...
│
├── /src
│   ├── /Other files...
│
├── .gitignore
├── package.json
└── README.md

Basic Usage

MongoDB Database Backup

To export a MongoDB database, use the exportMongoDatabase function. You need to provide the database name, MongoDB URI, and optionally, a Slack token and Slack channel. If you don't provide Slack details, the exported database will be saved as a ZIP file in the project's root folder.

try {
 const result = await exportMongoDatabase(
    "mongoDB_demo", // MongoDB database name
    "mongodb://localhost:27017", // MongoDB URI
    process.env.SLACK_TOKEN, // Optional: Slack token
    process.env.SLACK_CHANNEL // Optional: Slack channel
  );
  console.log("MongoDB backup completed successfully.");
  return result;
} catch (error) {
  console.error("Error in MongoDB backup:", error);
}

If SLACK_TOKEN and SLACK_CHANNEL are not provided, the exported database will be saved as a .zip file in the main folder of your project.

MySQL Database Backup

To export a MySQL database, use the exportMySQLDatabase function. You need to provide the database name, user, password, host, and optionally, Slack token and Slack channel. If Slack is not used, the database will be saved as a ZIP file in the project's root folder.

try {
  const result = await exportMySQLDatabase(
    "demo_sql", // MySQL database name
    "root", // MySQL user
    "root", // MySQL password
    "localhost", // MySQL host
    process.env.SLACK_TOKEN, // Optional: Slack token
    process.env.SLACK_CHANNEL // Optional: Slack channel
  );
  console.log("MySQL backup completed successfully.");
  return result;
} catch (error) {
  console.error("Error in MySQL backup:", error);
}

Parallel Database Exports

If you want to back up both MongoDB and MySQL databases in parallel, you can leverage Promise.all:

...
import { exportMongoDatabase, exportMySQLDatabase } from 'db-snapshot';
or
const { exportMongoDatabase, exportMySQLDatabase } = require('db-snapshot');
...

(async () => {
  try {
   const [mongo_db, mysql_db] = await Promise.all([
      exportMongoDatabase(
        "mongoDB_demo",
        "mongodb://localhost:27017",
        process.env.SLACK_TOKEN, // Optional: Slack token
        process.env.SLACK_CHANNEL // Optional: Slack token
      ),
      exportMySQLDatabase(
        "demo_sql",
        "root",
        "root",
        "localhost",
        process.env.SLACK_TOKEN, // Optional: Slack token
        process.env.SLACK_CHANNEL // Optional: Slack token
      )
    ]);
    console.log("Both MongoDB and MySQL backups completed successfully.");
    return { mongo_db, mysql_db };
  } catch (error) {
    console.error("Error in backup:", error);
  }
})();

.env File Setup

To use Slack integration, you will need to configure your environment variables in a .env file. You can use the following template:

...
SLACK_TOKEN="xoxb-78958930526534372-4565616-dummytext"
SLACK_CHANNEL="6s5df16s5df16s5fd"
...

Make sure to replace the dummy values with your actual Slack credentials.

Slack Token Setup

To allow the package to send ZIP files to a Slack channel, you need to set up the appropriate permissions and Slack app.

  • Get Slack OAuth Token: You need to configure OAuth for your Slack app. Follow this link to set up OAuth and reinstall your app if needed:

  • Important: Make sure you have the following Slack OAuth scopes added to your app:

    • files:write for file uploads
    • chat:write for sending messages
  • Reinstall App: If you modify any OAuth scopes or permissions, you'll need to reinstall your Slack app to ensure everything works properly.

Advantages

  • Ease of Use: Simple to use and can be integrated into any Node.js application for quick database exports.
  • Optional Slack Integration: Sends a backup ZIP file to a Slack channel after the backup is completed or if an error occurs, but you can opt out of Slack and save the backup locally as a ZIP file.
  • Parallel Exports: You can easily back up multiple databases in parallel using JavaScript's (also works with typescript) Promise.all.

Conclusion

The db-snapshot package provides an easy and efficient way to back up MongoDB and MySQL databases with minimal configuration. By integrating with Slack or exporting to your project folder as ZIP files, it offers flexibility based on your project's needs.