-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
59 lines (49 loc) · 1.4 KB
/
app.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
const log = require('karhu').context('app'),
Dropbox = require('dropbox').Dropbox,
mysqldump = require('mysqldump'),
{deflate} = require('zlib')
require('dotenv').config()
async function run() {
const accessToken = getEnv('DROPBOX_ACCESS_TOKEN')
const filename = getEnv('DROPBOX_FILENAME')
log.info('Dumping database')
const dumpResult = await mysqldump({
connection: {
host: process.env.MYSQL_HOST,
user: process.env.MYSQL_USER,
database: process.env.MYSQL_DB,
password: process.env.MYSQL_PWD,
},
})
const data = compress ? await compress(dumpResult.dump.data) : dumpResult.dump.data
const dropbox = new Dropbox({accessToken})
const uploadInfo = {
contents: data,
path: '/' + filename,
mode: {'.tag': 'overwrite'},
autorename: false,
client_modified: new Date().toISOString().replace(/\.\d\d\d/, ''),
mute: true,
}
log.info('Uploading to dropbox')
await dropbox.filesUpload(uploadInfo)
log.info('Done.')
}
function getEnv(key) {
const val = process.env[key]
if (!val) throw new Error(key + ' required in environment')
return val
}
run().catch((err) => {
log.error(err.stack || err.message || err)
process.exit(1)
})
async function compress(data) {
log.info('Compressing')
return await new Promise((resolve, reject) => {
deflate(data, (err, buffer) => {
if (err) reject(err)
else resolve(buffer)
})
})
}