diff --git a/app.js b/app.js index aecd8db..24cdc89 100755 --- a/app.js +++ b/app.js @@ -122,8 +122,10 @@ app.get(routes.crontab, function(req, res, next) { // backup crontab db app.get(routes.backup, function(req, res) { - crontab.backup(); - res.end(); + crontab.backup((err) => { + if (err) next(err); + else res.end(); + }); }); // This renders the restore page similar to backup page diff --git a/crontab.js b/crontab.js index 4f59b68..d9a60f1 100644 --- a/crontab.js +++ b/crontab.js @@ -220,27 +220,24 @@ exports.get_backup_names = function(){ } }); - // Sort by date. Newest on top - for(var i=0; i Tj){ - var temp = backups[i]; - backups[i] = backups[j]; - backups[j] = temp; - } - } + let backup_date = (backup_name) => { + let T = backup_name.split("backup")[1]; + return new Date(T.substring(0, T.length-3)).valueOf(); } + backups.sort((a, b) => backup_date(b) - backup_date(a)); + return backups; }; -exports.backup = function(){ - //TODO check if it failed - fs.createReadStream(exports.crontab_db_file).pipe(fs.createWriteStream( path.join(exports.db_folder, 'backup ' + (new Date()).toString().replace("+", " ") + '.db'))); +exports.backup = (callback) => { + fs.copyFile(exports.crontab_db_file, path.join(exports.db_folder, 'backup ' + (new Date()).toString().replace("+", " ") + '.db'), (err) => { + if (err) { + console.error(err); + return callback(err); + } + callback(); + }); }; exports.restore = function(db_name){