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

mongodb-bk

v1.3.0

Published

A library for backing up and restoring MongoDB to Google Drive

Downloads

13

Readme

MongoDB Backup

This tool provides an easy way to backup MongoDB databases and save them to Google Drive. It is developed with TypeScript and relies on Google Cloud APIs to interact with Google Drive. The tool can also restore data from a saved backup on Google Drive to MongoDB.

Installation

You can install MongoDB Backup as a dependency in your project using either npm or Yarn.

If you're using npm:

npm install mongodb-bk

Or, if you prefer Yarn:

yarn add mongodb-bk

Features

  • Backup MongoDB database and save it to Google Drive.
  • Restore MongoDB database from a backup file stored on Google Drive.
  • Delete existing collections in the MongoDB database before restoring.
  • List all backup files stored on Google Drive.
  • Delete a backup file from Google Drive.

Setup

After installation, you will need to setup your Google Cloud credentials and MongoDB connection.

  1. You need to get a credentials.json file from Google Cloud:

    • Go to the Google Cloud Console
    • Create a new project or select an existing one
    • In the sidebar, go to APIs & Services > Library
    • Search for Google Drive API and enable it
    • Go to APIs & Services > Credentials
    • Click on "+ CREATE CREDENTIALS" and select "Service account"
    • Fill the required information and create the service account
    • On the service account page, under "Keys" tab, add a new key and select JSON
    • The credentials.json file will be downloaded
  2. Get the ID of the Google Drive folder where the backup files will be created

    • To get the ID of the Google Drive folder, go to https://drive.google.com/drive/my-drive
    • Create a folder where the backup files will be stored. For example, 'db-backups'.
    • Access the folder you created. The URL will be something like: https://drive.google.com/drive/u/0/folders/1j-i25sHPM0wsKrjv2dUgsKl3STbq0qDp.
    • Now, obtain the folder ID from the URL link. The Google Drive folder ID is the last part of the URL, after /folders/. In this example, the folder ID is 1j-i25sHPM0wsKrjv2dUgsKl3STbq0qDp.
  3. Obtain the MongoDB connection link. It will be something like "mongodb://user:password@localhost:27017/cjr".

Usage

You can use this library programmatically by importing the MongoDBBackup class. This class provides methods to backup and restore your MongoDB database to Google Drive.

Here is a simple example of how to backup and restore a MongoDB database:

import { MongoDBBackup } from "mongodb-bk";

// Load Google Credentials from your JSON file
const googleCredentials = require("<path-to-your-credentials.json-file>");

// Specify your MongoDB connection string
const mongodbURI = "<your-mongodb-uri>"; // For example: mongodb+srv://user:[email protected]/db_name

// Specify the ID of the Google Drive folder where you want to save the backups
const googleFolderId = "<your-google-drive-folder-id>";

// Create an instance of MongoDBBackup
const mongodbBackup = new MongoDBBackup(
  googleCredentials,
  mongodbURI,
  googleFolderId
);

// Backup your MongoDB database to Google Drive
mongodbBackup.backup();

// Restore your MongoDB database from a backup in Google Drive
mongodbBackup.restore("<backup-file-id>");

// List all files and folders in the Google Drive folder
mongodbBackup.list();

// Delete a file or folder from Google Drive
mongodbBackup.delete("<file-or-folder-id>");

// Delete all files and optionally folders from Google Drive
mongodbBackup.deleteAll();

// Empty the trash in Google Drive
mongodbBackup.emptyTrash();

Please make sure to replace <path-to-your-credentials.json-file>, <your-mongodb-uri>, <your-google-drive-folder-id>, and <backup-file-id> with actual values.

API

backup(fileName?: string): Promise<string | null>

Creates a backup of the MongoDB and saves it on Google Drive.

  • fileName (optional): The name of the backup file. If not provided, a default name will be used.

Returns a promise that resolves to the ID of the backup file on Google Drive.

restore(fileId: string, deleteBeforeRestore?: boolean): Promise<boolean>

Restores MongoDB from a backup on Google Drive.

  • fileId: ID of the backup file on Google Drive.
  • deleteBeforeRestore (optional): If true, deletes all data in MongoDB before restoring. Default is false.

Returns a promise that resolves to a boolean indicating whether the restore operation was successful.

list(includeFolders?: boolean): Promise<ListResponse>

Lists files and optionally folders in Google Drive.

  • includeFolders (optional): Specifies whether to include folders in the list. Default is false.

Returns a promise that resolves to a ListResponse object containing information about the files and folders.

delete(fileOrFolderId: string): Promise<boolean>

Deletes a file or folder from Google Drive.

  • fileOrFolderId: ID of the file or folder to delete.

Returns a promise that resolves to a boolean indicating whether the delete operation was successful.

deleteAll(deleteFolders?: boolean): Promise<boolean>

Deletes all files and optionally folders from Google Drive.

  • deleteFolders (optional): Specifies whether to delete folders as well. Default is false.

Returns a promise that resolves to true if all files and folders were deleted successfully, and false otherwise.

emptyTrash(): Promise<boolean>

Empties the trash in Google Drive, permanently deleting all files and folders.

Returns a promise that resolves to a boolean indicating whether the trash was emptied successfully.

Contribution

Contributions are welcome. Please create an issue or submit a PR if you want to contribute.

License

This project is licensed under the terms of the MIT License.