split uploaded and generated backups
This commit is contained in:
parent
542a77fbef
commit
0d6103170a
6 changed files with 122 additions and 15 deletions
|
@ -4,6 +4,8 @@ import { EntityManager } from "typeorm";
|
|||
import uniqBy from "lodash.uniqby";
|
||||
import InternalException from "../exceptions/internalException";
|
||||
import UserService from "../service/user/userService";
|
||||
import { BACKUP_COPIES, BACKUP_INTERVAL } from "../env.defaults";
|
||||
import DatabaseActionException from "../exceptions/databaseActionException";
|
||||
|
||||
export type BackupSection =
|
||||
| "member"
|
||||
|
@ -97,7 +99,26 @@ export default abstract class BackupHelper {
|
|||
|
||||
FileSystemHelper.writeFile(path, filename + ".json", JSON.stringify(json, null, 2));
|
||||
|
||||
// TODO: delete older backups by copies env
|
||||
let files = FileSystemHelper.getFilesInDirectory("backup", ".json");
|
||||
let sorted = files.sort((a, b) => new Date(b.split(".")[0]).getTime() - new Date(a.split(".")[0]).getTime());
|
||||
|
||||
const filesToDelete = sorted.slice(BACKUP_COPIES);
|
||||
for (const file of filesToDelete) {
|
||||
FileSystemHelper.deleteFile("backup", file);
|
||||
}
|
||||
}
|
||||
|
||||
static async createBackupOnInterval() {
|
||||
let files = FileSystemHelper.getFilesInDirectory("backup", ".json");
|
||||
let newestFile = files.sort((a, b) => new Date(b.split(".")[0]).getTime() - new Date(a.split(".")[0]).getTime())[0];
|
||||
|
||||
let lastBackup = new Date(newestFile.split(".")[0]);
|
||||
let diffInMs = new Date().getTime() - lastBackup.getTime();
|
||||
let diffInDays = diffInMs / (1000 * 60 * 60 * 24);
|
||||
|
||||
if (diffInDays >= BACKUP_INTERVAL) {
|
||||
await this.createBackup({});
|
||||
}
|
||||
}
|
||||
|
||||
static async loadBackup({
|
||||
|
@ -105,11 +126,13 @@ export default abstract class BackupHelper {
|
|||
path = "/backup",
|
||||
include = [],
|
||||
partial = false,
|
||||
overwrite = false,
|
||||
}: {
|
||||
filename: string;
|
||||
path?: string;
|
||||
partial?: boolean;
|
||||
include?: Array<BackupSection>;
|
||||
overwrite?: boolean;
|
||||
}): Promise<void> {
|
||||
this.transactionManager = undefined;
|
||||
|
||||
|
@ -127,10 +150,12 @@ export default abstract class BackupHelper {
|
|||
const sections = this.backupSection
|
||||
.filter((bs) => (partial ? include.includes(bs.type) : true))
|
||||
.sort((a, b) => a.orderOnClear - b.orderOnClear);
|
||||
for (const section of sections.filter((s) => Object.keys(backup).includes(s.type))) {
|
||||
let refered = this.backupSectionRefered[section.type];
|
||||
for (const ref of refered) {
|
||||
await this.transactionManager.getRepository(ref).delete({});
|
||||
if (!overwrite) {
|
||||
for (const section of sections.filter((s) => Object.keys(backup).includes(s.type))) {
|
||||
let refered = this.backupSectionRefered[section.type];
|
||||
for (const ref of refered) {
|
||||
await this.transactionManager.getRepository(ref).delete({});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -144,7 +169,7 @@ export default abstract class BackupHelper {
|
|||
})
|
||||
.catch((err) => {
|
||||
this.transactionManager = undefined;
|
||||
throw new InternalException("failed to restore backup - rolling back actions", err);
|
||||
throw new DatabaseActionException("BACKUP RESTORE", include.join(", "), err);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -11,14 +11,17 @@ export abstract class FileSystemHelper {
|
|||
}
|
||||
|
||||
static readFile(...filePath: string[]) {
|
||||
this.createFolder(...filePath);
|
||||
return readFileSync(this.formatPath(...filePath), "utf8");
|
||||
}
|
||||
|
||||
static readFileasBase64(...filePath: string[]) {
|
||||
this.createFolder(...filePath);
|
||||
return readFileSync(this.formatPath(...filePath), "base64");
|
||||
}
|
||||
|
||||
static readTemplateFile(filePath: string) {
|
||||
this.createFolder(filePath);
|
||||
return readFileSync(process.cwd() + filePath, "utf8");
|
||||
}
|
||||
|
||||
|
@ -28,6 +31,13 @@ export abstract class FileSystemHelper {
|
|||
writeFileSync(path, file);
|
||||
}
|
||||
|
||||
static deleteFile(...filePath: string[]) {
|
||||
const path = this.formatPath(...filePath);
|
||||
if (existsSync(path)) {
|
||||
unlinkSync(path);
|
||||
}
|
||||
}
|
||||
|
||||
static formatPath(...args: string[]) {
|
||||
return join(process.cwd(), "files", ...args);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue