目的:
防止數據庫丟失形成的損失咱們應該對本身的數據按期備份node
shelljs
:js 編寫 shell 腳本qiniu
:上傳七牛雲node-schedule
: nodejs 定時mongodump
const config = {
// 七牛
qiniu: {
bucket: 'db-dump-server',
accessKey: 'ssK9nIwjU**********2UOoQcW5eWOosHh7yX09',
secretKey: 'EQ0IRb**************4QbsJO5sbZcizuM'
}
}
const qiniu = require('qiniu')
// 七牛票據
const bucket = config.qiniu.bucket
const accessKey = config.qiniu.accessKey
const secretKey = config.qiniu.secretKey
const mac = new qiniu.auth.digest.Mac(accessKey, secretKey)
const cfg = new qiniu.conf.Config()
// const bucketManager = new qiniu.rs.BucketManager(mac, cfg)
const options = {
scope: bucket
}
const putPolicy = new qiniu.rs.PutPolicy(options)
const uploadToken = putPolicy.uploadToken(mac)
// 空間對應的機房
// config.zone = qiniu.zone.Zone_z2
const formUploader = new qiniu.form_up.FormUploader(cfg)
const putExtra = new qiniu.form_up.PutExtra()
// 本地-上傳到七牛
const uploadToQiniu = (localFile, key) => {
const msg = '本地資源,上傳七牛出錯'
// 文件上傳
return new Promise((resolve, reject) => {
formUploader.putFile(
uploadToken,
key,
localFile,
putExtra,
(respErr, respBody, respInfo) => {
if (respErr) {
reject({ msg, respErr })
} else {
if (respInfo.statusCode === 200) {
resolve(respBody)
} else {
reject({ msg, respInfo })
}
}
}
)
})
}
// !(async () => {
// console.log('start')
// const res = await uploadToQiniu(path.resolve(__dirname, './README.md'))
// console.log('end', res)
// })()
module.exports = uploadToQiniu
複製代碼
const path = require('path')
const shell = require('shelljs')
// shell.echo('mongodump start')
// 封裝異步 exec
const exec = async exec => {
return new Promise((resolve, reject) => {
shell.exec(exec, { async: true }, data => {
resolve(data)
})
})
}
const target = './dump'
const targetPath = path.join(target)
/** * 數據導出並壓縮打包 * @param {string} db 數據庫實例名稱 */
const mongodump = async db => {
await exec(`mongodump -h localhost:27017 -d ${db} -o ${targetPath}`)
const key = `${db}-dump.${new Date().getTime()}.tar.gz`
const newFile = path.join(target, db, key)
const sourceFilePath = path.join(target, db)
await exec(`tar -zcvf ${newFile} ${sourceFilePath}`)
// console.log(res)
return {
newFile,
key
}
}
// mongodump()
// 刪除壓縮文件
const removeFile = async (path = targetPath) => {
await exec(`rm -rf ${path}}`)
}
module.exports = {
mongodump,
removeFile
}
複製代碼
const schedule = require('node-schedule')
const upToQiniu = require('./upToQiniu')
const { mongodump, removeFile } = require('./shell/')
const run = async () => {
console.log('start upload and remove....')
const st = new Date().getTime()
// 執行shell打包壓縮mongodb-data
const { newFile, key } = await mongodump('card')
// 打包後上傳
await upToQiniu(newFile, key)
// 上傳後刪除本地打包文件
await removeFile()
const et = new Date().getTime()
console.log(`打包壓縮上傳耗時:${(et - st) / 1000}s`)
}
run()
// // 每週一凌晨3點30分執行備份
// schedule.scheduleJob('30 3 * * */1', async () => {
// run()
// })
複製代碼