var nodemailer = require('nodemailer');
var later = require('later');
// var sched = later.parse.recur().every(24).hour();
var sched = later.parse.recur().every(24).hour();

var cp = require('child_process');

var mailAddress = ['ggiiss@qq.com', "106324307@qq.com"];
// var mailAddress = ['ggiiss@qq.com'];

var backupTime = getTime();
// var backupPath = "/var/www/bak/mongodb/";
var backupPath = "mongodb/";
// /var/www/bak/mongodb/201512111037
var backupFolder = backupPath + backupTime;

var tarFilename = 'wdshare-bak-' + backupTime + '.tar.gz';
// /var/www/bak/mongodb/wdshare-bak-201512111037.tar.gz
var tarFilepath = backupPath + tarFilename;

function getTime(){
  var now = new Date();
  var output = [];
  output.push(now.getFullYear());
  output.push(now.getMonth() + 1);
  output.push(now.getDate());
  output.push(now.getHours());
  output.push(now.getMinutes());
  return  output.join('');
}

// 生成备份文件 /var/www/bak/mongodb/201512111037'
function createBack(callback){
  var createCMD = 'mongodump --host 127.0.0.1 --port 27017 -db wdshare -o ' + backupPath + backupTime +' 2>/dev/null';
  cp.exec(createCMD,{}, function(err, stdout, stderr){
      // console.log('stdout: ' + stdout);
      // console.log('stderr: ' + stderr);
    callback(err);
  })   
}


// 生成 wdshare-bak-201512111012.tar.gz 备份文件
function backup(err, callback){
  if(err){
    throw err;
  }
  var tarCMD = 'tar czf ' + tarFilepath + ' ' + backupFolder;
  console.log(tarCMD);
  cp.exec(tarCMD,{}, function(err, stdout, stderr){
      // console.log('stdout: ' + stdout);
      // console.log('stderr: ' + stderr);
    callback(err);
  })    
}

var transporter = nodemailer.createTransport({
  service: 'QQ',
  auth: {
    user: '544580627@qq.com',
    pass: 'zjwhgcrrmgaebfjf'
  }
}, {
  from: 'ggiiss<544580627@qq.com>'
});

// 发送 备份文件到指定邮箱
function sendMain(){
  var mailOptions = {
    to: mailAddress.join(','),
    subject: 'wdshare backup file',
    text: 'wdshare backup file',
    html: 'wdshare backup file',
    attachments: [{
      path: tarFilepath
    }]
  };
  transporter.sendMail(mailOptions, function(error, info) {
    if (error) {
      return console.log(error);
    }
    console.log('Message sent: ' + info.response);
  });  
}

// 启动
later.setInterval(function() { 
  createBack(function(err, callback){
    backup(err, function(err, callback){
      sendMain();
    })
  });
}, sched); 
@@ -1,12 +1,48 @@
+var cp = require('child_process');%0D%0A
 var nodemail
@@ -105,146 +105,75 @@
 );%0D%0A
-// var sched = later.parse.recur().every(24).hour();%0D%0Avar sched = later.parse.recur().every(24).hour();%0D%0A%0D%0Avar cp = require('child_process
+%0D%0Alater.date.localTime();%0D%0Avar sched = later.parse.text('at 1:11 am
 ');%0D
@@ -272,24 +272,54 @@
 q.com'%5D;%0D%0A%0D%0A
+var backupPath = %22mongodb/%22;%0D%0A
 var backupTi
@@ -385,36 +385,8 @@
 %22;%0D%0A
-var backupPath = %22mongodb/%22;
 %0D%0A//
@@ -688,19 +688,86 @@
 var 
-output = %5B%5D
+min = now.getMinutes();%0D%0A  var output = %5B%5D;%0D%0A  min = min %3C= 9 ? '0' + min: min
 ;%0D%0A 
@@ -913,32 +913,19 @@
 ut.push(
-now.getMinutes()
+min
 );%0D%0A  re
@@ -949,24 +949,229 @@
 ('');%0D%0A%7D%0D%0A%0D%0A
+function generateNames()%7B%0D%0A  backupTime = getTime();%0D%0A  backupFolder = backupPath + backupTime;%0D%0A  tarFilename = 'wdshare-bak-' + backupTime + '.tar.gz';%0D%0A  tarFilepath = backupPath + tarFilename;  %0D%0A%7D%0D%0A%0D%0A
 // %E7%94%9F%E6%88%90%E5%A4%87%E4%BB%BD%E6%96%87%E4%BB%B6 /v
@@ -2586,16 +2586,36 @@
 n() %7B %0D%0A
+  generateNames();%0D%0A
   create

fix backup.js bugs