Skip to content

Commit

Permalink
Merge pull request #2 from CodeLit/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
CodeLit authored Sep 14, 2023
2 parents ad5c49c + 6615db7 commit 0dd9aa4
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 30 deletions.
6 changes: 3 additions & 3 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
{
"singleQuote": true
}
]
],
// "default-case": "warn",
// "no-param-reassign": "off",
// "no-console": "off",
// "prefer-const": "warn",
// "no-var": "warn",
"no-var": "warn"
// "semi": "warn",
// "padded-blocks": "warn",
// "space-before-blocks": "warn",
Expand All @@ -34,4 +34,4 @@
"plugins": [
"prettier"
]
}
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Super Easy backups

- Easy to set up (about 5 minutes)
- Removes old backups (backups rotation), saving space on your device
- Multiple annual, monthly, weekly backups by your choice
- #### Easy to set up (about 5 minutes)
- #### Removes old backups (backups rotation), saving space on your device
- #### Multiple annual, monthly, weekly backups by your choice

Tested on: Windows 11, Ubuntu 22.02

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"lint:fix": "npm run lint -- --fix",
"prettier": "npx prettier src test --check",
"prettier:fix": "npm run prettier -- --write",
"format": "npm run prettier:fix && npm run lint:fix"
"format": "npm run prettier:fix && npm run lint:fix",
"compose-tests": "docker-compose -f docker-compose-tests.yml up --build"
},
"author": {
"name": "CodeLit",
Expand Down
43 changes: 20 additions & 23 deletions src/class/FolderBackup.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default class FolderBackup {
}

static formatISODate(date) {
let params = {};
const params = {};
params.dd = String(date.getDate()).padStart(2, '0');
params.mm = String(date.getMonth() + 1).padStart(2, '0');
params.yyyy = date.getFullYear();
Expand All @@ -31,7 +31,8 @@ export default class FolderBackup {

async init() {
// fm - file manager
let fmImport = await import('./storage_base/' + this.type + '.js');
const fmImport = await import('./storage_base/' + this.type + '.js');

this.fm = new fmImport.default(this.options);

switch (this.type) {
Expand Down Expand Up @@ -93,15 +94,15 @@ export default class FolderBackup {
async makeBackups(count = 3, type) {
logger.log(_.capitalize(type) + ' backups started.');
const pathToBackups = path.normalize(this.pathToBackups + '/' + type);
let date = FolderBackup.formatISODate(this.today);
const date = FolderBackup.formatISODate(this.today);

let pathToBackup = path.normalize(pathToBackups + '/bkp_' + date + '.tgz');
const pathToBackup = path.normalize(
pathToBackups + '/bkp_' + date + '.tgz',
);

if (!this.fm.exists(pathToBackups)) await this.fm.makeDir(pathToBackups);

logger.log('Backups destination folder created.');

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

let doNotCreate = false;

Expand All @@ -110,8 +111,8 @@ export default class FolderBackup {
fileDate = new Date(fileDate[0]);

if (type === 'weekly') {
let week = currentWeekNumber(this.today);
let week2 = currentWeekNumber(fileDate);
const week = currentWeekNumber(this.today);
const week2 = currentWeekNumber(fileDate);
if (week === week2) {
doNotCreate = true;
break;
Expand All @@ -129,10 +130,10 @@ export default class FolderBackup {
}
}

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

Expand All @@ -143,32 +144,28 @@ export default class FolderBackup {
filter: this.filter,
});

logger.log('Project Directory was copied to temp folder.');

if (!this.fm.exists(pathToBackup) && !doNotCreate) {
let tmpArchive = path.normalize(tmpArchiveDir + '/temp.tgz');
const tmpArchive = path.normalize(tmpArchiveDir + '/temp.tgz');

await tar(tmpBackupDir, tmpArchive, {
compression: COMPRESSION_LEVEL.high,
});

logger.log('Temporary archive created.');

await this.fm.copy(tmpArchive, pathToBackup);

logger.log('Files copied (or uploaded) to backup destination.');
}

fs.rmSync(tmpBackupDir, { recursive: true });
fs.rmSync(tmpArchiveDir, { recursive: true });

logger.log('Temporary directories removed.');
files = (await this.fm.readDir(pathToBackups)) || [];

let countFilesToRemove = files.length - count;
const countFilesToRemove = files.length - count;

for (let i = 0; i < countFilesToRemove; i++) {
let fileToRemove = path.normalize(pathToBackups + '/' + files[i]);
await this.fm.rmFile(fileToRemove);
if (files.length > 0) {
for (let i = 0; i < countFilesToRemove; i++) {
const fileToRemove = path.normalize(pathToBackups + '/' + files[i]);
await this.fm.rmFile(fileToRemove);
}
}

logger.log(_.capitalize(type) + ' backups is done.');
Expand Down

0 comments on commit 0dd9aa4

Please sign in to comment.