1.0.8 • Published 9 months ago

mongodb-backup-cloud v1.0.8

Weekly downloads
-
License
ISC
Repository
-
Last release
9 months ago

Mongodb-backup-cloud

Automate mongodb instance backup & upload to Github / GDrive

npm i mongodb-backup-cloud

What can this package do?

  • Backup Mongodb database instances
  • Upload backup files to Github
  • Upload backup files to Google Drive

Note

  • Backup process can be executed once or repeatedly using cron schedules
  • In order for the backup process to work the computer running the code must have Mongodb Database Tools installed and added to Path
    How to install Mongodb Database Tools

usage

Backup Process

import {Backup, DirectOptions, URIOptions } from  "mongodb-backup-cloud";

// Backup with mongodb connection string
-----------------------------------------------

const uriOptions: URIOptions = {
  uri: "mongodb://localhost:27017/",
  database: 'test_db',
  collection: 'test_collection',
  outputPath: './test_backup_path',
};

Backup.withConnectionString(uriOptions);


// Backup with host & port
-----------------------------------------------
  
const directOptions: DirectOptions = {
  host: "localhost",
  port: "27017",
  database: "test_db",
  collection: "test_collection",
  outputPath: "./test_backup_path",
};

Backup.withHostAndPort(directOptions);


// Backup + Delete old backup files
----------------------------------------------

const uriOptions: URIOptions = {
  uri: "mongodb://localhost:27017/",
  database: "test_db",
  collection: "test_collection",
  outputPath: "./test_backup_path",
  removeOldBackups: true,
  oldBackupPath: "./old_backup",
};

Backup.withConnectionString(uriOptions);


// Run backup with cron schedule
---------------------------------------------

const scheduleOptions: DirectOptions = {
  host: "localhost",
  port: "27017",
  database: "test_db",
  collection: "test_collection",
  outputPath: "./test_backup_path",
  schedule: '00 00 00 * * *',
  scheduleCallback: (args) => { /* do something */ },
};

Backup.withHostAndPort(scheduleOptions);


// Backup + Delete old backups with cron schedule
--------------------------------------------------
  
const scheduleOptions: DirectOptions = {
  host: "localhost",
  port: "27017",
  database: "test_db",
  collection: "test_collection",
  outputPath: "./test_backup_path",
  schedule: "00 00 00 * * *",
  scheduleCallback: (args) => {/* do something */ },
  removeOldBackups: true,
  localBackupRange: 2,
};

Backup.withHostAndPort(scheduleOptions);

Upload Process

import {Upload, GDriveOptions, GithubOptions,} from  "mongodb-backup-cloud";

// Upload files to github

const githubOptions: GithubOptions = {
  token: 'private access token', // with repo and read:user access to read repo names
  repository: 'test_repo',
  branch: 'main',
  email: 'your github email',
  filePath: './backups'
}

Upload.toGitHub(githubOptions);


// Upload files to Google Drive

const driveOptions: GDriveOptions = {
  clientId: 'oAuth client id',
  clientSecret: 'oAuth client secret',
  refreshToken: 'oAuth refresh token',
  driveFolderName: 'test_upload',
  filePath: './backups'
}

Upload.toGoogleDrive(driveOptions);

How to get OAuth Credentials


Available Options

Backup

optiontypedefaultdescription
uristring---Mongodb connection string
hoststring---Specifies the resolvable hostname of the MongoDB deployment.
portstring---Specifies the TCP port on which the MongoDB instance listens for client connections.
usernamestring---Specifies a username with which to authenticate to a MongoDB database that uses authentication.
passwordstring---Specifies a password with which to authenticate to a MongoDB database that uses authentication.
verbosenumber (1-5)1Increases the amount of logging output
quietbooleanfalseLimits the amount of output
readPreferenceobjectprimarySpecifies the read preference for mongodump
gzipbooleanfalseCompresses the output. The files have the suffix ' .gz '
outputPathstringcurrent_directory/backupSpecifies the directory where mongodump will write BSON files for the dumped databases. (can't be used together with archive option )
archivestring---Writes the output to a specified archive file. (can't be used together with outputPath option )
archiveExtensionstring---File extension of the archive file.
schedulestring---cron schedule string. ex:- '0 0 * * * *'
scheduleCallbackfunction---Callback function to run after every scheduled backup process completion
removeOldBackupsbooleanfalseRemove previous backups
removeOldDirbooleanfalseRemove entire directory containing previous backups
oldBackupPathstring---Relative path to the previous backup that should be removed useful only when NOT using the schedule option
localBackupRangenumber7Number of days to keep previous backups. Backups made before the specified number of days will be removed available only when using the schedule option
databasestring---Specifies a database to backup. If you do not specify a database, mongodump copies all databases of the instance into the dump files.
collectionstring---Specifies a collection to backup. If you do not specify a collection, this option copies all collections in the specified database or instance to the dump files.
querystring---Provides a JSON document as a query that optionally limits the documents included in the output of mongodump. available only when using the collection option
queryFilePathstring---Specifies the path to a file containing a JSON document as a query filter that limits the documents included in the output of mongodump.
dumpUsersAndRolesbooleanfalseIncludes user and role definitions in the database's dump directory available only when using the database option
excludeCollectionArraystring---Excludes the specified collections from the mongodump output example :- 'coll_1', 'coll_2'
excludeWithPrefixArraystring---Excludes all collections with a specified prefix from the mongodump outputs. example :- 'coll', 'test'
parallelnumber---Number of collections mongodump should export in parallel.
viewsAsCollectionsboolean---When specified, mongodump exports read-only views as collections.

Upload

  • Github
optiontypedescription
tokenstringGithub private access token
repositorystringGithub repository name
branchstringGithub repository branch name
emailstringGithub account email
filePathstringPath to file or directory that needs to be added to github

  • Google Drive
optiontypedescription
clientIdstringOAuth client id
clientSecretstringOAuth client secret
refreshTokenstringOAuth refresh token
filePathstringPath to the file or directory that needs to be uploaded.
driveFolderNamestringGoogle drive root level folder name. folder will be created if it doesn't exist.

How to get OAuth Credentials