backup: automated SQLite snapshots (Verifactu requirement 4)¶
src/backup/ (backup_manager.h/cpp, static lib backup: Qt::Core, Qt::Sql, appsettings, sql_lite). Closes the record-retention requirement (Art. 8.2.c RD 1007/2023; 4-year LGT prescription period), see Verifactu. Tests: tests/test_backup_manager.cpp (backupsToPrune, backupWarning, real copies of a throwaway DB: rename after verifying, stale .part, same second, concurrent run, future last time, second folder), see unit suites.
Public API¶
| Member | Purpose |
|---|---|
performBackup() |
Snapshot, verify, rename, prune. Returns a Result |
setSecondFolder(folder) |
Optional second destination; set on the GUI thread before performBackup() |
recordSuccess(result) / recordFailure(result) |
Persist the outcome in AppSettings (GUI thread only) |
needsBackup(minIntervalSeconds = 24 h) |
True when the last backup is older, never ran, or is dated in the future |
pruneOldBackups() |
Applies the retention policy; returns files deleted |
backupsToPrune(fileNames, now) |
Pure retention decision, no I/O |
backupWarning(enabled, lastSuccessIso, lastError, now) / currentBackupWarning() |
Pure warning text / the same fed from AppSettings |
Result: success, skipped, skipReason (AlreadyRunning / SameSecond), backupPath, errorMessage (Spanish), bytesWritten, finishedAt, secondCopyPath, secondCopyError.
skippedmeans refused, not failed: a run is already going, or a copy with this second's name exists (that copy is left as it is). Callers show a notice (manual) or stay silent (automatic), and it is never stored as the last error. At start-up the data repairs of a new version run only after a copy of that start exists:SameSecondcounts,AlreadyRunningdoes not.
How a backup runs¶
- Guards. One backup at a time in the process (the automatic one runs on a worker thread, the manual one on the GUI thread). Stale
laideal_*.db.partfiles are removed. A copy that cannot be removed while pruning (locked by OneDrive, say) is logged and left for the next run. A run only removes files it created. - Snapshot. A dedicated connection (
laideal_backup_snapshot, busy timeout 30 s) runsVACUUM INTO '<final name>.part': a consistent point-in-time copy. The read lock it holds (rollback-journal mode) makes a ticket being saved wait, and the backup waits for a save in progress. - Verify. The
.partis reopened read-only onlaideal_backup_verify;PRAGMA integrity_checkmust returnok, then it is renamed to its final name. Any other verdict deletes it and reports failure, so a cut-off copy never matches the backup pattern. - Second folder (when set): the verified backup is also copied there as
.part+ rename and pruned with the same retention. A failed copy leaves the main backup successful andsecondCopyErrorsays why. A second folder equal to the backups folder is reported as such ("elija otra"). - Persist.
performBackup()never touchesAppSettings(it may run off the GUI thread); the caller passes the result torecordSuccess()(savesbackup.last_timefromfinishedAt) andrecordFailure(). - Prune on success.
Files and retention¶
<dbDir>/backups/laideal_yyyy-MM-dd_HHmmss.db (local time; created on demand next to dbPath(), not configurable). The timestamp sorts by time, which keeps the pruner trivial.
| Age | Kept |
|---|---|
| newest 3 | always, whatever their age |
| 0 - 30 days | every snapshot |
| 30 days - 4 years | the last snapshot of each (year, month) |
| over 4 years | none |
A wrong clock never decides what is deleted: nothing is pruned when "now" is before the newest copy or more than 45 days after it, or when the two newest copies are more than 45 days apart. Names that do not match the pattern are ignored. pruneOldBackups() lists the directory, calls backupsToPrune and removes the returned names.
Settings (~/.laideal_settings.json)¶
| Key | Default | Notes |
|---|---|---|
backup.enabled |
true |
gates needsBackup(); the manual backup and the pre-migration copy are independent |
backup.last_time |
empty | ISO-8601, set after each successful backup |
backup.last_version |
empty | the version whose first start backed up the DB before migrating it |
backup.second_folder |
empty | Configuración → Segunda carpeta de copias (USB stick, another drive) |
backup.last_error |
empty | time + reason of the last failed backup; cleared by the next success |
At every start MainWindow shows currentBackupWarning() in the result panel: backups switched off, or no correct backup for more than 3 days (with the last error).
Wiring (MainWindow)¶
mainwindowInitialSettings()createsm_backupManager. On the first start of a new version (backup.last_versiondiffers fromUpdater::currentVersion()) it takes a synchronous backup beforemigrateDatabase(). If that fails, the failure is shown andmigrateDatabase(db, false)runs only the schema steps (row-rewriting repairs wait for a start that has the copy). A failed migration step is reported in the result panel.- The constructor defers the daily snapshot (
needsBackup(), 3 s timer) to a worker thread, kept inm_backupThread;~MainWindowwaits for it. Success is silent (8 s status-bar message); failure is a red result-panel message. - Hacer copia de seguridad ahora runs
performBackup()underWaitCursorand shows path and size.
Out of scope¶
- Offsite replication. Point
dbPathat a synced folder or copy the directory. - Settings. The snapshot is the database only;
~/.laideal_settings.json(issuer data,aeat_chain_marker, the service key) is not copied and must be kept apart for a restore on another PC. - Encryption. Backups are plain SQLite;
VACUUM INTOkeeps whatever format the source has. - Restore UI. Copy the
.dboverdbPath()with the app closed.