netops.collect — Configuration Collection¶
Bulk configuration backup with diff tracking and git integration.
netops.collect.config¶
Single-device configuration collection.
CLI usage:
config ¶
Collect device configurations.
Usage: python -m netops.collect.config --inventory inventory.yaml --group core python -m netops.collect.config --host 10.0.0.1 --vendor cisco_ios --user admin
netops.collect.backup¶
Bulk configuration backup from an inventory file, with timestamped snapshots, diff tracking, and optional git commit.
CLI usage:
python -m netops.collect.backup --inventory inventory.yaml --output ./backups
python -m netops.collect.backup --inventory inventory.yaml --output ./backups --git
python -m netops.collect.backup --inventory inventory.yaml --output ./backups \
--workers 10
backup ¶
Bulk configuration backup with diff tracking.
Collects running configs from all inventory devices, saves them with timestamps to a per-device directory tree, and generates unified diffs against the previous backup so unexpected changes are immediately visible.
Optional git integration commits every changed file to a local repository so the full history is preserved.
Usage: python -m netops.collect.backup --inventory inv.yaml --output /var/backups/network/ python -m netops.collect.backup --inventory inv.yaml --output /var/backups/network/ --git python -m netops.collect.backup --inventory inv.yaml --output /var/backups/network/ --workers 10
Classes¶
Functions:¶
generate_diff ¶
Return a unified diff between old_path on disk and new_config text.
Returns:
| Type | Description |
|---|---|
str
|
Unified diff string. Returns an empty string when the configs are identical. |
save_backup ¶
Save one device's collected config to output_dir and compute a diff.
Directory layout::
<output_dir>/
<host>/
20240101-120000.cfg
20240102-130000.cfg
...
Returns:
| Type | Description |
|---|---|
dict
|
Summary dict with keys: host, success, saved_path, diff, changed, error |
git_init ¶
Initialise a git repository in output_dir if one does not exist yet.
git_commit ¶
Stage all changes in output_dir and create a git commit.
Returns:
| Type | Description |
|---|---|
bool
|
True on success (including the nothing to commit case). |
run_backup ¶
run_backup(params_list: list[ConnectionParams], output_dir: Path, *, workers: int = 5, git: bool = False, alert_on_change: bool = True, _timestamp: str | None = None) -> list[dict]
Collect configs from all devices and save them with diff tracking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params_list
|
list[ConnectionParams]
|
Connection parameters for each target device. |
required |
output_dir
|
Path
|
Root directory for the backup archive. |
required |
workers
|
int
|
Maximum number of concurrent collection threads. |
5
|
git
|
bool
|
When True, commit every changed file to a local git repository. |
False
|
alert_on_change
|
bool
|
When True, write change alerts to stderr. |
True
|
_timestamp
|
str | None
|
Override the timestamp string (intended for tests only). |
None
|
Returns:
| Type | Description |
|---|---|
list
|
List of per-device summary dicts (see :func: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If workers is less than 1. |
RuntimeError
|
If git is True and git initialisation or commit fails. |