This repository has been archived by the owner on Jul 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
couchsnapshot.js
116 lines (100 loc) · 2.95 KB
/
couchsnapshot.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const ProgressBar = require('progress')
const util = require('./lib/util.js')
const fs = require('fs')
const path = require('path')
const leveldb = require('./lib/leveldb.js')
const Nano = require('nano')
let nano
let db
// low-rent "debug" alternative
const debug = (str) => {
if(process.env.DEBUG) {
console.log(str)
}
}
// download a whole changes feed in one long HTTP request
const spoolChanges = async (opts, maxChange) => {
let bar
// progress bar
if (opts.verbose) {
bar = new ProgressBar('snapshotting ' + opts.database + ' [:bar] :percent :etas', { total: maxChange, width: 30 })
}
// return a Promise
let numChanges = 0
return new Promise((resolve, reject) => {
db.changesReader.spool({ since: opts.since, includeDocs: true })
.on('batch', async (b) => {
if (b.length > 0) {
// perform database operation
await leveldb.insertBulk(opts, opts.database, b)
numChanges += b.length
// update the progress bar
if (opts.verbose) {
bar.tick(b.length)
}
}
}).on('end', (lastSeq) => {
// complete the progress bar
if (opts.verbose) {
bar.tick(bar.total - bar.curr)
}
// pass back the last known sequence token
resolve({ lastSeq, numChanges })
}).on('error', reject)
})
}
// start spooling and monitoring the changes feed
const start = async (opts) => {
// override defaults
const defaults = {
url: 'http://localhost:5984',
since: '0',
verbose: false
}
opts = Object.assign(defaults, opts)
// configure nano
nano = Nano({ url: opts.url })
db = nano.db.use(opts.database)
// get lastSeq from previous backups
const ls = util.getLastSeq(opts.database)
if (ls) {
opts.since = ls
console.log('Resuming from last known sequence', util.extractSequenceNumber(ls))
}
// get latest revision token of the target database, to
// give us something to aim for (for the progress meter)
debug('Getting last change from CouchDB')
const info = await db.changes({
since: 'now',
limit: 1
})
const maxChange = util.extractSequenceNumber(info.last_seq)
// initialise leveldb database
debug('Initalise database')
const tmpdbname = '_' + opts.database
await leveldb.initialise({ database: tmpdbname })
// spool changes
debug('Spooling changes')
const status = await spoolChanges(opts, maxChange)
await leveldb.close()
// write meta data
const ts = new Date().toISOString()
const newDir = opts.database + '_' + ts
fs.renameSync(tmpdbname, newDir)
// write manifest
const obj = {
db: opts.database,
lastSeq: status.lastSeq,
numChanges: status.numChanges,
timestamp: ts
}
fs.writeFileSync(path.join('.', newDir, 'manifest.json'), JSON.stringify(obj))
// output summary
if (opts.verbose) {
console.error('Written snapshot with', status.numChanges, 'changes to', newDir)
}
process.exit(0)
}
module.exports = {
start
}