Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolve #56 #61

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
.vscode
node_modules
dumps/
2 changes: 2 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
"axios": "^1.4.0",
"bcryptjs": "^2.4.3",
"cors": "^2.8.5",
"cron": "^3.1.7",
"cross-spawn": "^7.0.3",
"date-fns": "^2.30.0",
"dotenv": "^16.0.3",
"fastify": "^4.17.0",
Expand Down
19 changes: 19 additions & 0 deletions backend/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import * as Fastify from "fastify";
import cors from "cors";
import fastifyMiddie from "@fastify/middie";
import fastifyMultipart from "@fastify/multipart";
import { CronJob } from "cron";
import { closeDatabase, connectDatabase } from "@/database";
import APIHandler from "@/api";
import StaticHandler from "@/static";
import UploadHandler from "@/upload";
import PageHandler from "@/page";
import { dumpDatabase, removeOldDumps } from "@/utils/database";
//import NotFoundHandler from "@/notFound";

async function init() {
Expand Down Expand Up @@ -50,3 +52,20 @@ process.on("SIGINT", async function() {
await closeDatabase();
process.exit(1);
});


const dumpDatabaseJob = new CronJob(
"0 0 0 * * *",
Kotsudes marked this conversation as resolved.
Show resolved Hide resolved
dumpDatabase,
null,
true,
"Europe/Paris"
);
Kotsudes marked this conversation as resolved.
Show resolved Hide resolved

const deleteOldDumpJob = new CronJob(
"0 30 0 * * *",
removeOldDumps,
null,
true,
"Europe/Paris"
);
43 changes: 43 additions & 0 deletions backend/src/utils/database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import * as fs from "node:fs";
Kotsudes marked this conversation as resolved.
Show resolved Hide resolved
import * as cs from "cross-spawn";

const BACKUP_RETENTION_DAYS = 30;

export async function dumpDatabase() {
const dumpDate = new Date().toLocaleDateString("fr-fr",{ day: "2-digit", month: "2-digit", year: "numeric" }).replaceAll("/", "-");
const folderName = `../dumps/${ dumpDate }`;
try {
if (!fs.existsSync("../dumps")) {
fs.mkdirSync("../dumps");
}
if (!fs.existsSync(folderName)) {
fs.mkdirSync(folderName);
}
}
catch (err) {
console.error(err);
}
Kotsudes marked this conversation as resolved.
Show resolved Hide resolved

const child = cs.spawn("mongodump", ["--db", "sgnw", "--out", `../dumps/${ dumpDate }`], { stdio: "inherit" });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ça serait peut être pas mal de mettre le path vers mongodump dans le .env, je suis pas sûr qu'on veuille forcément le mettre dans le Path 🤔

}

export async function removeOldDumps() {
const currentDate = new Date();
const folderName = "../dumps/";

fs.readdir(folderName,{ encoding: "utf-8" }, (err, files) => {
if (err)
console.log(err);
else {
files.forEach(file => {
const [day, month, year] = file.split("-").map(Number);
const dumpDate = new Date(year, month - 1, day);
const Difference_In_Days = Math.floor((currentDate.getTime() - dumpDate.getTime()) / (1000 * 3600 * 24));

if (Difference_In_Days > BACKUP_RETENTION_DAYS) {
fs.rmdirSync(`${ folderName }/${ file }`, { recursive: true });
}
});
}
});
}
2 changes: 1 addition & 1 deletion backend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@
"exclude": [
"node_modules"
]
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pourquoi y'a cette modif 🤔 ? L'eslint devrait garder les lignes vide à la fin normalement, non ?

Copy link
Contributor Author

@Kotsudes Kotsudes Sep 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

J'ai enfin compris. J'ai ajouté ces lignes à mon .vscode dans website

"eslint.workingDirectories": [
        {
            "mode": "auto"
        }
    ]

25 changes: 25 additions & 0 deletions pnpm-lock.yaml

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