Overview
Export complete system data as JSON for backup purposes or import data from a backup file. Includes all tables and preserves relationships.
Admin Only: Backup operations require administrator privileges.
Authentication
Requires valid JWT token with admin role.
Authorization: Bearer <token>
Export Backup
GET /api/backup/export
Exports all system data as a JSON file.
curl -X GET http://localhost:3001/api/backup/export \
-H "Authorization: Bearer YOUR_TOKEN" \
-o backup.json
Response Structure
ISO timestamp when backup was created
System version (e.g., “2.0”)
Server identifier (e.g., “aptiv-scrap-api”)
Object containing all table data
areas - Production areas
catnp - Parts catalog
fallas - Failure modes
pesaje - Scrap records
usuarios - Users (passwords remain hashed)
turnos - Shifts
cadenas - Production chains
lineas - Production lines
categorias_scrap - Scrap categories
unidades - Units of measurement
roles - User roles
tolerancias - Tolerance limits
auditoria - Audit log
Response Example
{
"exportDate": "2024-03-15T18:30:00.000Z",
"version": "2.0",
"server": "aptiv-scrap-api",
"data": {
"areas": [
{
"id": 1,
"AREA": "Ensamble",
"CADENA": "GNT1",
"activo": 1
}
],
"pesaje": [
{
"ID": 1234,
"AREA": "Ensamble",
"NP": "12345678",
"COSTO": 45.75,
"FECHA_REGISTRO": "2024-03-15T14:30:00.000Z"
}
],
"usuarios": [
{
"id": 1,
"nombre": "Juan",
"gafete": "admin",
"contra": "$2a$10$...",
"activo": 1
}
]
}
}
Import Backup
POST /api/backup/import
Imports data from a backup file. Uses INSERT IGNORE to skip duplicate records.
Partial Import: The import process skips duplicate records and sensitive tables (sessions). It does not delete existing data.
curl -X POST http://localhost:3001/api/backup/import \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d @backup.json
Request Body
Object containing table data from backup export
Response
Indicates if import completed successfully
Object showing number of records imported per table
Success Response Example
{
"success": true,
"imported": {
"areas": 15,
"catnp": 234,
"fallas": 45,
"pesaje": 1523,
"usuarios": 12,
"turnos": 3,
"cadenas": 4,
"lineas": 18,
"categorias_scrap": 8,
"unidades": 5,
"roles": 4,
"tolerancias": 6,
"auditoria": 2341
}
}
Database Statistics
GET /api/backup/stats
Get database statistics including record counts and storage size.
curl -X GET http://localhost:3001/api/backup/stats \
-H "Authorization: Bearer YOUR_TOKEN"
Response Example
{
"tables": {
"areas": 15,
"catnp": 234,
"fallas": 45,
"pesaje": 15234,
"usuarios": 25,
"turnos": 3,
"cadenas": 4,
"lineas": 18,
"categorias_scrap": 8,
"unidades": 5,
"roles": 4,
"tolerancias": 6,
"auditoria": 45231,
"sesiones": 12
},
"database_size_mb": "145.67",
"timestamp": "2024-03-15T18:30:00.000Z"
}
Backup UI Component
const BackupManager: React.FC = () => {
const [stats, setStats] = useState<any>(null);
const [importing, setImporting] = useState(false);
useEffect(() => {
fetchStats();
}, []);
const fetchStats = async () => {
const response = await fetch('http://localhost:3001/api/backup/stats', {
headers: { 'Authorization': `Bearer ${getToken()}` }
});
setStats(await response.json());
};
const handleExport = async () => {
try {
await exportBackup();
alert('Backup exported successfully');
} catch (error) {
alert(`Export failed: ${error.message}`);
}
};
const handleImport = async (file: File) => {
setImporting(true);
try {
const text = await file.text();
const backupData = JSON.parse(text);
const result = await importBackup(backupData);
alert(`Import complete! Imported ${Object.values(result.imported).reduce((a, b) => a + b, 0)} records`);
fetchStats(); // Refresh stats
} catch (error) {
alert(`Import failed: ${error.message}`);
} finally {
setImporting(false);
}
};
return (
<div>
<h2>Backup & Restore</h2>
{stats && (
<div className="stats">
<p>Total Records: {Object.values(stats.tables).reduce((a: any, b: any) => a + b, 0)}</p>
<p>Database Size: {stats.database_size_mb} MB</p>
</div>
)}
<button onClick={handleExport}>Export Backup</button>
<input
type="file"
accept=".json"
onChange={e => e.target.files?.[0] && handleImport(e.target.files[0])}
disabled={importing}
/>
{importing && <p>Importing... please wait</p>}
</div>
);
};
Best Practices
Regular Backups: Schedule automatic daily backups and store them securely off-site.
Test Restores: Periodically test backup restoration in a non-production environment to ensure data integrity.
Version Compatibility: When importing, verify the backup version matches your system version.