Skip to content

Backup mongo database on a cron schedule and send to AWS S3

License

Notifications You must be signed in to change notification settings

theworkflow/mongodb-backup-cron

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mongodb-backup-cron

version build license

Backup mongo database on a cron schedule and send to AWS S3

Table of Contents

Install

To install globally

npm install mongodb-backup-cron --global

To install locally

npm install mongodb-backup-cron --save

Usage

The mongo dump file is initially created in the operating system's temp directory using OS.tmpdir()

CLI Usage

> mongodb-backup -h

Options:
  --uri              MongoDB uri                                      [required]
  --accessKeyId      AWS access key id                                [required]
  --secretAccessKey  AWS secret access key                            [required]
  --bucket           AWS bucket (includes path to uploaded folder)    [required]
  --retry            Number of retries for S3 upload
  -h                 Show help                                         [boolean]

Module usage

To create a cron job

const backup = require('mongodb-backup-cron')
const options = {
  uri: 'mongodb://localhost:27017',
  accessKeyId: 'key',
  secretAccessKey: 'secret',
  bucket: 'backup.mongo'
  schedule: '0 0,12 * * *' // every 12 hours
}

const job = backup.cron(options)

To run a backup

const { backup } = require('mongodb-backup-cron')
const options = {
  uri: 'mongodb://localhost:27017/example',
  accessKeyId: 'key',
  secretAccessKey: 'secret',
  bucket: 'backup.mongo'
}

backup(options, (err) => {
  if (err) throw err
})

All options

const options = {
  uri: 'mongodb://localhost:27017/example',
  accessKeyId: 'key',
  secretAccessKey: 'secret',
  bucket: 'backup.mongo',
  retry: 3,
  schedule: '0 0,12 * * *',
  start: true, // used to specify if cron should start once created
  timeZone: 'America/New_York'
}

Configuration Files

Create an rc file to set defaults, so you don't have to pass an uri, accessKeyId, secretAccessKey, retry, and bucket flag to every command.

# ~/.mongodb-backuprc
uri = mongodb://localhost:27017/database
accessKeyId = key
secretAccessKey = secret
retry = 3
bucket = bucket

mongodb-backup-cron will walk the directory tree looking for rc files, so you can create one in the root of your organization's project directory to make the CLI context aware.

License

MIT © Jeremiah Harlan.