I’m planning a small blog post series about Cisco configuration backup with Netbox and Gitlab.
My idea is to “control” which devices are backed up by Netbox with Device Type, Site, IPv4 address and Status (“Active”).
Based on above information the script should then login with SSH to the devices, send a couple of commands like banner, wr mem, copy run scp to start a file transfer to save the configuration on a SCP host temporary directory.
The configuration files are then checked in into a GitLab repository so it’s easy to track and compare configuration files.
To outline the idea, I made the drawing shown below.
To check in the configuration files the script uses a for-loop to go through the devices. The for-loop does some IPv4 cleanup (remove the subnet bits) and checks for the device role.
If there is in the /tmp folder a configuration file, the script authenticate against the Gitlab server.
If Gitlab does not have the configuration file, the script needs to call the create action.
If there is already a configuration file, then the script needs to use the update action.
The try/except block for the create and update action is creating its data in JSON. This JSON data is then committed to the Gitlab server project.
To handle multiple sites (in my case I moved from Germany to New Hampshire, after five years in the USA back to Germany and then within Germany), the Gitlab project uses different directories matching the Sites in Netbox.
As shown in the screenshot below, the configuration files are stored in the sites directory. Please note that if a configuration file did not exist, the comment would change to Initial commit of config file.
The nice thing and benefit of having Cisco configuration files on a Gitlab server is that I can now compare and track changes in the configuration below.