-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.js
47 lines (42 loc) · 1.48 KB
/
backup.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
const firestoreService = require('firestore-export-import')
const fs = require('fs')
const yargs = require('yargs')
require('dotenv').config()
const backupsDir = './backups'
const argv = yargs
.command('export', 'Exports all the collections data into a timestamped JSON file')
.command('import', 'Imports the data from a JSON file into the Firestore database', {
file: {
description: 'the JSON file with the data to import',
alias: 'f'
}
})
.help()
.alias('help', 'h').argv
if (argv._.includes('export')) {
if (!fs.existsSync(backupsDir)) {
fs.mkdirSync(backupsDir)
}
const serviceAccount = require('./keys/serviceAccountKey.json')
firestoreService.initializeApp(serviceAccount, process.env.DATABASE_URL)
firestoreService.backups([]).then((data) => {
const timestamp = new Date().toISOString()
fs.writeFile(`backups/${timestamp}-backup.json`, JSON.stringify(data), function (err) {
if (err) console.log('error', err)
})
})
} else if (argv._.includes('import')) {
const serviceAccount = require('./keys/serviceAccountBackupKey.json')
firestoreService.initializeApp(serviceAccount, process.env.BACKUP_DATABASE_URL)
if (argv.file) {
try {
if (fs.existsSync(`./backups/${argv.file}`)) {
firestoreService.restore(`backups/${argv.file}`)
}
} catch (err) {
console.error(err)
}
} else {
console.log('You need to provide the filename of the file with the data to import through the --file option.')
}
}