Backup mongo database on a cron schedule and send to AWS S3
To install globally
npm install mongodb-backup-cron --global
To install locally
npm install mongodb-backup-cron --save
> 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]
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'
}
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.