Free tech learning for everyone, from basics to pro

Network AutomationProgramming and Application SecurityIntermediate

Automated network configuration backups with Python and Git

In short: A simple automated backup collects each device's running configuration with Python and Netmiko, saves one file per device, and commits the folder to Git every day. Git keeps a full history and shows exactly what changed. Add failure alerts, keep the repository private, and protect or remove secrets inside configurations.

Why automate configuration backups?

Configuration backups are one of the best first automation projects. They are read-only, so they cannot break anything, and they solve a real problem: when a switch fails or a bad change is made, you need yesterday's configuration fast. Storing backups in Git adds a complete history, so you can see who changed what and when. This also supports compliance and drift detection.

An automated backup pipelineInventorylist ofdevicesConnectSSH or APICollectrunningconfigSaveone file perdeviceCommit toGithistory anddiffsAlerton failureor change
Figure 1. Git turns simple text backups into a full change history you can compare.

A simple backup script

import os
from datetime import date
from netmiko import ConnectHandler

devices = [
    {"device_type": "cisco_ios", "host": "10.0.0.11"},
    {"device_type": "cisco_ios", "host": "10.0.0.12"},
]
user = os.environ["NET_USER"]
password = os.environ["NET_PASS"]
os.makedirs("backups", exist_ok=True)

for dev in devices:
    try:
        with ConnectHandler(**dev, username=user, password=password) as conn:
            config = conn.send_command("show running-config")
        with open(f"backups/{dev['host']}.cfg", "w") as f:
            f.write(config)
        print("OK", dev["host"])
    except Exception as err:
        print("FAILED", dev["host"], err)

Credentials come from environment variables, not the script. In production, use a secrets manager (see handling credentials in automation) and read the device list from your inventory or source of truth.

Adding Git

cd backups
git init                       # first time only
git add .
git commit -m "Backup 2026-11-25"
git diff HEAD~1 -- 10.0.0.11.cfg   # what changed since the last backup?

Run the script and commit from a scheduler such as cron or a CI pipeline every night. Each commit is a snapshot, and git diff shows the exact lines added or removed. Some lines change on every run, such as timestamps or uptime counters; remove them before saving so diffs show only real changes.

Doing it safely

  • Secrets: configurations contain password hashes, SNMP communities and keys. Keep the repository private, restrict access and consider masking secrets before saving.
  • Failures: alert when a device cannot be backed up, otherwise you will discover the gap only when you need the file.
  • Least privilege: use an account that can only read configurations.
  • Test restores: occasionally restore a backup to a lab device to prove it works.

Tools such as Oxidized and RANCID do the same job off the shelf; building your own teaches the building blocks of automation.

Growing the project

Once basic backups work, the same pattern can collect other useful information, such as show version for software versions or show inventory for serial numbers, and save it alongside the configs. Running the job in parallel with a framework such as Nornir makes it fast even for hundreds of devices. The Git history can also feed reports, for example the number of changes per device each month.

Real-world example: who changed the ACL?

A fictional school's Wi-Fi guests suddenly cannot reach the internet one Monday. The network team checks the Git history of the nightly backups. A git diff between Friday and Saturday shows a single changed line in the core switch's guest ACL, made during a weekend vendor visit. The team restores the line in minutes, rather than spending hours comparing configs by hand. They then add a rule that any change to the guest ACL in the nightly backup sends an alert to the team chat.

Practice

Why are configuration backups a good first automation project?
They are read-only so they cannot break devices, and they solve a real, common problem.
What does storing backups in Git add?
A full change history, with diffs showing exactly what changed between backups and when.
Why should backup failures trigger an alert?
Otherwise missing backups are only discovered when you urgently need them.

Key takeaways

  • Automated backups are safe, useful first automation projects.
  • Collect running configs with Netmiko and save one file per device.
  • Commit to Git nightly to get history and exact diffs.
  • Protect secrets inside configs and keep the repository private.
  • Alert on failures and test restores regularly.
Tracks your learning on this device only. See your progress on the learning paths.
Verified author✓ Valid
Issued to
Mohamed Fayas
Role
Founder of FM TecHub
Credentials
CCNABTEC HND Network EngineeringBEng (Hons) Computer Networking & Cloud Security
Issuer
FM TecHub · fmtechub.com

He writes practical, plain-English guides on networking, security and cloud.

More about the author →

Original content written by Mohamed Fayas for FM TecHub. © 2026 FM TecHub. You're welcome to share a link to this page; please don't copy the text elsewhere without permission.

Email updates: coming soon

New notes, tools and security news from FM TecHub by email, explained simply. Free. We're not collecting email addresses yet; see our privacy policy.

PortServiceStatus
SMTP/25Email updatesopening soon

Meanwhile, stay connected: