From 20e5e1f358a97da04d4a29506ff6b7d4135a08af Mon Sep 17 00:00:00 2001 From: simatec Date: Thu, 25 May 2023 12:56:06 +0200 Subject: [PATCH] (simatec) Beta 2.6.23 --- README.md | 5 ++++ io-package.json | 1 + lib/oneDriveLib.js | 22 ++++++++++++++++++ lib/restore/influxDB.js | 2 +- lib/restore/pgsql.js | 3 ++- main.js | 51 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 82 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 868955c7..95372d66 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,11 @@ When the adapter crashes or another Code error happens, this error message that ## Changelog +### **WORK IN PROGRESS** +* (simatec) Fix Influx Restore for MultiDB +* (simatec) Token renew for Onedrive added +* (simatec) Fix PSQL Restore + ### 2.6.22 (2023-05-24) * (simatec) Fix Influx Restore for MultiDB * (simatec) Default Ports for InfluxDB added diff --git a/io-package.json b/io-package.json index 7d810e0e..87996eac 100644 --- a/io-package.json +++ b/io-package.json @@ -275,6 +275,7 @@ "dropboxDir": "/backupDir", "onedriveEnabled": false, "onedriveAccessJson": "", + "onedriveLastTokenRenew": "", "onedriveDeleteOldBackup": false, "onedriveOwnDir": false, "onedriveMinimalDir": "backupDir/iobroker", diff --git a/lib/oneDriveLib.js b/lib/oneDriveLib.js index 22b9a9ca..9ddb254d 100644 --- a/lib/oneDriveLib.js +++ b/lib/oneDriveLib.js @@ -100,6 +100,28 @@ class onedrive { } }); } + + renewToken(refreshToken, log) { + return new Promise(async (resolve, reject) => { + try { + const data = `refresh_token=${refreshToken}&grant_type=refresh_token&client_id=${await this.getClientID(log)}`; + + const accessToken = await axios(url, { + method: 'post', + data: data + }); + + if (accessToken && accessToken.data && accessToken.data.refresh_token) { + resolve(accessToken.data.refresh_token); + } else { + reject(); + } + } catch (e) { + log.warn('refresh_token Onedrive: ' + e); + reject(); + } + }); + } } module.exports = onedrive; \ No newline at end of file diff --git a/lib/restore/influxDB.js b/lib/restore/influxDB.js index bc429e9a..80b64369 100644 --- a/lib/restore/influxDB.js +++ b/lib/restore/influxDB.js @@ -19,7 +19,7 @@ function replayInfuxDB(options, tmpDir, log, callback) { const json = JSON.parse(manifest); options.dbversion = json.files ? '1.x' : json.buckets ? '2.x' : options.dbversion; - dbName = options.dbversion == '1.x' ? json.files[0].database : options.dbversion == '2.x' ? json.buckets.bucketName : options.dbName; + dbName = options.dbversion == '1.x' ? json.files[0].database : options.dbversion == '2.x' ? json.buckets[0].bucketName : options.dbName; } }); } catch (err) { diff --git a/lib/restore/pgsql.js b/lib/restore/pgsql.js index 32842ba1..a3a9444f 100644 --- a/lib/restore/pgsql.js +++ b/lib/restore/pgsql.js @@ -7,7 +7,8 @@ function replayPgSql(options, fileNamePgsql, log, callback) { const cmdCreate = `psql -c "create database ${options.dbName};" postgresql://${options.user}:${options.pass}@${options.host}:${options.port}/`; try { exec(cmdCreate, (error, stdout, stderr) => { - const cmd = `pg_restore --dbname=postgresql://${options.user}:${options.pass}@${options.host}:${options.port}/${options.dbName} < ${fileNamePgsql}`; + //const cmd = `pg_restore --dbname=postgresql://${options.user}:${options.pass}@${options.host}:${options.port}/${options.dbName} < ${fileNamePgsql}`; + const cmd = `pg_dump --format=custom --dbname=postgresql://${options.user}:${options.pass}@${options.host}:${options.port}/${options.dbName} < ${fileNamePgsql}`; try { const child = exec(cmd, (error, stdout, stderr) => { if (error) log.error(stderr); diff --git a/main.js b/main.js index 7172faf7..4b300689 100644 --- a/main.js +++ b/main.js @@ -123,6 +123,10 @@ function startAdapter(options) { if (state && state.val === '[EXIT] 0') { adapter.setState(`history.${type}Success`, true, true); adapter.setState(`history.${type}LastTime`, tools.getTimeString(systemLang), true); + + if (adapter.config.onedriveEnabled && adapter.config.hostType === 'Single') { + renewOnedriveToken(); + } } else { adapter.setState(`history.${type}LastTime`, 'error: ' + tools.getTimeString(systemLang), true); adapter.setState(`history.${type}Success`, false, true); @@ -433,6 +437,10 @@ function startAdapter(options) { err && adapter.log.error(err); adapter.log.error('slave Backup not finish!'); } + + if (adapter.config.onedriveEnabled) { + renewOnedriveToken(); + } } else { adapter.setState(`history.${type}LastTime`, 'error: ' + tools.getTimeString(systemLang), true); adapter.setState(`history.${type}Success`, false, true); @@ -538,6 +546,10 @@ function createBackupSchedule() { if (state && state.val === '[EXIT] 0') { adapter.setState(`history.${type}Success`, true, true); adapter.setState(`history.${type}LastTime`, tools.getTimeString(systemLang), true); + + if (adapter.config.onedriveEnabled && adapter.config.hostType === 'Single') { + renewOnedriveToken(); + } } else { adapter.setState(`history.${type}LastTime`, 'error: ' + tools.getTimeString(systemLang), true); adapter.setState(`history.${type}Success`, false, true); @@ -1464,6 +1476,10 @@ async function startSlaveBackup(slaveInstance, num) { return slaveTimeOut = setTimeout(startSlaveBackup, 3000, adapter.config.slaveInstance[num], num); } else { adapter.log.debug('slave backups are completed'); + + if (adapter.config.onedriveEnabled) { + renewOnedriveToken(); + } } } else { num++; @@ -1473,6 +1489,10 @@ async function startSlaveBackup(slaveInstance, num) { return slaveTimeOut = setTimeout(startSlaveBackup, 3000, adapter.config.slaveInstance[num], num); } else { adapter.log.debug('slave backups are completed'); + + if (adapter.config.onedriveEnabled) { + renewOnedriveToken(); + } } } } catch (err) { @@ -1597,6 +1617,37 @@ function fileServer(protocol) { } } +async function renewOnedriveToken() { + const Onedrive = require('./lib/oneDriveLib'); + const onedrive = new Onedrive(); + + let currentDay = new Date(); + let diffDays; + + if (adapter.config.onedriveLastTokenRenew != '') { + const lastRenew = new Date(adapter.config.onedriveLastTokenRenew); + + diffDays = parseInt((currentDay - lastRenew) / (1000 * 60 * 60 * 24)); //day difference + } + + if (diffDays >= 30 || adapter.config.onedriveLastTokenRenew == '') { + adapter.log.debug('Renew Onedrive Refresh-Token'); + + onedrive.renewToken(adapter.config.onedriveAccessJson, adapter.log) + .then(refreshToken => { + adapter.extendForeignObject(`system.adapter.${adapter.namespace}`, { + native: { + onedriveAccessJson: refreshToken, + onedriveLastTokenRenew: ('0' + (currentDay.getMonth() + 1)).slice(-2) + '/' + ('0' + currentDay.getDate()).slice(-2) + '/' + currentDay.getFullYear() + } + }); + }) + .catch(err => adapter.log.error(err)); + } else { + adapter.log.debug(`Renew Onedrive Refresh-Token in ${30 - diffDays} days`); + } +} + async function main(adapter) { createBashScripts(); readLogFile();