Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
JackGruber committed Dec 10, 2022
2 parents 1a5f98e + c836fe1 commit e4e8e6c
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 21 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## not released

## v1.2.1 (2022-12-10)

- Fix: #47 Suppress repeating error message during automatic execution if the backup path is not accessible
- Fix: #48 File already exists when a RAW or MD Frontmatter backup with no revisions is made

## v1.2.0 (2022-11-20)

- Add: Option to select export format between JEX, MD with Frontmatter and RAW (Only supported with Joplin > 2.9.12)
Expand Down
45 changes: 45 additions & 0 deletions __test__/backup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,51 @@ describe("Backup", function () {
expect(fs.existsSync(expected)).toBe(true);
});

it(`retention = 1, folder exist with same files and folder`, async () => {
const emptyFolder = path.join(testPath.activeBackupJob, "emptyFolder");
const emptyFolderCheck = path.join(
testPath.backupBasePath,
"emptyFolder"
);
const folderNotes = path.join(testPath.activeBackupJob, "notes");
const folderNotesCheck = path.join(testPath.backupBasePath, "notes");
const file1 = path.join(folderNotes, "file.txt");
const file1Check = path.join(folderNotesCheck, "file.txt");
const file2 = path.join(testPath.activeBackupJob, "file.txt");
const file2Check = path.join(testPath.backupBasePath, "file.txt");
const file3Check = path.join(testPath.backupBasePath, "fileStay.txt");
const file4Check = path.join(folderNotesCheck, "fileNo.txt");
backup.backupBasePath = testPath.backupBasePath;
backup.activeBackupPath = testPath.activeBackupJob;

fs.emptyDirSync(testPath.backupBasePath);
fs.emptyDirSync(emptyFolderCheck);
fs.emptyDirSync(folderNotesCheck);
fs.writeFileSync(file1Check, "old file");
fs.writeFileSync(file2Check, "old file");
fs.writeFileSync(file3Check, "old file");
fs.writeFileSync(file4Check, "old file");

fs.emptyDirSync(testPath.activeBackupJob);
fs.emptyDirSync(emptyFolder);
fs.emptyDirSync(folderNotes);
fs.writeFileSync(file1, "new file");
fs.writeFileSync(file2, "new file");

backup.backupRetention = 1;

expect(await backup.moveFinishedBackup()).toBe(testPath.backupBasePath);
expect(fs.existsSync(folderNotes)).toBe(false);
expect(fs.existsSync(folderNotesCheck)).toBe(true);
expect(fs.existsSync(emptyFolderCheck)).toBe(true);
expect(fs.existsSync(file1Check)).toBe(true);
expect(fs.existsSync(file2Check)).toBe(true);
expect(fs.existsSync(file3Check)).toBe(true);
expect(fs.existsSync(file4Check)).toBe(false);
expect(backup.log.error).toHaveBeenCalledTimes(0);
expect(backup.log.warn).toHaveBeenCalledTimes(0);
});

it(`retention > 1, file exist`, async () => {
backup.backupBasePath = testPath.backupBasePath;
backup.activeBackupPath = testPath.activeBackupJob;
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "joplin-plugin-backup",
"version": "1.2.0",
"version": "1.2.1",
"scripts": {
"dist": "webpack --joplin-plugin-config buildMain && webpack --joplin-plugin-config buildExtraScripts && webpack --joplin-plugin-config createArchive",
"prepare": "npm run dist && husky install",
Expand Down
70 changes: 53 additions & 17 deletions src/Backup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Backup {
private backupSetName: string;
private exportFormat: string;
private execFinishCmd: string;
private suppressErrorMsgUntil: number;

constructor() {
this.log = backupLogging;
Expand All @@ -50,6 +51,7 @@ class Backup {
await sevenZip.updateBinPath();
await sevenZip.setExecutionFlag();
this.backupStartTime = null;
this.suppressErrorMsgUntil = 0;
}

private async upgradeBackupPluginVersion() {
Expand Down Expand Up @@ -225,8 +227,12 @@ class Backup {

if (this.createSubfolder) {
this.log.verbose("append subFolder");
const orgBackupBasePath = this.backupBasePath;
this.backupBasePath = path.join(this.backupBasePath, "JoplinBackup");
if (!fs.existsSync(this.backupBasePath)) {
if (
fs.existsSync(orgBackupBasePath) &&
!fs.existsSync(this.backupBasePath)
) {
try {
fs.mkdirSync(this.backupBasePath);
} catch (e) {
Expand Down Expand Up @@ -261,7 +267,7 @@ class Backup {
(await helper.validFileName(this.backupSetName)) === false
) {
this.backupSetName = "{YYYYMMDDHHmm}";
this.showError(
await this.showError(
'Backup set name does contain not allowed characters ( \\/:*?"<>| )!'
);
}
Expand Down Expand Up @@ -368,6 +374,8 @@ class Backup {
"backupVersion",
"backupPlugins",
"createSubfolder",
"createSubfolder",
"exportFormat",
];

this.log.verbose("Plugin settings:");
Expand Down Expand Up @@ -438,13 +446,32 @@ class Backup {
this.log.info("Backup completed");
this.moveLogFile(backupDst);

this.suppressErrorMsgUntil = 0;

if (showDoneMsg === true) {
await this.showMsg(`Backup completed`);
}
} else {
await this.showError(
`The Backup path '${this.backupBasePath}' does not exist!`
);
const now = new Date();

// Show error msg only every x hours on automatic runs
if (
showDoneMsg === false &&
this.suppressErrorMsgUntil > now.getTime()
) {
this.log.error(
`The Backup path '${this.backupBasePath}' does not exist!`
);
this.log.info("Error dialog suppressed");
} else {
await this.showError(
`The Backup path '${this.backupBasePath}' does not exist!`
);

if (showDoneMsg === false) {
this.suppressErrorMsgUntil = now.getTime() + 6 * 60 * 60 * 1000;
}
}
}

this.backupStartTime = null;
Expand Down Expand Up @@ -704,7 +731,7 @@ class Backup {
file
);
} catch (e) {
this.showError("Backup error", format + ": " + e.message);
await this.showError("Backup error", format + ": " + e.message);
throw e;
}
}
Expand Down Expand Up @@ -965,17 +992,20 @@ class Backup {
}
} else {
backupDestination = this.backupBasePath;
const oldBackupData = fs
const backupData = fs
.readdirSync(this.activeBackupPath, { withFileTypes: true })
.map((dirent) => dirent.name);
for (const file of oldBackupData) {
for (const file of backupData) {
let dst = path.join(backupDestination, file);
try {
fs.moveSync(
path.join(this.activeBackupPath, file),
path.join(backupDestination, file)
);
fs.moveSync(path.join(this.activeBackupPath, file), dst, {
overwrite: true,
});
} catch (e) {
await this.showError("moveFinishedBackup: " + e.message);
this.log.error(
path.join(this.activeBackupPath, file) + " => " + dst
);
throw e;
}
}
Expand All @@ -986,7 +1016,7 @@ class Backup {
recursive: true,
});
} catch (e) {
this.showError("moveFinishedBackup: " + e.message);
await this.showError("moveFinishedBackup: " + e.message);
throw e;
}
}
Expand All @@ -996,7 +1026,6 @@ class Backup {

private async clearBackupTarget(backupPath: string) {
this.log.verbose(`Clear backup target`);

// Remove only files
const oldBackupData = fs
.readdirSync(backupPath, { withFileTypes: true })
Expand All @@ -1011,23 +1040,30 @@ class Backup {
try {
fs.removeSync(path.join(backupPath, file));
} catch (e) {
await this.showError("" + e.message);
await this.showError("clearBackupTarget " + e.message);
throw e;
}
}
}

try {
fs.removeSync(path.join(backupPath, "notes"));
} catch (e) {
await this.showError("clearBackupTarget " + e.message);
throw e;
}

try {
fs.removeSync(path.join(backupPath, "templates"));
} catch (e) {
await this.showError("deleteOldBackupSets" + e.message);
await this.showError("clearBackupTarget " + e.message);
throw e;
}

try {
fs.removeSync(path.join(backupPath, "profile"));
} catch (e) {
await this.showError("deleteOldBackupSets" + e.message);
await this.showError("clearBackupTarget " + e.message);
throw e;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 1,
"id": "io.github.jackgruber.backup",
"app_min_version": "2.1.3",
"version": "1.2.0",
"version": "1.2.1",
"name": "Simple Backup",
"description": "Plugin to create manual and automatic backups.",
"author": "JackGruber",
Expand Down

0 comments on commit e4e8e6c

Please sign in to comment.