Skip to content
This repository has been archived by the owner on Dec 28, 2022. It is now read-only.

Commit

Permalink
cleanup backups
Browse files Browse the repository at this point in the history
  • Loading branch information
hobbyquaker committed Mar 8, 2020
1 parent 9aea8b0 commit 90bfcc5
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions nodes/shepherd.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,27 @@ module.exports = function (RED) {
fs.copyFileSync(this.backupPath, this.backupPath + '.' + now);
}

fs.readdir(this.persistPath, (err, dir) => {
const count = {dev: 0, backup: 0};
const remove = [];

const now = (new Date()).getTime();
const maxAge = now - this.maxBackupAge;

dir.sort().reverse().forEach(file => {
const match = file.match(/^(backup|dev)\.db\.(\d+)$/);
if (match) {
const [, type, timestamp] = match;
if (++count[type] > this.maxBackupCount || parseInt(timestamp, 10) < maxAge) {
remove.push(file);
}
}
});
remove.forEach(file => {
fs.unlink(path.join(this.persistPath, file), () => {});
});
});

try {
this.names = require(this.namesPath);
} catch (_) {
Expand Down

0 comments on commit 90bfcc5

Please sign in to comment.