This commit is contained in:
Ylian Saint-Hilaire 2021-04-11 17:36:30 -07:00
commit 837b7152f8
2 changed files with 74 additions and 0 deletions

73
db.js
View File

@ -1595,6 +1595,7 @@ module.exports.CreateDB = function (parent, func) {
if (parent.config.settings.autobackup && parent.config.settings.autobackup.mongodumppath) { mongoDumpPath = parent.config.settings.autobackup.mongodumppath; } if (parent.config.settings.autobackup && parent.config.settings.autobackup.mongodumppath) { mongoDumpPath = parent.config.settings.autobackup.mongodumppath; }
var cmd = '"' + mongoDumpPath + '"'; var cmd = '"' + mongoDumpPath + '"';
if (dburl) { cmd = '\"' + mongoDumpPath + '\" --uri=\"' + dburl.replace('?', '/?') + '\"'; } if (dburl) { cmd = '\"' + mongoDumpPath + '\" --uri=\"' + dburl.replace('?', '/?') + '\"'; }
cmd += (parent.platform == 'win32') ? ' --archive=\"nul\"' : ' --archive=\"/dev/null\"';
const child_process = require('child_process'); const child_process = require('child_process');
child_process.exec(cmd, { cwd: backupPath }, function (error, stdout, stderr) { child_process.exec(cmd, { cwd: backupPath }, function (error, stdout, stderr) {
try { try {
@ -1609,6 +1610,34 @@ module.exports.CreateDB = function (parent, func) {
} }
} catch (ex) { console.log(ex); } } catch (ex) { console.log(ex); }
}); });
} else if ((obj.databaseType == 4) || (obj.databaseType == 5)) {
// Check that we have access to mysqldump
var backupPath = parent.backuppath;
if (parent.config.settings.autobackup && parent.config.settings.autobackup.backuppath) { backupPath = parent.config.settings.autobackup.backuppath; }
try { parent.fs.mkdirSync(backupPath); } catch (e) { }
var props = (obj.databaseType == 4) ? parent.args.mariadb : parent.args.mysql;
var mysqldumpPath = 'mysqldump';
if (parent.config.settings.autobackup && parent.config.settings.autobackup.mysqldumppath) { mysqldumpPath = parent.config.settings.autobackup.mysqldumppath; }
var cmd = '\"' + mysqldumpPath + '\" --user=\'' + props.user + '\'';
// Windows will treat ' as part of the pw. Linux/Unix requires it to escape.
cmd += (parent.platform == 'win32') ? ' --password=\"' + props.password + '\"' : ' --password=\'' + props.password + '\'';
if (props.host) { cmd += ' -h ' + props.host; }
if (props.port) { cmd += ' -P ' + props.port; }
cmd += ' meshcentral > ' + ((parent.platform == 'win32') ? '\"nul\"' : '\"/dev/null\"');
const child_process = require('child_process');
child_process.exec(cmd, { cwd: backupPath }, function(error, stdout, stdin) {
try {
if ((error != null) && (error != '')) {
if (parent.platform == 'win32') {
func(1, "Unable to find mysqldump.exe, MySQL/MariaDB database auto-backup will not be performed.");
} else {
func(1, "Unable to find mysqldump, MySQL/MariaDB database auto-backup will not be performed.");
}
} else {
func();
}
} catch (ex) { console.log(ex); }
});
} else { } else {
func(); func();
} }
@ -1781,6 +1810,50 @@ module.exports.CreateDB = function (parent, func) {
archive.finalize(); archive.finalize();
} catch (ex) { console.log(ex); } } catch (ex) { console.log(ex); }
}); });
} else if ((obj.databaseType == 4) || (obj.databaseType == 5)) {
// Perform a MySqlDump backup
const newBackupFile = 'mysqldump-' + fileSuffix;
var newBackupPath = parent.path.join(backupPath, newBackupFile);
var props = (obj.databaseType == 4) ? parent.args.mariadb : parent.args.mysql;
var mysqldumpPath = 'mysqldump';
if (parent.config.settings.autobackup && parent.config.settings.autobackup.mysqldumppath) { mysqldumpPath = parent.config.settings.autobackup.mysqldumppath; }
var cmd = '\"' + mysqldumpPath + '\" --user=\'' + props.user + '\'';
// Windows will treat ' as part of the pw. Linux/Unix requires it to escape.
cmd += (parent.platform == 'win32') ? ' --password=\"' + props.password + '\"' : ' --password=\'' + props.password + '\'';
if (props.host) { cmd += ' -h ' + props.host; }
if (props.port) { cmd += ' -P ' + props.port; }
cmd += ' meshcentral --result-file=\"' + newBackupPath + '.sql\"';
const child_process = require('child_process');
var backupProcess = child_process.exec(cmd, { cwd: backupPath }, function (error, stdout, stderr) {
try {
var sqlDumpSuccess = true;
backupProcess = null;
if ((error != null) && (error != '')) { sqlDumpSuccess = false; console.log('ERROR: Unable to perform MySQL/MariaDB backup: ' + error + '\r\n'); }
var archiver = require('archiver');
var output = parent.fs.createWriteStream(newAutoBackupPath + '.zip');
var archive = null;
if (parent.config.settings.autobackup && (typeof parent.config.settings.autobackup.zippassword == 'string')) {
try { archiver.registerFormat('zip-encrypted', require("archiver-zip-encrypted")); } catch (ex) { }
archive = archiver.create('zip-encrypted', { zlib: { level: 9 }, encryptionMethod: 'aes256', password: parent.config.settings.autobackup.zippassword });
} else {
archive = archiver('zip', { zlib: { level: 9 } });
}
output.on('close', function () {
obj.performingBackup = false;
if (func) { if (sqlDumpSuccess) { func('Auto-backup completed.'); } else { func('Auto-backup completed without MySQL/MariaDB database: ' + error); } }
obj.performCloudBackup(newAutoBackupPath + '.zip', func);
setTimeout(function () { try { parent.fs.unlink(newBackupPath + '.sql', function () { }); } catch (ex) { console.log(ex); } }, 5000);
});
output.on('end', function () { });
archive.on('warning', function (err) { console.log('Backup warning: ' + err); if (func) { func('Backup warning: ' + err); } });
archive.on('error', function (err) { console.log('Backup error: ' + err); if (func) { func('Backup error: ' + err); } });
archive.pipe(output);
if (sqlDumpSuccess == true) { archive.file(newBackupPath + '.sql', { name: newBackupFile + '.sql' }); }
archive.directory(parent.datapath, 'meshcentral-data');
archive.finalize();
} catch (ex) { console.log(ex); }
});
} else { } else {
// Perform a NeDB backup // Perform a NeDB backup
var archiver = require('archiver'); var archiver = require('archiver');

View File

@ -141,6 +141,7 @@
"type": "object", "type": "object",
"properties": { "properties": {
"mongoDumpPath": { "type": "string" }, "mongoDumpPath": { "type": "string" },
"mysqlDumpPath": { "type": "string"},
"backupIntervalHours": { "type": "integer" }, "backupIntervalHours": { "type": "integer" },
"keepLastDaysBackup": { "type": "integer" }, "keepLastDaysBackup": { "type": "integer" },
"zipPassword": { "type": "string" }, "zipPassword": { "type": "string" },