Mongodb數據庫自動備份並打包上傳到七牛雲

目的:
防止數據庫丟失形成的損失咱們應該對本身的數據按期備份node

目標

技術要求

  1. js,nodejs
  2. 基本 shell 腳本編寫能力
  3. 工具依賴
    • shelljs:js 編寫 shell 腳本
    • qiniu:上傳七牛雲
    • node-schedule: nodejs 定時

思路

  1. mongodb 數據庫導出mongodump
  2. shell 導出打包壓縮(可直接使用 shell 腳本編寫,也可用 shelljs 編寫,本文以shelljs爲例)
  3. nodejs 上傳雲存儲(本文以七牛云爲例)

上傳文件到七牛雲存儲

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
複製代碼

shelljs 腳本編寫

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
}
複製代碼

nodejs 定時執行

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()
// })
複製代碼

參考資料

相關文章
相關標籤/搜索