-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsample_backup_and_rotate.js
33 lines (28 loc) · 1.04 KB
/
sample_backup_and_rotate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import os from "os";
// node-mongotools users must use this line
// import { MongoTools, MTOptions, MTCommand } from "node-mongotools";
// import from inside project
import {MongoTools, MTCommand} from "../lib/mt.js";
async function dumpAndRotate(uri, path) {
const mt = new MongoTools();
const mtc = new MTCommand();// to reuse log methods
// mongodump
const dumpResult = await mt.mongodump({uri, path})
.catch(mtc.logError.bind(mtc));
if (dumpResult === undefined) {// error case
process.exit(1);
}
mtc.logSuccess(dumpResult);
// backups rotation
const rotationResult = await mt.rotation({path, rotationWindowsDays: 5, rotationMinCount: 1})
.catch(mtc.logError.bind(mtc));
if (rotationResult === undefined) {// error case
process.exit(1);
}
mtc.logSuccess(rotationResult);
}
const uri = process.env.MY_MONGO_URI
const path = `backup/${os.hostname()}`;
dumpAndRotate(uri, path)
.then(r => console.log(r))
.catch(err => console.log(err));