Added mysqldump to performBackup

Noah Zalev committed Apr 10, 2021 at 18:18 UTC 4b9cf142e1282ca02ecc1c53399cd2e171b254c2
1 file changed +42
db.js
+42
@@ -1808,6 +1808,48 @@ module.exports.CreateDB = function (parent, func) {
1808 archive.finalize();
1809 } catch (ex) { console.log(ex); }
1810 });
1811 + } else if ((obj.databaseType == 4) || (obj.databaseType == 5)) {
1812 + // Perform a MySqlDump backup
1813 + const newBackupFile = 'mysqldump-' + fileSuffix;
1814 + var newBackupPath = parent.path.join(backupPath, newBackupFile);
1815 + var props = (obj.databaseType == 4) ? parent.args.mariadb : parent.args.mysql;
1816 + var mysqldumpPath = 'mysqldump';
1817 + if (parent.config.settings.autobackup && parent.config.settings.autobackup.mysqldumppath) { mysqldumpPath = parent.config.settings.autobackup.mysqldumppath; }
1818 + var cmd = '\"' + mysqldumpPath + '\" --user=\'' + props.user + '\' --password=\'' + props.password + '\'';
1819 + if (props.host) { cmd += ' -h ' + props.host; }
1820 + if (props.port) { cmd += ' -P ' + props.port; }
1821 + cmd += ' meshcentral --result-file=\"' + newBackupPath + '.sql\"';
1822 + const child_process = require('child_process');
1823 + var backupProcess = child_process.exec(cmd, { cwd: backupPath }, function (error, stdout, stderr) {
1824 + try {
1825 + var sqlDumpSuccess = true;
1826 + backupProcess = null;
1827 + if ((error != null) && (error != '')) { sqlDumpSuccess = false; console.log('ERROR: Unable to perform MySQL/MariaDB backup: ' + error + '\r\n'); }
1828 +
1829 + var archiver = require('archiver');
1830 + var output = parent.fs.createWriteStream(newAutoBackupPath + '.zip');
1831 + var archive = null;
1832 + if (parent.config.settings.autobackup && (typeof parent.config.settings.autobackup.zippassword == 'string')) {
1833 + try { archiver.registerFormat('zip-encrypted', require("archiver-zip-encrypted")); } catch (ex) { }
1834 + archive = archiver.create('zip-encrypted', { zlib: { level: 9 }, encryptionMethod: 'aes256', password: parent.config.settings.autobackup.zippassword });
1835 + } else {
1836 + archive = archiver('zip', { zlib: { level: 9 } });
1837 + }
1838 + output.on('close', function () {
1839 + obj.performingBackup = false;
1840 + if (func) { if (sqlDumpSuccess) { func('Auto-backup completed.'); } else { func('Auto-backup completed without mongodb database: ' + error); } }
1841 + obj.performCloudBackup(newAutoBackupPath + '.zip', func);
1842 + setTimeout(function () { try { parent.fs.unlink(newBackupPath + '.sql', function () { }); } catch (ex) { console.log(ex); } }, 5000);
1843 + });
1844 + output.on('end', function () { });
1845 + archive.on('warning', function (err) { console.log('Backup warning: ' + err); if (func) { func('Backup warning: ' + err); } });
1846 + archive.on('error', function (err) { console.log('Backup error: ' + err); if (func) { func('Backup error: ' + err); } });
1847 + archive.pipe(output);
1848 + if (sqlDumpSuccess == true) { archive.file(newBackupPath + '.sql', { name: newBackupFile + '.sql' }); }
1849 + archive.directory(parent.datapath, 'meshcentral-data');
1850 + archive.finalize();
1851 + } catch (ex) { console.log(ex); }
1852 + });
1853 } else {
1854 // Perform a NeDB backup
1855 var archiver = require('archiver');