2019-06-17 21:26:46 +00:00
|
|
|
var request = require('request-promise');
|
|
|
|
var fs = require('fs');
|
2019-09-01 17:01:38 +00:00
|
|
|
var common = require('../../common');
|
|
|
|
var logger = require('../logger');
|
2019-06-17 21:26:46 +00:00
|
|
|
var options = {};
|
|
|
|
|
2019-10-08 02:00:56 +00:00
|
|
|
function getFilesList(callback) {
|
|
|
|
let files_list = [];
|
|
|
|
let all_restore_exists = false;
|
|
|
|
let response = {all_restore_exists: false, files: []};
|
|
|
|
fs.readdir(common.selectedNode.channel_backup_path + common.path_separator + 'restore', function (err, files) {
|
|
|
|
if (err && err.code !== 'ENOENT' && err.errno !== -4058) { response = { message: 'Channels Restore List Failed!', error: err } }
|
2019-10-27 20:39:40 +00:00
|
|
|
if(undefined !== files && files.length > 0) {
|
|
|
|
files.forEach(file => {
|
|
|
|
if (!file.includes('.restored')) {
|
|
|
|
if (file === 'channel-all.bak') {
|
|
|
|
all_restore_exists = true;
|
|
|
|
} else {
|
|
|
|
files_list.push({channel_point: file.substring(8, file.length - 4).replace('-', ':')});
|
|
|
|
}
|
2019-10-08 02:00:56 +00:00
|
|
|
}
|
2019-10-27 20:39:40 +00:00
|
|
|
});
|
|
|
|
}
|
2019-10-08 02:00:56 +00:00
|
|
|
response = {all_restore_exists: all_restore_exists, files: files_list};
|
|
|
|
callback(response);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-17 21:26:46 +00:00
|
|
|
exports.getBackup = (req, res, next) => {
|
|
|
|
options = common.getOptions();
|
|
|
|
let channel_backup_file = '';
|
|
|
|
let message = '';
|
|
|
|
if (req.params.channelPoint === 'ALL') {
|
2019-07-28 18:00:15 +00:00
|
|
|
channel_backup_file = common.selectedNode.channel_backup_path + common.path_separator + 'channel-all.bak';
|
2019-11-24 22:56:57 +00:00
|
|
|
message = 'All Channels Backup Successful.';
|
|
|
|
// message = 'All Channels Backup Successful at: ' + channel_backup_file + ' !';
|
2019-09-02 04:11:37 +00:00
|
|
|
options.url = common.getSelLNServerUrl() + '/channels/backup';
|
2019-06-17 21:26:46 +00:00
|
|
|
} else {
|
2019-07-28 18:00:15 +00:00
|
|
|
channel_backup_file = common.selectedNode.channel_backup_path + common.path_separator + 'channel-' + req.params.channelPoint.replace(':', '-') + '.bak';
|
2019-11-24 22:56:57 +00:00
|
|
|
message = 'Channel Backup Successful.';
|
|
|
|
// message = 'Channel Backup Successful at: ' + channel_backup_file + ' !';
|
2019-06-17 21:26:46 +00:00
|
|
|
let channelpoint = req.params.channelPoint.replace(':', '/');
|
2019-09-02 04:11:37 +00:00
|
|
|
options.url = common.getSelLNServerUrl() + '/channels/backup/' + channelpoint;
|
2019-06-17 21:26:46 +00:00
|
|
|
let exists = fs.existsSync(channel_backup_file);
|
|
|
|
if (exists) {
|
|
|
|
fs.writeFile(channel_backup_file, '', () => { });
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
var createStream = fs.createWriteStream(channel_backup_file);
|
|
|
|
createStream.end();
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
return res.status(500).json({ message: 'Channels Backup Failed!', error: err });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
request(options).then(function (body) {
|
2019-07-27 18:20:17 +00:00
|
|
|
logger.info({fileName: 'Channels Backup', msg: 'Channel Backup: ' + JSON.stringify(body)});
|
2019-06-17 21:26:46 +00:00
|
|
|
fs.writeFile(channel_backup_file, JSON.stringify(body), function(err) {
|
|
|
|
if (err) {
|
|
|
|
return res.status(500).json({ message: 'Channels Backup Failed!', error: err.error });
|
|
|
|
} else {
|
|
|
|
res.status(200).json({ message: message });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
2019-07-27 18:20:17 +00:00
|
|
|
logger.error({fileName: 'Channels Backup', lineNum: 44, msg: 'Channel Backup: ' + JSON.stringify(err)});
|
2019-06-17 21:26:46 +00:00
|
|
|
return res.status(500).json({
|
|
|
|
message: 'Channels Backup Failed!',
|
|
|
|
error: err.error
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.postBackupVerify = (req, res, next) => {
|
|
|
|
options = common.getOptions();
|
2019-09-02 04:11:37 +00:00
|
|
|
options.url = common.getSelLNServerUrl() + '/channels/backup/verify';
|
2019-06-17 21:26:46 +00:00
|
|
|
let channel_verify_file = '';
|
|
|
|
let message = '';
|
|
|
|
let verify_backup = '';
|
|
|
|
if (req.params.channelPoint === 'ALL') {
|
2019-11-24 22:56:57 +00:00
|
|
|
message = 'All Channels Verify Successful.';
|
2019-07-28 18:00:15 +00:00
|
|
|
channel_verify_file = common.selectedNode.channel_backup_path + common.path_separator + 'channel-all.bak';
|
2019-06-17 21:26:46 +00:00
|
|
|
let exists = fs.existsSync(channel_verify_file);
|
|
|
|
if (exists) {
|
|
|
|
verify_backup = fs.readFileSync(channel_verify_file, 'utf-8');
|
|
|
|
if (verify_backup !== '') {
|
|
|
|
verify_backup = JSON.parse(verify_backup);
|
|
|
|
delete verify_backup.single_chan_backups;
|
|
|
|
options.form = JSON.stringify(verify_backup);
|
|
|
|
} else {
|
|
|
|
res.status(404).json({ message: 'Channels backup to verify does not Exist!' });
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
verify_backup = '';
|
|
|
|
res.status(404).json({ message: 'Channels backup to verify does not Exist!' });
|
|
|
|
}
|
|
|
|
} else {
|
2019-11-24 22:56:57 +00:00
|
|
|
message = 'Channel Verify Successful.';
|
|
|
|
// message = 'Channel ' + req.params.channelPoint + ' Verify Successful!';
|
2019-07-28 18:00:15 +00:00
|
|
|
channel_verify_file = common.selectedNode.channel_backup_path + common.path_separator + 'channel-' + req.params.channelPoint.replace(':', '-') + '.bak';
|
2019-06-17 21:26:46 +00:00
|
|
|
let exists = fs.existsSync(channel_verify_file);
|
|
|
|
if (exists) {
|
|
|
|
verify_backup = fs.readFileSync(channel_verify_file, 'utf-8');
|
|
|
|
options.form = JSON.stringify({ single_chan_backups: { chan_backups: [JSON.parse(verify_backup)] } });
|
|
|
|
} else {
|
|
|
|
verify_backup = '';
|
|
|
|
res.status(404).json({ message: 'Channel backup to verify does not Exist!' });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (verify_backup !== '') {
|
|
|
|
request.post(options).then(function (body) {
|
2019-07-27 18:20:17 +00:00
|
|
|
logger.info({fileName: 'Channels Backup Verify', msg: 'Channel Backup Verify: ' + JSON.stringify(body)});
|
2019-06-17 21:26:46 +00:00
|
|
|
res.status(201).json({ message: message });
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
2019-07-27 18:20:17 +00:00
|
|
|
logger.error({fileName: 'Channels Backup Verify', lineNum: 93, msg: 'Channel Backup Verify: ' + JSON.stringify(err)});
|
2019-06-17 21:26:46 +00:00
|
|
|
return res.status(404).json({
|
|
|
|
message: 'Channel backup to Verify failed!',
|
|
|
|
error: err.error
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2019-10-02 20:38:15 +00:00
|
|
|
|
|
|
|
exports.postRestore = (req, res, next) => {
|
|
|
|
options = common.getOptions();
|
|
|
|
options.url = common.getSelLNServerUrl() + '/channels/backup/restore';
|
|
|
|
let channel_restore_file = '';
|
|
|
|
let message = '';
|
|
|
|
let restore_backup = '';
|
|
|
|
if (req.params.channelPoint === 'ALL') {
|
2019-11-24 22:56:57 +00:00
|
|
|
message = 'All Channels Restore Successful.';
|
2019-10-07 23:10:21 +00:00
|
|
|
channel_restore_file = common.selectedNode.channel_backup_path + common.path_separator + 'restore' + common.path_separator + 'channel-all.bak';
|
2019-10-02 20:38:15 +00:00
|
|
|
let exists = fs.existsSync(channel_restore_file);
|
|
|
|
if (exists) {
|
|
|
|
restore_backup = fs.readFileSync(channel_restore_file, 'utf-8');
|
|
|
|
if (restore_backup !== '') {
|
|
|
|
restore_backup = JSON.parse(restore_backup);
|
|
|
|
options.form = JSON.stringify({multi_chan_backup: restore_backup.multi_chan_backup.multi_chan_backup});
|
|
|
|
} else {
|
|
|
|
res.status(404).json({ message: 'Channels backup to restore does not Exist!' });
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
restore_backup = '';
|
|
|
|
res.status(404).json({ message: 'Channels backup to restore does not Exist!' });
|
|
|
|
}
|
|
|
|
} else {
|
2019-11-24 22:56:57 +00:00
|
|
|
message = 'Channel Restore Successful.';
|
|
|
|
// message = 'Channel ' + req.params.channelPoint + ' Restore Successful!';
|
2019-10-07 23:10:21 +00:00
|
|
|
channel_restore_file = common.selectedNode.channel_backup_path + common.path_separator + 'restore' + common.path_separator + 'channel-' + req.params.channelPoint.replace(':', '-') + '.bak';
|
2019-10-02 20:38:15 +00:00
|
|
|
let exists = fs.existsSync(channel_restore_file);
|
|
|
|
if (exists) {
|
|
|
|
restore_backup = fs.readFileSync(channel_restore_file, 'utf-8');
|
|
|
|
options.form = JSON.stringify({ chan_backups: { chan_backups: [JSON.parse(restore_backup)] } });
|
|
|
|
} else {
|
|
|
|
restore_backup = '';
|
|
|
|
res.status(404).json({ message: 'Channel backup to restore does not Exist!' });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (restore_backup !== '') {
|
|
|
|
request.post(options).then(function (body) {
|
|
|
|
logger.info({fileName: 'Channels Backup Restore', msg: 'Channel Backup Restore: ' + JSON.stringify(body)});
|
2019-10-08 02:00:56 +00:00
|
|
|
fs.rename(channel_restore_file, channel_restore_file + '.restored', () => {
|
|
|
|
getFilesList(getFilesListRes => {
|
|
|
|
if (getFilesListRes.error) {
|
|
|
|
return res.status(500).json({ message: 'Channel restore failed!', list: getFilesListRes });
|
|
|
|
} else {
|
2019-10-09 01:25:19 +00:00
|
|
|
return res.status(201).json({ message: message, list: getFilesListRes });
|
2019-10-08 02:00:56 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2019-10-02 20:38:15 +00:00
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
logger.error({fileName: 'Channels Backup Restore', lineNum: 143, msg: 'Channel Backup Restore: ' + JSON.stringify(err)});
|
|
|
|
return res.status(404).json({
|
|
|
|
message: 'Channel restore failed!',
|
2019-10-07 23:10:21 +00:00
|
|
|
error: err.error.error
|
2019-10-02 20:38:15 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2019-10-07 23:10:21 +00:00
|
|
|
|
|
|
|
exports.getRestoreList = (req, res, next) => {
|
2019-10-08 02:00:56 +00:00
|
|
|
getFilesList(getFilesListRes => {
|
|
|
|
if (getFilesListRes.error) {
|
|
|
|
return res.status(500).json(getFilesListRes);
|
|
|
|
} else {
|
|
|
|
return res.status(200).json(getFilesListRes);
|
2019-10-07 23:10:21 +00:00
|
|
|
}
|
2019-10-08 02:00:56 +00:00
|
|
|
});
|
2019-10-07 23:10:21 +00:00
|
|
|
};
|