Skip to content

Commit

Permalink
Merge pull request #19 from CodeLit/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
CodeLit authored Sep 24, 2023
2 parents 371ade5 + a4eae77 commit 56cbe2d
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 295 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

data
docker-compose.override.yml
temp

# Logs
logs
Expand Down
32 changes: 30 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,26 @@ docker-compose up

5. Check out the result.

<pre>
📦backups
┣ 📂annually
┃ ┣ 📜bkp_2022-08-01.tgz
┃ ┗ 📜bkp_2023-08-01.tgz
┣ 📂daily
┃ ┣ 📜bkp_2023-09-17.tgz
┃ ┣ 📜bkp_2023-09-18.tgz
┃ ┣ 📜bkp_2023-09-19.tgz
┃ ┗ 📜bkp_2023-09-20.tgz
┣ 📂monthly
┃ ┣ 📜bkp_2023-08-01.tgz
┃ ┗ 📜bkp_2023-09-01.tgz
┗ 📂weekly
┣ 📜bkp_2023-09-06.tgz
┣ 📜bkp_2023-09-13.tgz
┗ 📜bkp_2023-09-20.tgz

</pre>

## Advanced configuration

You can also exclude files and folders from backup. Use **filter** option
Expand All @@ -66,7 +86,8 @@ Example of backups-config.json:
"data/folder/*",
"package.lock",
"**/*_file-ending.*"
]
],
"compression_level": "default"
}
```

Expand All @@ -76,7 +97,7 @@ Type of backup, local or cloud

### `copies` option:

How many copies need to be in according folders, tree will be like this:
How many copies need to be in according folders, file tree will look like this:
<pre>
📦backups
┣ 📂annually
Expand All @@ -98,6 +119,13 @@ How many copies need to be in according folders, tree will be like this:
- Exclude **package.lock**
- Exclude files recursively by ending **_fileending**

### `compression_level` option:

- `default`: default compression, compromise between speed and compression
- `fast`: fastest compression
- `best`: best and slowest compression
- `none`: no compression

## Mega Online Cloud storage integration

[Link to official mega website](https://mega.nz/)
Expand Down
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@babel/core": "^7.22.20",
"@babel/eslint-parser": "^7.22.9",
"@types/fs-extra": "^11.0.2",
"archiver": "^6.0.1",
"babel-core": "^6.26.3",
"babel-eslint": "^10.1.0",
"babel-loader": "^9.1.3",
Expand All @@ -47,9 +48,6 @@
"megajs": "^1.1.4",
"node-cron": "^3.0.2",
"prettier": "^3.0.3",
"randomstring": "^1.2.2",
"recursive-copy": "^2.0.14",
"temp-dir": "^3.0.0",
"zip-a-folder": "^3.1.3"
"randomstring": "^1.2.2"
}
}
18 changes: 13 additions & 5 deletions src/backups.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'fs-extra';
import path from 'path';
import FolderBackup from './class/FolderBackup.js';
import logger from './class/Logger.js';
import zlib from 'zlib';

const jsonConfigName = 'backups-config.json';
const projectsPath = path.resolve('projects');
Expand Down Expand Up @@ -42,13 +43,20 @@ async function handeBackup(backupJson, projectPath, rewriteDate = undefined) {
logger.log('Current date is ' + backup.today.toDateString());

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

// Add ! at the beginning of each pattern to exclude it
backup.filter = backup.filter.map((element) => '!' + element);
const compressionMap = {
none: zlib.constants.Z_NO_COMPRESSION,
default: zlib.constants.Z_DEFAULT_COMPRESSION,
fast: zlib.constants.Z_BEST_SPEED,
best: zlib.constants.Z_BEST_COMPRESSION,
};

// Adding ** at the beginning of filter to match all files recursively
backup.filter.unshift('**');
if (backupJson.compression_level) {
backup.compressionLevel =
compressionMap[backupJson.compressionLevel] ||
zlib.constants.Z_DEFAULT_COMPRESSION;
}

if (backupJson.copies.daily)
await backup.makeBackups(backupJson.copies.daily, 'daily');
Expand Down
61 changes: 36 additions & 25 deletions src/class/FolderBackup.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { COMPRESSION_LEVEL, tar } from 'zip-a-folder';
import tempDirectory from 'temp-dir';
import randomString from 'randomstring';
import fs from 'fs-extra';
import _ from 'lodash';
import { Storage as MegaStorage } from 'megajs';
import copy from 'recursive-copy';
import logger from './Logger.js';
import path from 'path';
import archiver from 'archiver';
import fs from 'fs-extra';
import zlib from 'zlib';
import randomString from 'randomstring';

const temporaryFolder = path.resolve('temp');

export default class FolderBackup {
filter;
Expand All @@ -18,6 +19,7 @@ export default class FolderBackup {
this.fromFolder = options.fromFolder;
this.pathToBackups = options.pathToBackups;
this.today = new Date();
this.compressionLevel = zlib.constants.Z_DEFAULT_COMPRESSION;
}

static formatISODate(date) {
Expand Down Expand Up @@ -91,32 +93,41 @@ export default class FolderBackup {
}
}

const tmpBackupDir = path.normalize(
tempDirectory + '/' + randomString.generate(),
);
const tmpArchiveDir = path.normalize(
tempDirectory + '/' + randomString.generate(),
);

fs.mkdirSync(tmpBackupDir, { recursive: true });
fs.mkdirSync(tmpArchiveDir, { recursive: true });
if (!doNotCreate) {
const tmpArchive = path.normalize(
temporaryFolder + '/' + randomString.generate() + '.tgz',
);

await copy(this.fromFolder, tmpBackupDir, {
filter: this.filter,
});
let destination = pathToBackup;

if (!doNotCreate) {
const tmpArchive = path.normalize(tmpArchiveDir + '/temp.tgz');
if (this.type === 'mega-storage') {
fs.mkdirSync(temporaryFolder, { recursive: true });
destination = tmpArchive;
}

await tar(tmpBackupDir, tmpArchive, {
compression: COMPRESSION_LEVEL.high,
const archive = archiver('tar', {
gzip: true,
gzipOptions: { level: this.compressionLevel }, // Sets the compression level.
});

await this.fm.copy(tmpArchive, pathToBackup);
}
archive.pipe(fs.createWriteStream(destination));

fs.rmSync(tmpBackupDir, { recursive: true });
fs.rmSync(tmpArchiveDir, { recursive: true });
archive.glob(
'**/*',
{
cwd: this.fromFolder,
ignore: this.filter,
},
{},
);

await archive.finalize();

if (this.type === 'mega-storage') {
await this.fm.copy(tmpArchive, pathToBackup);
fs.rmSync(tmpArchive);
}
}

files = (await this.fm.readDir(pathToBackups)) || [];

Expand Down
Loading

0 comments on commit 56cbe2d

Please sign in to comment.