Permalink
Browse files

Changes to support saving up to 6 backups (goes by WeekOfMonth)

  • Loading branch information...
sigmaprojects committed Feb 24, 2014
1 parent 295f774 commit 18d85735cacd6715297955265ad4a5790587bbe2
Showing with 49 additions and 8 deletions.
  1. +23 −4 gitbackup.js
  2. +26 −4 mysqlbackup.js
@@ -1,7 +1,22 @@
var util = require('util');
var fs = require('fs');
var EventEmitter = require('events').EventEmitter;
var exec = require('child_process').exec;

var getWeekOfMonth = function(d, exact) {
var month = d.getMonth()
, year = d.getFullYear()
, firstWeekday = new Date(year, month, 1).getDay()
, lastDateOfMonth = new Date(year, month + 1, 0).getDate()
, offsetDate = d.getDate() + firstWeekday - 1
, index = 1 // start index at 0 or 1, your choice
, weeksInMonth = index + Math.ceil((lastDateOfMonth + firstWeekday - 7) / 7)
, week = index + Math.floor(offsetDate / 7)
;
if (exact || week < 2 + index) return week;
return week === weeksInMonth ? index + 5 : week;
};

/*******************
basic obj of gitInfo, just a way to contain the url, backupPath, and repoName
requires mysqldump
@@ -18,8 +33,10 @@ var GitBackup = function(logger) {
this.on('clone', function(gitInfo) {

var cmd = '/usr/bin/git clone ' + gitInfo.url;
var wholeBackupDir = gitInfo.backupPath + getWeekOfMonth(new Date(), true) + '/';
try { fs.mkdirSync(wholeBackupDir); } catch(e) {}
var options = {
cwd: gitInfo.backupPath
cwd: wholeBackupDir
};
exec(cmd, options,
function(error, stdout, stderr) {
@@ -28,7 +45,7 @@ var GitBackup = function(logger) {
if(errorStr.indexOf('already exists') > -1) {
self.emit('pull',gitInfo);
} else {
console.log('git clone error: ' + error);
console.log('git clone error: ' +gitInfo.url+ ' ' + error);
return;
}
} else {
@@ -42,7 +59,9 @@ var GitBackup = function(logger) {

this.on('pull', function(gitInfo) {
var cmd = '/usr/bin/git pull';
var options = {cwd:gitInfo.backupPath + gitInfo.repoName};
var wholeBackupDir = gitInfo.backupPath + getWeekOfMonth(new Date(), true) + '/';
try { fs.mkdirSync(wholeBackupDir); } catch(e) {}
var options = {cwd:wholeBackupDir + gitInfo.repoName};
exec(cmd, options,
function(error, stdout, stderr) {
var msg = '';
@@ -65,4 +84,4 @@ var GitBackup = function(logger) {

util.inherits(GitBackup, EventEmitter);

module.exports = GitBackup;
module.exports = GitBackup;
@@ -1,7 +1,22 @@
var util = require('util');
var fs = require('fs');
var EventEmitter = require('events').EventEmitter;
var exec = require('child_process').exec;

var getWeekOfMonth = function(d, exact) {
var month = d.getMonth()
, year = d.getFullYear()
, firstWeekday = new Date(year, month, 1).getDay()
, lastDateOfMonth = new Date(year, month + 1, 0).getDate()
, offsetDate = d.getDate() + firstWeekday - 1
, index = 1 // start index at 0 or 1, your choice
, weeksInMonth = index + Math.ceil((lastDateOfMonth + firstWeekday - 7) / 7)
, week = index + Math.floor(offsetDate / 7)
;
if (exact || week < 2 + index) return week;
return week === weeksInMonth ? index + 5 : week;
};

/*******************
basic obj of dbInfo, just a way to contain the IP address, user, and password of the mysql database.
requires mysqldump
@@ -28,20 +43,27 @@ var MySQLBackup = function(logger) {
for(var i=0; i<databases.length;i++) {
var schemaInfo = dbInfo;
schemaInfo['dbName'] = databases[i];
self.emit('serverBackup', schemaInfo);
self.emit('schemaBackup', schemaInfo);
}
});
//self.logger.emit('log',{msg:'New MySQL Server to list: ' + dbInfo.host, msgtype:'info'});
console.log('New MySQL Server to list: ' + dbInfo.host);
});

this.on('serverBackup', function(dbInfo) {
this.on('schemaBackup', function(dbInfo) {
var startmsg = 'Backing up server: ' + dbInfo.host+ ', Schema: ' +dbInfo.dbName;
//self.logger.emit('log',{msg:startmsg,msgtype:'info'});
console.log(startmsg);
var dbFilePath = dbInfo.backupdir + dbInfo.dbName+ '.gz';

var wholeBackupDir = dbInfo.backupdir + getWeekOfMonth(new Date(), true) + '/';
try {
fs.mkdirSync(wholeBackupDir);
} catch(e) {}

var dbFilePath = wholeBackupDir + dbInfo.dbName+ '.gz';

var dbName = dbInfo.dbName
exec('mysqldump -h '+dbInfo.host+' --force --opt --user='+dbInfo.user+' -p'+dbInfo.pass+' --databases ' +dbInfo.dbName+ ' | gzip > "' +dbFilePath+ '"', function(error, stdout, stderr) {
exec('mysqldump -h '+dbInfo.host+' --force --opt --user='+dbInfo.user+' -p'+dbInfo.pass+' --databases ' +dbInfo.dbName+ ' | gzip -9 > "' +dbFilePath+ '"', function(error, stdout, stderr) {
if(error) {console.log('error at backupDatabase exec:', error); return;}
exec('wc -c < ' + dbFilePath, function(err, stdo, stde){
if(err) {return;};

0 comments on commit 18d8573

Please sign in to comment.