mirror of
https://github.com/R0m1k3/LogiFlow.git
synced 2026-10-11 17:27:31 +02:00
Add system for managing and scheduling data backups
Introduce a new backup management feature with automated scheduling, manual backups, and a dedicated interface for viewing backup status and details. This includes integrating `node-cron` for scheduling, updating the database schema to store backup information, and creating new routes and components for the backup manager. Replit-Commit-Author: Agent Replit-Commit-Session-Id: 7c468936-2a88-4686-ac0a-84921a45222d Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/1957c339-2757-4d1f-8e92-e9f71a1ce58e/7c468936-2a88-4686-ac0a-84921a45222d/9xfbVBU
This commit is contained in:
1 parent
bfc90053f0
commit
fbeb36c04a
9 files changed
+131
-58
No files matched your search
@@ -138,7 +138,7 @@ export default function Layout({ children }: LayoutProps) {
|
|||||||
<div className="flex items-center gap-2">
|
<div className="flex items-center gap-2">
|
||||||
<div
|
<div
|
||||||
className="w-3 h-3"
|
className="w-3 h-3"
|
||||||
style={{ backgroundColor: store.color }}
|
style={{ backgroundColor: store.color || '#gray-400' }}
|
||||||
/>
|
/>
|
||||||
<span>{store.name}</span>
|
<span>{store.name}</span>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import DatabaseDebug from "@/pages/DatabaseDebug";
|
|||||||
import CustomerOrders from "@/pages/CustomerOrders";
|
import CustomerOrders from "@/pages/CustomerOrders";
|
||||||
|
|
||||||
import DlcPage from "@/pages/DlcPage";
|
import DlcPage from "@/pages/DlcPage";
|
||||||
|
import BackupManager from "@/pages/BackupManager";
|
||||||
import Tasks from "@/pages/Tasks";
|
import Tasks from "@/pages/Tasks";
|
||||||
import TasksSimplified from "@/pages/TasksSimplified";
|
import TasksSimplified from "@/pages/TasksSimplified";
|
||||||
import Layout from "@/components/Layout";
|
import Layout from "@/components/Layout";
|
||||||
@@ -76,6 +77,7 @@ function RouterProduction() {
|
|||||||
<Route path="/publicities" component={Publicities} />
|
<Route path="/publicities" component={Publicities} />
|
||||||
<Route path="/customer-orders" component={CustomerOrders} />
|
<Route path="/customer-orders" component={CustomerOrders} />
|
||||||
<Route path="/dlc" component={DlcPage} />
|
<Route path="/dlc" component={DlcPage} />
|
||||||
|
<Route path="/backup" component={BackupManager} />
|
||||||
<Route path="/tasks" component={Tasks} />
|
<Route path="/tasks" component={Tasks} />
|
||||||
<Route path="/nocodb-config" component={NocoDBConfig} />
|
<Route path="/nocodb-config" component={NocoDBConfig} />
|
||||||
<Route path="/database-debug" component={DatabaseDebug} />
|
<Route path="/database-debug" component={DatabaseDebug} />
|
||||||
|
|||||||
@@ -146,6 +146,12 @@ export default function Sidebar() {
|
|||||||
icon: UserCog,
|
icon: UserCog,
|
||||||
roles: ["admin"]
|
roles: ["admin"]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
path: "/backup",
|
||||||
|
label: "Sauvegardes",
|
||||||
|
icon: Database,
|
||||||
|
roles: ["admin"]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
path: "/nocodb-config",
|
path: "/nocodb-config",
|
||||||
label: "Configuration NocoDB",
|
label: "Configuration NocoDB",
|
||||||
|
|||||||
@@ -24,9 +24,13 @@ import { fr } from "date-fns/locale";
|
|||||||
interface BackupFile {
|
interface BackupFile {
|
||||||
id: string;
|
id: string;
|
||||||
filename: string;
|
filename: string;
|
||||||
|
description: string | null;
|
||||||
size: number;
|
size: number;
|
||||||
createdAt: string;
|
createdAt: string;
|
||||||
type: 'manual' | 'automatic';
|
createdBy: string;
|
||||||
|
tablesCount: number;
|
||||||
|
status: string;
|
||||||
|
backupType: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function BackupManager() {
|
export default function BackupManager() {
|
||||||
@@ -118,6 +122,19 @@ export default function BackupManager() {
|
|||||||
: <Badge variant="outline">Manuelle</Badge>;
|
: <Badge variant="outline">Manuelle</Badge>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const getStatusBadge = (status: string) => {
|
||||||
|
switch (status) {
|
||||||
|
case 'completed':
|
||||||
|
return <Badge className="bg-green-100 text-green-800">Terminé</Badge>;
|
||||||
|
case 'creating':
|
||||||
|
return <Badge className="bg-blue-100 text-blue-800">En cours</Badge>;
|
||||||
|
case 'failed':
|
||||||
|
return <Badge variant="destructive">Échec</Badge>;
|
||||||
|
default:
|
||||||
|
return <Badge variant="outline">{status}</Badge>;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
if (!canManageBackups) {
|
if (!canManageBackups) {
|
||||||
return (
|
return (
|
||||||
<div className="flex-1 flex items-center justify-center">
|
<div className="flex-1 flex items-center justify-center">
|
||||||
@@ -254,7 +271,7 @@ export default function BackupManager() {
|
|||||||
>
|
>
|
||||||
<div className="flex items-center space-x-4">
|
<div className="flex items-center space-x-4">
|
||||||
<div className="flex items-center space-x-2">
|
<div className="flex items-center space-x-2">
|
||||||
{getTypeIcon(backup.type)}
|
{getTypeIcon(backup.backupType)}
|
||||||
<FileText className="w-5 h-5 text-gray-400" />
|
<FileText className="w-5 h-5 text-gray-400" />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -262,12 +279,17 @@ export default function BackupManager() {
|
|||||||
<div className="font-medium text-gray-900">
|
<div className="font-medium text-gray-900">
|
||||||
{backup.filename}
|
{backup.filename}
|
||||||
</div>
|
</div>
|
||||||
<div className="text-sm text-gray-500 flex items-center space-x-4">
|
<div className="text-sm text-gray-500">
|
||||||
|
{backup.description || 'Sauvegarde de base de données'}
|
||||||
|
</div>
|
||||||
|
<div className="text-sm text-gray-500 flex items-center space-x-4 mt-1">
|
||||||
<span>
|
<span>
|
||||||
{format(new Date(backup.createdAt), 'dd/MM/yyyy à HH:mm', { locale: fr })}
|
{format(new Date(backup.createdAt), 'dd/MM/yyyy à HH:mm', { locale: fr })}
|
||||||
</span>
|
</span>
|
||||||
<span>{formatFileSize(backup.size)}</span>
|
<span>{formatFileSize(backup.size)}</span>
|
||||||
{getTypeBadge(backup.type)}
|
<span>{backup.tablesCount} tables</span>
|
||||||
|
{getTypeBadge(backup.backupType)}
|
||||||
|
{getStatusBadge(backup.status)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Generated
+10
@@ -66,6 +66,7 @@
|
|||||||
"memorystore": "^1.6.7",
|
"memorystore": "^1.6.7",
|
||||||
"nanoid": "^5.1.5",
|
"nanoid": "^5.1.5",
|
||||||
"next-themes": "^0.4.6",
|
"next-themes": "^0.4.6",
|
||||||
|
"node-cron": "^4.2.1",
|
||||||
"openid-client": "^6.6.2",
|
"openid-client": "^6.6.2",
|
||||||
"passport": "^0.7.0",
|
"passport": "^0.7.0",
|
||||||
"passport-local": "^1.0.0",
|
"passport-local": "^1.0.0",
|
||||||
@@ -6919,6 +6920,15 @@
|
|||||||
"node": "^18 || ^20 || >= 21"
|
"node": "^18 || ^20 || >= 21"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/node-cron": {
|
||||||
|
"version": "4.2.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/node-cron/-/node-cron-4.2.1.tgz",
|
||||||
|
"integrity": "sha512-lgimEHPE/QDgFlywTd8yTR61ptugX3Qer29efeyWw2rv259HtGBNn1vZVmp8lB9uo9wC0t/AT4iGqXxia+CJFg==",
|
||||||
|
"license": "ISC",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=6.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/node-gyp-build": {
|
"node_modules/node-gyp-build": {
|
||||||
"version": "4.8.4",
|
"version": "4.8.4",
|
||||||
"resolved": "https://registry.npmjs.org/node-gyp-build/-/node-gyp-build-4.8.4.tgz",
|
"resolved": "https://registry.npmjs.org/node-gyp-build/-/node-gyp-build-4.8.4.tgz",
|
||||||
|
|||||||
@@ -68,6 +68,7 @@
|
|||||||
"memorystore": "^1.6.7",
|
"memorystore": "^1.6.7",
|
||||||
"nanoid": "^5.1.5",
|
"nanoid": "^5.1.5",
|
||||||
"next-themes": "^0.4.6",
|
"next-themes": "^0.4.6",
|
||||||
|
"node-cron": "^4.2.1",
|
||||||
"openid-client": "^6.6.2",
|
"openid-client": "^6.6.2",
|
||||||
"passport": "^0.7.0",
|
"passport": "^0.7.0",
|
||||||
"passport-local": "^1.0.0",
|
"passport-local": "^1.0.0",
|
||||||
|
|||||||
+62
-52
@@ -3,17 +3,14 @@ import { promisify } from 'util';
|
|||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import cron from 'node-cron';
|
import cron from 'node-cron';
|
||||||
|
import { nanoid } from 'nanoid';
|
||||||
|
import { eq, desc } from "drizzle-orm";
|
||||||
|
import { db } from "./db";
|
||||||
|
import { databaseBackups } from "@shared/schema";
|
||||||
|
import type { DatabaseBackup, InsertDatabaseBackup } from "@shared/schema";
|
||||||
|
|
||||||
const execAsync = promisify(exec);
|
const execAsync = promisify(exec);
|
||||||
|
|
||||||
export interface BackupFile {
|
|
||||||
id: string;
|
|
||||||
filename: string;
|
|
||||||
size: number;
|
|
||||||
createdAt: Date;
|
|
||||||
type: 'manual' | 'automatic';
|
|
||||||
}
|
|
||||||
|
|
||||||
export class BackupService {
|
export class BackupService {
|
||||||
private backupDir: string;
|
private backupDir: string;
|
||||||
private maxBackups = 10;
|
private maxBackups = 10;
|
||||||
@@ -48,12 +45,25 @@ export class BackupService {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async createBackup(type: 'manual' | 'automatic' = 'manual'): Promise<BackupFile> {
|
async createBackup(type: 'manual' | 'automatic' = 'manual', createdBy: string = 'system'): Promise<DatabaseBackup> {
|
||||||
try {
|
try {
|
||||||
|
const id = nanoid();
|
||||||
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
|
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
|
||||||
const filename = `backup_${type}_${timestamp}.sql`;
|
const filename = `backup_${type}_${timestamp}.sql`;
|
||||||
const filepath = path.join(this.backupDir, filename);
|
const filepath = path.join(this.backupDir, filename);
|
||||||
|
|
||||||
|
// Create database record first with "creating" status
|
||||||
|
const [backupRecord] = await db.insert(databaseBackups).values({
|
||||||
|
id,
|
||||||
|
filename,
|
||||||
|
description: `${type === 'manual' ? 'Manuel' : 'Automatique'} backup du ${new Date().toLocaleDateString('fr-FR')}`,
|
||||||
|
size: 0,
|
||||||
|
createdBy,
|
||||||
|
tablesCount: 0,
|
||||||
|
status: 'creating',
|
||||||
|
backupType: type,
|
||||||
|
}).returning();
|
||||||
|
|
||||||
const dbConfig = this.getDatabaseConfig();
|
const dbConfig = this.getDatabaseConfig();
|
||||||
|
|
||||||
// Set environment variable for password to avoid prompt
|
// Set environment variable for password to avoid prompt
|
||||||
@@ -69,56 +79,41 @@ export class BackupService {
|
|||||||
|
|
||||||
await execAsync(command, { env });
|
await execAsync(command, { env });
|
||||||
|
|
||||||
// Get file stats
|
// Get file stats and count tables
|
||||||
const stats = fs.statSync(filepath);
|
const stats = fs.statSync(filepath);
|
||||||
|
const sqlContent = fs.readFileSync(filepath, 'utf8');
|
||||||
|
const tablesCount = (sqlContent.match(/CREATE TABLE/g) || []).length;
|
||||||
|
|
||||||
const backupFile: BackupFile = {
|
// Update database record with completion details
|
||||||
id: timestamp,
|
const [updatedBackup] = await db.update(databaseBackups)
|
||||||
filename,
|
.set({
|
||||||
size: stats.size,
|
size: stats.size,
|
||||||
createdAt: new Date(),
|
tablesCount,
|
||||||
type,
|
status: 'completed',
|
||||||
};
|
})
|
||||||
|
.where(eq(databaseBackups.id, id))
|
||||||
|
.returning();
|
||||||
|
|
||||||
console.log(`✅ ${type} backup created: ${filename} (${this.formatFileSize(stats.size)})`);
|
console.log(`✅ ${type} backup created: ${filename} (${this.formatFileSize(stats.size)}, ${tablesCount} tables)`);
|
||||||
|
|
||||||
// Clean old backups
|
// Clean old backups
|
||||||
await this.cleanOldBackups();
|
await this.cleanOldBackups();
|
||||||
|
|
||||||
return backupFile;
|
return updatedBackup;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('❌ Backup failed:', error);
|
console.error('❌ Backup failed:', error);
|
||||||
throw new Error(`Backup failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
throw new Error(`Backup failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async getBackupList(): Promise<BackupFile[]> {
|
async getBackupList(): Promise<DatabaseBackup[]> {
|
||||||
try {
|
try {
|
||||||
const files = fs.readdirSync(this.backupDir);
|
const backups = await db.select()
|
||||||
const backupFiles: BackupFile[] = [];
|
.from(databaseBackups)
|
||||||
|
.orderBy(desc(databaseBackups.createdAt))
|
||||||
|
.limit(this.maxBackups);
|
||||||
|
|
||||||
for (const filename of files) {
|
return backups;
|
||||||
if (filename.endsWith('.sql')) {
|
|
||||||
const filepath = path.join(this.backupDir, filename);
|
|
||||||
const stats = fs.statSync(filepath);
|
|
||||||
|
|
||||||
// Extract type and timestamp from filename
|
|
||||||
const parts = filename.replace('.sql', '').split('_');
|
|
||||||
const type = parts[1] as 'manual' | 'automatic';
|
|
||||||
const timestamp = parts.slice(2).join('_');
|
|
||||||
|
|
||||||
backupFiles.push({
|
|
||||||
id: timestamp,
|
|
||||||
filename,
|
|
||||||
size: stats.size,
|
|
||||||
createdAt: stats.birthtime,
|
|
||||||
type,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sort by creation date (newest first)
|
|
||||||
return backupFiles.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('❌ Failed to list backups:', error);
|
console.error('❌ Failed to list backups:', error);
|
||||||
return [];
|
return [];
|
||||||
@@ -127,13 +122,26 @@ export class BackupService {
|
|||||||
|
|
||||||
async deleteBackup(filename: string): Promise<void> {
|
async deleteBackup(filename: string): Promise<void> {
|
||||||
try {
|
try {
|
||||||
const filepath = path.join(this.backupDir, filename);
|
// Find backup record
|
||||||
|
const [backup] = await db.select()
|
||||||
if (!fs.existsSync(filepath)) {
|
.from(databaseBackups)
|
||||||
throw new Error('Backup file not found');
|
.where(eq(databaseBackups.filename, filename))
|
||||||
|
.limit(1);
|
||||||
|
|
||||||
|
if (!backup) {
|
||||||
|
throw new Error('Backup record not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.unlinkSync(filepath);
|
// Delete physical file
|
||||||
|
const filepath = path.join(this.backupDir, filename);
|
||||||
|
if (fs.existsSync(filepath)) {
|
||||||
|
fs.unlinkSync(filepath);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete database record
|
||||||
|
await db.delete(databaseBackups)
|
||||||
|
.where(eq(databaseBackups.filename, filename));
|
||||||
|
|
||||||
console.log(`🗑️ Backup deleted: ${filename}`);
|
console.log(`🗑️ Backup deleted: ${filename}`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('❌ Failed to delete backup:', error);
|
console.error('❌ Failed to delete backup:', error);
|
||||||
@@ -153,10 +161,12 @@ export class BackupService {
|
|||||||
|
|
||||||
private async cleanOldBackups(): Promise<void> {
|
private async cleanOldBackups(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
const backups = await this.getBackupList();
|
const allBackups = await db.select()
|
||||||
|
.from(databaseBackups)
|
||||||
|
.orderBy(desc(databaseBackups.createdAt));
|
||||||
|
|
||||||
if (backups.length > this.maxBackups) {
|
if (allBackups.length > this.maxBackups) {
|
||||||
const toDelete = backups.slice(this.maxBackups);
|
const toDelete = allBackups.slice(this.maxBackups);
|
||||||
|
|
||||||
for (const backup of toDelete) {
|
for (const backup of toDelete) {
|
||||||
await this.deleteBackup(backup.filename);
|
await this.deleteBackup(backup.filename);
|
||||||
|
|||||||
+6
-1
@@ -2352,7 +2352,12 @@ RÉSUMÉ DU SCAN
|
|||||||
|
|
||||||
app.post('/api/backups', isAuthenticated, requireAdmin, async (req: any, res) => {
|
app.post('/api/backups', isAuthenticated, requireAdmin, async (req: any, res) => {
|
||||||
try {
|
try {
|
||||||
const backup = await backupService.createBackup('manual');
|
const user = await storage.getUserWithGroups(req.user.claims ? req.user.claims.sub : req.user.id);
|
||||||
|
if (!user) {
|
||||||
|
return res.status(404).json({ message: "User not found" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const backup = await backupService.createBackup('manual', user.id);
|
||||||
res.json(backup);
|
res.json(backup);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error creating backup:", error);
|
console.error("Error creating backup:", error);
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import {
|
|||||||
date,
|
date,
|
||||||
decimal,
|
decimal,
|
||||||
primaryKey,
|
primaryKey,
|
||||||
|
bigint,
|
||||||
} from "drizzle-orm/pg-core";
|
} from "drizzle-orm/pg-core";
|
||||||
import { relations } from "drizzle-orm";
|
import { relations } from "drizzle-orm";
|
||||||
import { createInsertSchema } from "drizzle-zod";
|
import { createInsertSchema } from "drizzle-zod";
|
||||||
@@ -512,8 +513,24 @@ export const insertInvoiceVerificationCacheSchema = createInsertSchema(invoiceVe
|
|||||||
updatedAt: true,
|
updatedAt: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const databaseBackups = pgTable("database_backups", {
|
||||||
|
id: varchar("id", { length: 255 }).primaryKey(),
|
||||||
|
filename: varchar("filename", { length: 255 }).notNull(),
|
||||||
|
description: text("description"),
|
||||||
|
size: bigint("size", { mode: "number" }).default(0),
|
||||||
|
createdAt: timestamp("created_at").defaultNow(),
|
||||||
|
createdBy: varchar("created_by", { length: 255 }).notNull(),
|
||||||
|
tablesCount: integer("tables_count").default(0),
|
||||||
|
status: varchar("status", { length: 50 }).default("creating"),
|
||||||
|
backupType: varchar("backup_type", { length: 10 }).default("manual"),
|
||||||
|
});
|
||||||
|
|
||||||
|
export const insertDatabaseBackupSchema = createInsertSchema(databaseBackups);
|
||||||
|
|
||||||
export type InsertNocodbConfig = z.infer<typeof insertNocodbConfigSchema>;
|
export type InsertNocodbConfig = z.infer<typeof insertNocodbConfigSchema>;
|
||||||
export type InsertInvoiceVerificationCache = z.infer<typeof insertInvoiceVerificationCacheSchema>;
|
export type InsertInvoiceVerificationCache = z.infer<typeof insertInvoiceVerificationCacheSchema>;
|
||||||
|
export type DatabaseBackup = typeof databaseBackups.$inferSelect;
|
||||||
|
export type InsertDatabaseBackup = z.infer<typeof insertDatabaseBackupSchema>;
|
||||||
|
|
||||||
// Types
|
// Types
|
||||||
export type User = typeof users.$inferSelect;
|
export type User = typeof users.$inferSelect;
|
||||||
|
|||||||
Reference in new issue
Block a user