# cesaremiliano79/backups architecture

> Architecture diagram of the GitHub repository cesaremiliano79/backups, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/cesaremiliano79/backups
- Repository: https://github.com/cesaremiliano79/backups

## Overview

1. **Overall shape**
   This is a small, single-purpose Python 3.10+ command-line utility. The repository has one executable script, `backup.py`, plus an external `config.json` that controls behavior. There is no service layer, database, or UI; the architecture is centered on filesystem traversal and copy/restore operations.

2. **Primary runtime boundary**
   The program runs on Windows and operates entirely through the local filesystem and Windows network shares. Its main external boundary is a destination folder that can be either a local path or a UNC share on another machine in the LAN. The backup target is expected to support read/write access over the network.

3. **Entry points / modes**
   There are two operating modes exposed through the console:
   - default execution: create backups
   - `restaurar`: restore the latest backup
   This makes the script behave like a small command dispatcher inside one process rather than separate tools.

4. **Configuration subsystem**
   `config.json` is the core control plane. It defines:
   - source directories to scan
   - destination path
   - allowed file extensions
   - maximum retained versions per source folder
   - log file location
   - user identity placeholder support via Windows environment variables
   The config is an explicit boundary between code and deployment environment, which is important for diagramming because it determines where data is read from and written to.

5. **Source discovery and path resolution**
   Source paths are not hardcoded to a specific username. Instead, the tool resolves `%USERNAME%` and `%USERPROFILE%` at runtime so it can adapt to the logged-in Windows account. This means the scanner operates on user-profile-relative folders like Desktop and Documents, regardless of machine or account name.

6. **File selection and filtering**
   The backup process walks each configured source tree recursively, but only includes files whose extensions appear in `extensiones_permitidas`. This acts as a filter stage before any copying occurs, reducing irrelevant data movement and narrowing the backup set to business-relevant documents.

7. **Change detection**
   The program uses MD5 hashes to compare file contents. Hashing is the key mechanism for detecting:
   - unchanged files during backup
   - files that should not be overwritten during restore unless they differ
   - file-level differences across versions
   This makes the system content-aware rather than relying only on timestamps.

8. **Backup storage model**
   Each run creates a dated versioned folder under the destination. The folder name combines the source folder name and a timestamp, creating a version history per source directory. This is a simple snapshot model: each backup run produces a new immutable version folder, and old versions are later pruned when the maximum count is exceeded.

9. **Retention and cleanup**
   `max_versiones` defines the retention policy per source folder. When the number of backups exceeds this limit, older versions are deleted. This is a dedicated housekeeping step in the backup flow and should be shown as a post-processing stage in the architecture diagram.

10. **Logging and observability**
   The tool writes an execution log to a configured text file. The log records:
   - backed up files and timestamps
   - ignored files due to extension rules
   - deleted old versions
   - unchanged files detected by MD5
   - total space used at completion
   This is the only persistent operational telemetry in the system and acts as the audit trail.

11. **Restore path**
   In restore mode, the program locates the most recent backup for each source folder and copies files back to their original locations. Restoration is selective: it only overwrites files whose hashes differ, reducing unnecessary churn and preserving unchanged local files.

12. **Infrastructure assumptions**
   The design assumes a Windows LAN environment, often with a shared folder on another computer or server. The README’s use of UNC paths and `cmdkey` indicates the destination may be protected by network credentials. There is no cloud dependency; the architecture is explicitly local-network-first.

13. **Packaging / deployment**
   The repo is intended to run either as a Python script or as a standalone Windows executable built with PyInstaller. In executable mode, `backup.exe` and `config.json` are deployed together, which makes configuration external to the binary and keeps the deployment portable.

14. **Key architectural boundaries to draw**
   For a diagram, the important boundaries are:
   - user/operator invoking CLI
   - `backup.py` orchestration layer
   - `config.json` configuration input
   - source directories on the local machine
   - destination backup folder or network share
   - log file output
   The main data flow is source scan → filter → hash compare → copy/version → log, with restore using the reverse path back to the original source locations.

Main entry points: [`backup.py`](https://github.com/cesaremiliano79/backups/blob/main/backup.py) (backup.py), [`config.json`](https://github.com/cesaremiliano79/backups/blob/main/config.json) (config.json)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime"]
  node_cli(("CLI user<br/>operator"))
  node_backup_py["backup.py<br/>orchestrator<br/>[backup.py]"]
  node_mode_dispatch{{"Mode dispatch<br/>command router"}}
  node_ext_filter{{"Extension filter<br/>selector"}}
  node_scanner["Scanner<br/>filesystem walk"]
  node_hasher{{"MD5 compare<br/>content diff"}}
  node_retention{{"Retention<br/>cleanup"}}
  node_restorer["Restore flow<br/>restore worker"]
end

subgraph group_storage["Storage"]
  node_source_dirs[("Source trees<br/>local folders")]
  node_versioner[("Version store<br/>snapshot writer")]
  node_dest_share[("Backup target<br/>local/UNC path")]
  node_logger["Execution log<br/>audit log"]
  node_log_file["Log file<br/>text file"]
  node_windows_fs[("Windows FS<br/>filesystem boundary")]
end

subgraph group_config["Config"]
  node_source_resolve{{"Path resolve<br/>env resolver"}}
  node_config_json["config.json<br/>settings<br/>[config.json]"]
end

node_cli -->|"run"| node_backup_py
node_backup_py -->|"choose mode"| node_mode_dispatch
node_mode_dispatch -->|"load config"| node_source_resolve
node_source_resolve -->|"read"| node_config_json
node_mode_dispatch -->|"backup"| node_scanner
node_scanner -->|"traverse"| node_source_dirs
node_scanner -->|"filter"| node_ext_filter
node_ext_filter -->|"check content"| node_hasher
node_hasher -->|"new or changed"| node_versioner
node_versioner -->|"store"| node_dest_share
node_versioner -->|"record"| node_logger
node_scanner -->|"ignore/track"| node_logger
node_versioner -->|"prune"| node_retention
node_retention -->|"delete old"| node_dest_share
node_mode_dispatch -->|"restore"| node_restorer
node_restorer -->|"read latest"| node_dest_share
node_restorer -->|"compare"| node_hasher
node_restorer -->|"restore to"| node_source_dirs
node_restorer -->|"record"| node_logger
node_logger -->|"write"| node_log_file
node_backup_py -->|"operate on"| node_windows_fs

click node_backup_py "https://github.com/cesaremiliano79/backups/blob/main/backup.py"
click node_config_json "https://github.com/cesaremiliano79/backups/blob/main/config.json"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli,node_backup_py,node_mode_dispatch,node_ext_filter,node_scanner,node_hasher,node_retention,node_restorer toneBlue
class node_source_dirs,node_versioner,node_dest_share,node_logger,node_log_file,node_windows_fs toneAmber
class node_source_resolve,node_config_json toneMint
```

## Components

### Runtime

CLI orchestration and execution flow inside backup.py

- **CLI user**: operator · Invokes backup or restore mode from the console
- [backup.py](https://github.com/cesaremiliano79/backups/blob/main/backup.py): orchestrator · Single entry script that coordinates backup and restore flows · `backup.py`
- **Mode dispatch**: command router · Selects default backup or restaurar restore mode
- **Extension filter**: selector · Keeps only files whose extensions are allowed by configuration
- **Scanner**: filesystem walk · Traverses each source tree to discover candidate files
- **MD5 compare**: content diff · Hashes files to detect unchanged content and restore differences
- **Retention**: cleanup · Deletes older backup versions beyond max_versiones
- **Restore flow**: restore worker · Finds latest backup and copies differing files back to original locations

### Storage

Local source paths, backup destination, and log output

- **Source trees**: local folders · Configured user-profile-relative folders scanned recursively
- **Version store**: snapshot writer · Creates dated per-source backup folders under the destination
- **Backup target**: local/UNC path · Destination folder or Windows network share used for versioned backups
- **Execution log**: audit log · Writes backed up, ignored, unchanged, deleted, and total-space records
- **Log file**: text file · Configured persistent output for operational audit trail
- **Windows FS**: filesystem boundary · Local filesystem and UNC network-share access boundary

### Config

External configuration and environment resolution

- **Path resolve**: env resolver · Expands %USERNAME% and %USERPROFILE% into concrete Windows paths
- [config.json](https://github.com/cesaremiliano79/backups/blob/main/config.json): settings · External control plane for sources, destination, extensions, retention, and logging · `config.json`

## Connections

- CLI user → backup.py: run · User starts the script from the console
- backup.py → Mode dispatch: choose mode · Dispatches between backup and restaurar modes
- Mode dispatch → Path resolve: load config · Reads runtime settings and expands environment placeholders
- Path resolve → config.json: read · Configuration is the external control plane
- Mode dispatch → Scanner: backup · Default mode scans source trees for files to copy
- Scanner → Source trees: traverse · Recursively walks configured source folders
- Scanner → Extension filter: filter · Drops files whose extensions are not permitted
- Extension filter → MD5 compare: check content · Allowed files are compared by MD5
- MD5 compare → Version store: new or changed · Changed content is written into a new version folder
- Version store → Backup target: store · Creates dated snapshot folders under the destination
- Version store → Execution log: record · Logs copied files and run summary
- Scanner → Execution log: ignore/track · Logs skipped files and unchanged files
- Version store → Retention: prune · Enforces per-source maximum retained versions
- Retention → Backup target: delete old · Removes outdated snapshot folders from the backup target
- Mode dispatch → Restore flow: restore · Restore mode reverses the flow toward original paths
- Restore flow → Backup target: read latest · Loads the most recent backup version for each source
- Restore flow → MD5 compare: compare · Only overwrites files whose hashes differ
- Restore flow → Source trees: restore to · Copies files back to their original source locations
- Restore flow → Execution log: record · Logs restore actions and outcomes
- Execution log → Log file: write · Persists the audit trail to the configured text file
- backup.py → Windows FS: operate on · All file movement happens through the local filesystem or UNC share
