Skip to content

Commit

Permalink
Merge pull request #12 from CodeLit/development
Browse files Browse the repository at this point in the history
Mega huge fixes.
  • Loading branch information
CodeLit authored Sep 22, 2023
2 parents 245dbf3 + acef3c7 commit 516831f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 32 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,21 @@ Config example:
"type": "mega-storage",
"email": "email-name@mail.me",
"password": "!test24passWord",
"backups_path": "path/to/backups/my-project",
"copies": {
"daily": 1
}
},
"filter": [
"**/*.log"
]
}
```

docker-compose.override.yml for Mega:

```yml
services:
super-easy-file-backups:
volumes:
- C:/Users/Adam/Desktop/Projects/gameServer:/app/projects/gameServer:ro
```
48 changes: 26 additions & 22 deletions src/backups.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,28 @@ const backupsPath = path.resolve('backups');
/**
* A function to handle a backup operation.
*
* @param {object} backupJsonData - The backup JSON data.
* @param {object} backupJson - The backup JSON data.
* @param {string} projectPath - The path to the project.
* @param {Date} [rewriteDate=undefined] - The date to rewrite.
*/
async function handeBackup(
backupJsonData,
projectPath,
rewriteDate = undefined,
) {
async function handeBackup(backupJson, projectPath, rewriteDate = undefined) {
if (!backupJson.type) throw new Error('Missing backup type option.');
if (backupJson.type === 'mega-storage') {
if (!backupJson.email || !backupJson.password)
throw new Error('Missing Mega credentials.');
if (!backupJson.backups_path)
throw new Error('Missing Mega backups destination path.');
}

const backup = new FolderBackup({
type: backupJsonData.type,
'from-folder': projectPath,
'path-to-backups': path.normalize(
`${backupsPath}/${path.basename(projectPath)}`,
),
'no-info-messages': false, // Disabling info messages from console.info
type: backupJson.type,
email: backupJson.email,
password: backupJson.password,
fromFolder: projectPath,
pathToBackups:
backupJson.type === 'mega-storage'
? backupJson.backups_path
: path.normalize(`${backupsPath}/${path.basename(projectPath)}`),
});

// Initialize file system
Expand All @@ -36,22 +42,20 @@ async function handeBackup(
logger.log('Current date is ' + backup.today.toDateString());

// https://www.npmjs.com/package/maximatch
backup.filter = backupJsonData.filter || [];
backup.filter = backupJson.filter || [];

// Add ! at the beginning of each pattern to exclude it
backup.filter = backup.filter.map((element) => '!' + element);

// Adding ** at the beginning of filter to match all files recursively
backup.filter.unshift('**');

if (backupJsonData.copies.daily)
await backup.daily(backupJsonData.copies.daily);
if (backupJsonData.copies.weekly)
await backup.weekly(backupJsonData.copies.weekly);
if (backupJsonData.copies.monthly)
await backup.monthly(backupJsonData.copies.monthly);
if (backupJsonData.copies.annually)
await backup.annually(backupJsonData.copies.annually);
if (backupJson.copies.daily) await backup.daily(backupJson.copies.daily);
if (backupJson.copies.weekly) await backup.weekly(backupJson.copies.weekly);
if (backupJson.copies.monthly)
await backup.monthly(backupJson.copies.monthly);
if (backupJson.copies.annually)
await backup.annually(backupJson.copies.annually);
await backup.closeConnection();
}

Expand Down Expand Up @@ -93,7 +97,7 @@ async function startBackups(rewriteDate = undefined, synchronously = false) {
}
}
} catch (e) {
logger.error(e);
logger.error(e.stack);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/class/FolderBackup.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ export default class FolderBackup {

constructor(options) {
this.options = options;
this.type = options['type'] || 'local-storage';
this.fromFolder = options['from-folder'];
this.pathToBackups = options['path-to-backups'];
this.type = options.type || 'local-storage';
this.fromFolder = options.fromFolder;
this.pathToBackups = options.pathToBackups;
this.today = new Date();
}

Expand All @@ -38,7 +38,7 @@ export default class FolderBackup {
case 'mega-storage':
this.fm.storage = new MegaStorage(this.options);
this.fm.storage.once('error', (error) => {
logger.error(error);
logger.error(error.stack);
});
await this.fm.storage.ready;
break;
Expand Down
21 changes: 16 additions & 5 deletions src/class/storage_base/mega-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,28 @@ class MegaFileBase {
return obj !== false;
}

/**
* Asynchronously creates a directory at the specified path.
*
* @param {string} path - The path to the directory.
* @return {boolean} Returns true if the directory is created successfully.
*/
async makeDir(path) {
path = path.split('/');
const pathArr = path.split('/');

if (pathArr[0] === '') pathArr.shift();

async function findObject(nextObj) {
if (!nextObj.directory || path.length === 0) return;
if (!nextObj.directory || pathArr.length === 0) return;

let children = nextObj.children || [];
let found = children.find((obj) => obj.name === path[0]);
let found = children.find((obj) => obj.name === pathArr[0]);

if (found === undefined) {
found = await nextObj.mkdir(path[0]);
found = await nextObj.mkdir(pathArr[0]);
}

await path.shift();
pathArr.shift();

await findObject(found);
}
Expand Down Expand Up @@ -112,6 +120,9 @@ class MegaFileBase {
*/
async copy(fromFile, toFile) {
let toFileArr = toFile.split('/');

if (toFileArr[0] === '') toFileArr.shift();

let toFileName = toFileArr.pop();
let dir = this.find(toFileArr.join('/'));

Expand Down

0 comments on commit 516831f

Please sign in to comment.