- Sauvegarde automatique : Le système crée automatiquement une sauvegarde toutes les 12 heures (00:00 et 12:00) et conserve les 10 plus récentes.
+ Sauvegarde automatique : Le système crée automatiquement une sauvegarde SQL toutes les 12 heures (00:00 et 12:00) et conserve les 10 plus récentes.
- Format des fichiers : Les sauvegardes sont au format JSON et contiennent toutes les données de l'application.
+ Format des fichiers : Les sauvegardes sont au format SQL PostgreSQL (.sql) et contiennent la structure complète et les données de la base.
- Contenu inclus : Utilisateurs, magasins, ventes, et toutes les relations entre ces données.
+ Contenu inclus : Schéma de base de données, utilisateurs, magasins, ventes, et toutes les relations entre ces données.
- Sécurité : Les mots de passe sont hachés et ne peuvent pas être récupérés depuis une sauvegarde.
+ Avantages SQL : Sauvegarde native PostgreSQL compatible avec pg_dump/pg_restore, structure et données préservées.
+
+
+ Restauration : Remplace complètement la base de données existante par le contenu du fichier SQL importé.
Recommandation : Les sauvegardes automatiques couvrent la plupart des besoins, mais vous pouvez créer des sauvegardes manuelles avant des modifications importantes.
diff --git a/replit.md b/replit.md
index c549cbc..ea9b4c2 100644
--- a/replit.md
+++ b/replit.md
@@ -315,6 +315,16 @@ Preferred communication style: Simple, everyday language.
- ✅ **Database Schema**: Sales and sale_products tables properly configured for multi-product sales
- ✅ **Export Compatibility**: All export functions (Excel, PDF, CSV) support multiple products per sale
+### SQL Backup System Implementation (January 19, 2025)
+- ✅ **SQL Backup Format**: Migrated from JSON to native PostgreSQL SQL format using pg_dump
+- ✅ **Database Restoration**: Implemented SQL import capability using psql for complete database restoration
+- ✅ **Automatic SQL Backups**: Modified scheduler to create SQL dumps instead of JSON exports
+- ✅ **Manual Export/Import**: Updated administration interface to handle SQL files (.sql)
+- ✅ **Buffer Optimization**: Increased maxBuffer to 50MB for handling large databases with photo data
+- ✅ **Production Compatibility**: Native PostgreSQL backup format ensures full compatibility with production systems
+- ✅ **Data Integrity**: SQL format preserves all database structure, constraints, and relationships
+- ✅ **User Interface**: Updated backup management UI with SQL-specific messaging and file extensions
+
### Docker Configuration & Cleanup (January 9, 2025)
- ✅ Complete Docker setup optimized for external PostgreSQL database
- ✅ Docker Compose configuration with RegisFlow application only
diff --git a/server/backup-scheduler.ts b/server/backup-scheduler.ts
index 72a9d07..564f65e 100644
--- a/server/backup-scheduler.ts
+++ b/server/backup-scheduler.ts
@@ -2,10 +2,22 @@ import cron from 'node-cron';
import fs from 'fs';
import path from 'path';
import { storage } from './storage';
+import { exec } from 'child_process';
+import { promisify } from 'util';
+
+const execAsync = promisify(exec);
const BACKUP_DIR = path.join(process.cwd(), 'backups');
const MAX_BACKUPS = 10;
+// Database connection info from environment variables
+const DB_HOST = process.env.PGHOST || 'localhost';
+const DB_PORT = process.env.PGPORT || '5432';
+const DB_NAME = process.env.PGDATABASE || 'postgres';
+const DB_USER = process.env.PGUSER || 'postgres';
+const DB_PASSWORD = process.env.PGPASSWORD || '';
+const DATABASE_URL = process.env.DATABASE_URL;
+
// Ensure backup directory exists
if (!fs.existsSync(BACKUP_DIR)) {
fs.mkdirSync(BACKUP_DIR, { recursive: true });
@@ -13,66 +25,117 @@ if (!fs.existsSync(BACKUP_DIR)) {
export async function createAutomaticBackup() {
try {
- console.log('Creating automatic backup...');
+ console.log('Creating automatic SQL backup...');
- // Get all data from the database
- const users = await storage.getAllUsers();
- const stores = await storage.getAllStores();
-
- // Get all sales from all stores
- const allSales = [];
- for (const store of stores) {
- const storeSales = await storage.getSalesByStore(store.id);
- allSales.push(...storeSales);
- }
-
- const backupData = {
- timestamp: new Date().toISOString(),
- version: "1.0",
- type: "automatic",
- data: {
- users: users.map(user => ({
- ...user,
- // Don't include the password hash in backup for security
- password: undefined
- })),
- stores,
- sales: allSales
- },
- metadata: {
- totalUsers: users.length,
- totalStores: stores.length,
- totalSales: allSales.length,
- exportedBy: 'system-auto-backup'
- }
- };
-
// Create filename with timestamp
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
- const filename = `auto-backup-${timestamp}.json`;
+ const filename = `auto-backup-${timestamp}.sql`;
const filepath = path.join(BACKUP_DIR, filename);
- // Write backup to file
- fs.writeFileSync(filepath, JSON.stringify(backupData, null, 2));
+ // Create SQL dump using pg_dump
+ await createSQLBackup(filepath);
- console.log(`✅ Automatic backup created: ${filename}`);
- console.log(`📊 Backup stats: ${users.length} users, ${stores.length} stores, ${allSales.length} sales`);
+ // Get stats for logging
+ const users = await storage.getAllUsers();
+ const stores = await storage.getAllStores();
+ let totalSales = 0;
+ for (const store of stores) {
+ const storeSales = await storage.getSalesByStore(store.id);
+ totalSales += storeSales.length;
+ }
+
+ console.log(`✅ Automatic SQL backup created: ${filename}`);
+ console.log(`📊 Backup stats: ${users.length} users, ${stores.length} stores, ${totalSales} sales`);
// Clean up old backups
await cleanupOldBackups();
- return { success: true, filename, stats: backupData.metadata };
+ return {
+ success: true,
+ filename,
+ stats: {
+ totalUsers: users.length,
+ totalStores: stores.length,
+ totalSales: totalSales,
+ exportedBy: 'system-auto-backup'
+ }
+ };
} catch (error) {
- console.error('❌ Failed to create automatic backup:', error);
+ console.error('❌ Failed to create automatic SQL backup:', error);
return { success: false, error: error instanceof Error ? error.message : 'Unknown error' };
}
}
+export async function createSQLBackup(filepath: string): Promise {
+ try {
+ // Use pg_dump to create SQL backup
+ let command: string;
+
+ if (DATABASE_URL) {
+ // Use DATABASE_URL if available
+ command = `pg_dump "${DATABASE_URL}" --no-owner --no-privileges --clean --if-exists`;
+ } else {
+ // Use individual connection parameters
+ command = `PGPASSWORD="${DB_PASSWORD}" pg_dump -h "${DB_HOST}" -p "${DB_PORT}" -U "${DB_USER}" -d "${DB_NAME}" --no-owner --no-privileges --clean --if-exists`;
+ }
+
+ const { stdout, stderr } = await execAsync(command, {
+ maxBuffer: 50 * 1024 * 1024, // 50MB buffer for large databases
+ });
+
+ if (stderr && !stderr.includes('NOTICE')) {
+ console.warn('pg_dump warnings:', stderr);
+ }
+
+ // Write SQL dump to file
+ fs.writeFileSync(filepath, stdout);
+
+ console.log(`SQL backup created: ${path.basename(filepath)}`);
+ } catch (error) {
+ console.error('Failed to create SQL backup:', error);
+ throw new Error(`SQL backup failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
+ }
+}
+
+export async function restoreSQLBackup(filepath: string): Promise {
+ try {
+ console.log(`Restoring SQL backup from: ${path.basename(filepath)}`);
+
+ // Read SQL file
+ const sqlContent = fs.readFileSync(filepath, 'utf8');
+
+ // Use psql to restore the backup
+ let command: string;
+
+ if (DATABASE_URL) {
+ // Use DATABASE_URL if available
+ command = `psql "${DATABASE_URL}"`;
+ } else {
+ // Use individual connection parameters
+ command = `PGPASSWORD="${DB_PASSWORD}" psql -h "${DB_HOST}" -p "${DB_PORT}" -U "${DB_USER}" -d "${DB_NAME}"`;
+ }
+
+ const { stderr } = await execAsync(command, {
+ input: sqlContent,
+ maxBuffer: 50 * 1024 * 1024, // 50MB buffer for large databases
+ });
+
+ if (stderr && !stderr.includes('NOTICE')) {
+ console.warn('psql warnings:', stderr);
+ }
+
+ console.log(`✅ SQL backup restored successfully from: ${path.basename(filepath)}`);
+ } catch (error) {
+ console.error('Failed to restore SQL backup:', error);
+ throw new Error(`SQL restore failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
+ }
+}
+
async function cleanupOldBackups() {
try {
- // Get all backup files
+ // Get all backup files (now SQL files)
const files = fs.readdirSync(BACKUP_DIR)
- .filter(file => file.startsWith('auto-backup-') && file.endsWith('.json'))
+ .filter(file => file.startsWith('auto-backup-') && file.endsWith('.sql'))
.map(file => ({
name: file,
path: path.join(BACKUP_DIR, file),
@@ -99,7 +162,7 @@ async function cleanupOldBackups() {
export function getBackupStats() {
try {
const files = fs.readdirSync(BACKUP_DIR)
- .filter(file => file.startsWith('auto-backup-') && file.endsWith('.json'));
+ .filter(file => file.startsWith('auto-backup-') && file.endsWith('.sql'));
return {
totalBackups: files.length,
diff --git a/server/routes.ts b/server/routes.ts
index c17e71f..a8fe864 100644
--- a/server/routes.ts
+++ b/server/routes.ts
@@ -5,7 +5,9 @@ import session from "express-session";
import connectPg from "connect-pg-simple";
import { loginSchema, insertUserSchema, insertStoreSchema, insertSaleSchema } from "@shared/schema";
import { z } from "zod";
-import { createAutomaticBackup, getBackupStats } from "./backup-scheduler";
+import fs from "fs";
+import path from "path";
+import { createAutomaticBackup, getBackupStats, createSQLBackup, restoreSQLBackup } from "./backup-scheduler";
import { executePurgeManually, getPurgeStats } from "./data-purge";
declare module 'express-session' {
@@ -464,139 +466,65 @@ export async function registerRoutes(app: Express): Promise {
}
});
- // Backup routes
+ // SQL Backup routes
app.get('/api/admin/backup/export', requireRole(['admin']), async (req, res) => {
try {
- // Get all data from the database
- const users = await storage.getAllUsers();
- const stores = await storage.getAllStores();
+ // Create a temporary SQL backup
+ const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
+ const filename = `manual-backup-${timestamp}.sql`;
+ const filepath = path.join(process.cwd(), 'backups', filename);
+
+ await createSQLBackup(filepath);
+
+ // Send the SQL file
+ res.setHeader('Content-Type', 'application/sql');
+ res.setHeader('Content-Disposition', `attachment; filename="${filename}"`);
+
+ const sqlContent = fs.readFileSync(filepath, 'utf8');
+ res.send(sqlContent);
+
+ // Optionally delete the temporary file after sending
+ // fs.unlinkSync(filepath);
- // Get all sales from all stores
- const allSales = [];
- for (const store of stores) {
- const storeSales = await storage.getSalesByStore(store.id);
- allSales.push(...storeSales);
- }
-
- const backupData = {
- timestamp: new Date().toISOString(),
- version: "1.0",
- data: {
- users: users.map(user => ({
- ...user,
- // Don't include the password hash in backup for security
- password: undefined
- })),
- stores,
- sales: allSales
- },
- metadata: {
- totalUsers: users.length,
- totalStores: stores.length,
- totalSales: allSales.length,
- exportedBy: req.user.username
- }
- };
-
- res.json(backupData);
} catch (error) {
- console.error('Export backup error:', error);
- res.status(500).json({ error: "Failed to export backup" });
+ console.error('Export SQL backup error:', error);
+ res.status(500).json({ error: "Failed to export SQL backup" });
}
});
+ // SQL Backup import route (receives SQL file content)
app.post('/api/admin/backup/import', requireRole(['admin']), async (req, res) => {
try {
- const backupData = req.body;
+ const { sqlContent } = req.body;
- // Validate backup structure
- if (!backupData.data || !backupData.data.users || !backupData.data.stores || !backupData.data.sales) {
- return res.status(400).json({ error: "Invalid backup file format" });
+ if (!sqlContent || typeof sqlContent !== 'string') {
+ return res.status(400).json({ error: "SQL content is required" });
}
- // Clear existing data (in reverse order due to foreign keys)
- // First delete all sales
- const existingSales = [];
- const existingStores = await storage.getAllStores();
- for (const store of existingStores) {
- const storeSales = await storage.getSalesByStore(store.id);
- existingSales.push(...storeSales);
- }
+ // Create a temporary file for the SQL content
+ const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
+ const filename = `restore-${timestamp}.sql`;
+ const filepath = path.join(process.cwd(), 'backups', filename);
+
+ // Write SQL content to temporary file
+ fs.writeFileSync(filepath, sqlContent);
+
+ // Restore the backup
+ await restoreSQLBackup(filepath);
+
+ // Clean up temporary file
+ fs.unlinkSync(filepath);
- for (const sale of existingSales) {
- await storage.deleteSale(sale.id);
- }
-
- // Then delete users (except admin to avoid locking out)
- const existingUsers = await storage.getAllUsers();
- for (const user of existingUsers) {
- if (user.role !== 'administrator' || user.username !== 'admin') {
- await storage.deleteUser(user.id);
- }
- }
-
- // Then delete stores
- for (const store of existingStores) {
- await storage.deleteStore(store.id);
- }
-
- // Import new data
- // First import stores
- const storeIdMapping = new Map();
- for (const storeData of backupData.data.stores) {
- const { id, ...storeWithoutId } = storeData;
- const newStore = await storage.createStore(storeWithoutId);
- storeIdMapping.set(id, newStore.id);
- }
-
- // Then import users (excluding admin and users with passwords)
- const userIdMapping = new Map();
- for (const userData of backupData.data.users) {
- if (userData.username === 'admin') continue; // Skip admin user
-
- const { id, password, ...userWithoutIdAndPassword } = userData;
-
- // Map store ID if it exists
- if (userData.storeId && storeIdMapping.has(userData.storeId)) {
- userWithoutIdAndPassword.storeId = storeIdMapping.get(userData.storeId);
- }
-
- // Set a default password since we can't restore the original
- const userWithPassword = {
- ...userWithoutIdAndPassword,
- password: 'restored123' // Users will need to change this
- };
-
- const newUser = await storage.createUser(userWithPassword);
- userIdMapping.set(id, newUser.id);
- }
-
- // Finally import sales
- for (const saleData of backupData.data.sales) {
- const { id, ...saleWithoutId } = saleData;
-
- // Map store and user IDs
- if (saleData.storeId && storeIdMapping.has(saleData.storeId)) {
- saleWithoutId.storeId = storeIdMapping.get(saleData.storeId);
- }
- if (saleData.userId && userIdMapping.has(saleData.userId)) {
- saleWithoutId.userId = userIdMapping.get(saleData.userId);
- }
-
- await storage.createSale(saleWithoutId);
- }
-
res.json({
- message: "Backup imported successfully",
- imported: {
- stores: backupData.data.stores.length,
- users: backupData.data.users.filter((u: any) => u.username !== 'admin').length,
- sales: backupData.data.sales.length
- }
+ message: "SQL backup restored successfully",
+ timestamp: new Date().toISOString()
});
} catch (error) {
- console.error('Import backup error:', error);
- res.status(500).json({ error: "Failed to import backup", details: error instanceof Error ? error.message : 'Unknown error' });
+ console.error('Import SQL backup error:', error);
+ res.status(500).json({
+ error: "Failed to restore SQL backup",
+ details: error instanceof Error ? error.message : 'Unknown error'
+ });
}
});