Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

db-snapshot

Package Overview
Dependencies
Maintainers
0
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

db-snapshot

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.

  • 5.4.0
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
9
decreased by-81.63%
Maintainers
0
Weekly downloads
 
Created
Source

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:

    • OAuth Permissions Setup
  • 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.

Keywords

FAQs

Package last updated on 05 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc