Skip to content

Commit

Permalink
Merge pull request #952 from simatec/dev
Browse files Browse the repository at this point in the history
(simatec) Beta 2.6.23
  • Loading branch information
simatec authored May 25, 2023
2 parents 67aa241 + 20e5e1f commit fd48060
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ When the adapter crashes or another Code error happens, this error message that

## Changelog
<!-- ### **WORK IN PROGRESS** -->
### **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
Expand Down
1 change: 1 addition & 0 deletions io-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@
"dropboxDir": "/backupDir",
"onedriveEnabled": false,
"onedriveAccessJson": "",
"onedriveLastTokenRenew": "",
"onedriveDeleteOldBackup": false,
"onedriveOwnDir": false,
"onedriveMinimalDir": "backupDir/iobroker",
Expand Down
22 changes: 22 additions & 0 deletions lib/oneDriveLib.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
2 changes: 1 addition & 1 deletion lib/restore/influxDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion lib/restore/pgsql.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
51 changes: 51 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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++;
Expand All @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit fd48060

Please sign in to comment.