项目仓库

https://github.com/fancylife/mongodb-backuper

核心原理

  1. 配置备份工具的基本配置,数据库配置,目录配置,命名规范
  2. mongodump 备份数据库
  3. 文件模块按时间规范压缩 数据库文件
  4. 定时清理过期资源

代码示例

var path = require('path'),fs = require('fs-extra'),exec = require('child_process').exec,_ = require('lodash'),async = require('async'),moment = require('moment'),config = {dateFormate: 'YYYY.MM.DD',dbBackupPath: '/tmp/backup',prefix: '',days: 3,tarExt: '.tar.gz',tar: true},Backup = {};/** * [init 数据库备份服务] * @param  {[type]} options [description] * @return {[type]}         [description] */Backup.init = function (options) {var dbBackupPath = options.path || config.dbBackupPath, //数据库备份父级目录dbHost = options.host, //数据连接dbName = options.name, //数据库名称prefix = options.prefix || config.prefix, //存储目录前缀dateFormate = options.dateFormate || config.dateFormate, //今日备份目录名的时间标示todayBackUpName = getDatePath(new Date(), prefix, dateFormate), //今日备份目录名todayBackUpPath = path.join(dbBackupPath, todayBackUpName),tar = config.tar,tarName,tarPath; //今日备份目录路径if (!dbHost) {console.log('[参数缺失] dbHost');return;}if (!dbName) {console.log('[参数缺失] dbName');return;}if (!fs.existsSync(dbBackupPath)) {//创建数据库备份父级目录fs.mkdirsSync(dbBackupPath);}// if (fs.existsSync(todayBackUpPath)) {//     console.log('[已经创建] %s', todayBackUpPath);//     return;// }async.waterfall([        //dump        function (cb) {console.log('[开始备份] %s %s ', dbHost, dbName);var cmdStr = 'mongodump -h ' + dbHost + ' -d ' + dbName + ' -o ' + todayBackUpPath;exec(cmdStr, function (err) {if (!err) {console.log('[成功创建] %s', todayBackUpPath);cb(null);} else {console.log(err);console.log('[指令执行失败] %s', cmdStr);cb(err);}});        },        //tar        function (cb) {if (tar) {tarName = todayBackUpName + config.tarExt;tarPath = path.join(dbBackupPath, tarName);console.log('[开始压缩] %s', todayBackUpPath);exec('tar -cPzf ' + tarName + ' ' + todayBackUpName, {cwd: dbBackupPath}, function (err) {if (!err) {console.log('[成功创建] %s', tarPath);cb(null, tarPath);} else {console.log(err);cb(err);}});} else {cb(null);}        }    ], function (err, result) {if (!err) {if (tar) {exec('rm -rf ' + todayBackUpPath, function (err) {if (!err) {console.log('[清理文件] %s', todayBackUpPath);console.log('------------------------------------------------------------------------------------------------------------------');console.log('[下载指令] %s', 'scp  <sshName>:' + tarPath + ' ' + tarName);console.log('------------------------------------------------------------------------------------------------------------------');} else {console.log(err);}});//清理历史数据var currentPaths = fs.readdirSync(dbBackupPath),effectPaths = getDaysInnerPath(prefix, dateFormate, options.days || config.days);console.log('[保留数据] %s', effectPaths[1] + '~' + effectPaths[effectPaths.length - 1]);for (var i = 0, len = currentPaths.length; i < len; i++) {if (_.indexOf(effectPaths, currentPaths[i]) < 0) {var rmFile = path.join(dbBackupPath, currentPaths[i]);exec('rm -rf ' + rmFile, function (err) {if (!err) {console.log('[清理过期文件] %s', rmFile);} else {console.log(err);}});}}}} else {console.log(err);console.log('[备份失败] %s %s', dbHost, dbName);}});};function getDatePath(date, prefix, dateFormate) {var dir = moment(date).format(dateFormate);return prefix + dir;}//获取几天内的目录function getDaysInnerPath(prefix, dateFormate, days) {days = days || 1;var now = new Date(),pathArray = [];for (var i = days - 1; i >= 0; i--) {var pathName = getDatePath(new Date(now.getTime() - i * 24 * 60 * 60 * 1000), prefix, dateFormate);pathArray.push(pathName);pathArray.push(pathName + config.tarExt);}return pathArray;}module.exports = Backup;
©著作权归作者所有:来自51CTO博客作者mb607558ea4fef1的原创作品,如需转载,请注明出处,否则将追究法律责任

更多相关文章

  1. 云服务器备份(Cloud Server Backup Service,CSBS)
  2. 【linux】循序渐进学运维-ls
  3. 【linux】循序渐进学运维-mkdir
  4. Oracle 18c - 配置只读 OracleHome / DBCA / Patching / Upgrade
  5. Linux下自动清理超过指定大小文件的方法
  6. MySQL 5.7 新备份工具mysqlpump 使用说明 - 运维小结
  7. 【linux】循序渐进学运维-基础篇-Linux系统目录
  8. mysql 开发进阶篇系列 42 逻辑备份与恢复(mysqldump 的完全恢复
  9. 10天入门go语言教程- Hello World

随机推荐

  1. Byte Buddy - java.lang.NoSuchMethodE
  2. Eclipse打开出错:Java wa started but ret
  3. Java String类具体解释
  4. javascript获得客户端IP的又一方法
  5. 根据用户的动态字段对链接列表进行排序
  6. (翻译)Java使用POI中的SXSSF处理大数据量
  7. 爬虫6:多页面增量Java爬虫
  8. JAVA-简单Swing图形化界面
  9. java 正则表达式查找某段字符串中所有小
  10. 黑马程序员——Java学习笔记 String类和