Network Device Backup
├── Inventory
│ ├── defaults.py
│ ├── groups.py
│ ├── hosts.py
│ └── staticHosts.py
├── Functions
│ ├── __init__.py
│ ├── decryption.py
│ ├── deviceBackupLog.txt
│ ├── Hosts Encryption Key.txt
│ └── obtainDynamicHosts.py
├── config.yaml
├── deviceBackup.py
├── requirements.txt
├── README.md
└── .gitignore
This "Device Backup" script is built using Nornir for the automation framework and Netbox as the inventory system.
deviceBackup.py
should be called from a cronjob on a Linux server that's executed on a regular interval.
* 5 * * * cd /scripts/Backup && python /scripts/Backup/deviceBackup.py > runtimeLog.txt 2>&1
- Identifies all hosts (statically defined, Netbox)
- SSH's into each device, performs a
show run
- Outputs the result to a text file on a remote directory
- Performs a
git commit
to version control the directory.
- Calls upon
Functions/obtainDynamicHosts.py
, which obtains hosts from Netbox and formats the hosts into a YAML-formatted file for the rest of the script to rely upon:- Reads in
Inventory/staticHosts.yaml
. These are statically defined hosts that require special handling. These hosts and their associated properties take the highest priority. Reasons to include a host in the static file may include:- Non-standard credentials (local auth)
- Non-standard device sub-type (Switch (IOS/IOS-XE vs. Nexus Switch (NXOS))
- Calls to Netbox based on what's defined in
obtainDynamicHosts.py
. You, as the user, will have to define this operation as it pertains to your Netbox environment. Have a read through the Pynetbox readthedocs for some guidance.
- Reads in
- Initializes the Nornir framework. This reads in the recently created
Inventory/hosts.yaml
file containing the hosts identified in the previous step. - Decrypts any provided credentials with
Functions/decryption.py
andFunctions/Hosts Encryption Key.txt
. - Log into each device via SSH using the Netmiko framework.
- My devices don't have HTTP enabled on them as of yet, so NAPALM and other API-based automation frameworks are out of the picture.
- Netmiko is an automation framework that utilizes SSH for its actions.
- Calls a
show run
as appropriate for each type of device. - Outputs the result to a text file at
/mnt/configs/
, which is an SMB/CIFS-mountpoint for the enterprise file store where my configs live. - Performs a
git commit
on all relevant directories to version control the backups.
- The
config.yaml
file highlights three pieces of information:- The location of the
hosts.yaml
file, which is the file that would contain all host information after the dynamic inventory is completed. groups.yaml
contains group-specific variables. For example, each firewall uses thecisco_asa
platform instead of thecisco_ios
platform, and as such this is listed here to allow Nornir to differentiate the proper context for logging in via SSH, translatingshow run
to the appropriate syntax for the OS, etc.defaults.yaml
contains default variables that apply in all cases when no more specific variables are listed in the other two files. Listed here are encrypted TACACS+/LDAP credentials forsvcnetops
used to log into the vast majority of the network appliances, as well as the the defaultcisco_ios
platform and SSH port 22 for Netmiko to use when SSH-ing into the devices.
- The location of the
- The running-configurations get stored to a git-tracked directory at the mount-point at
/mnt/configs/
.