Managing and backing up configurations for multiple network devices can be a complex and time-consuming task. While there are many commercial-grade backup solutions available in the market, I wanted to create a lightweight, fast, and customizable tool that could meet my specific requirements. That’s how NetBak was born.
NetBak is a Python-based tool that automates the backup process for network devices, DNS configurations, and NetBox databases. It’s designed to be triggered from a pipeline, making it easy to integrate with CI/CD workflows.
Table of contents
Open Table of contents
Why NetBak?
I had a few key requirements in mind when developing NetBak:
- Lightweight and Fast: I needed a tool that could quickly backup multiple devices without consuming too many resources.
- Customizable: I wanted the flexibility to tailor the backup process to my specific needs.
- Git Integration: Leveraging Git for version control and storing backups securely was a must.
- CI/CD Compatibility: The ability to trigger backups from a pipeline was crucial for automation.
While commercial solutions offer a wide range of features, they often come with a steep learning curve, expensive licenses and can be resource-intensive. NetBak, on the other hand, is a lightweight tool that focuses on the core functionality of backing up network devices efficiently.
How NetBak Works
NetBak simplifies the backup process by automating the following steps:
- Reading configuration details from YAML files.
- Connecting to network devices via SSH and retrieving running configurations.
- Backing up DNS configurations by fetching specific files and the entire DNS directory.
- Performing a backup of the NetBox database.
- Uploading all the backups to a designated GitLab repository.
By leveraging Python and various libraries, NetBak streamlines the entire backup process, saving time and effort.
Key Features of NetBak
Triggering Backups with GitLab CI
One of the key features of NetBak is its compatibility with GitLab CI. By using GitLab CI pipeline schedules, you can trigger backups at regular intervals, such as every 2 hours.
Here’s an example of how you can set up a GitLab CI pipeline to run NetBak:
backup:
script:
- netbak --config-file switches.yaml --variables-file variables.yaml
only:
- schedules
In this example, the backup job runs the netbak.py script with the specified configuration and variables files. The only keyword ensures that the job is triggered only by scheduled pipelines.
By leveraging GitLab CI, you can automate the backup process and ensure that your network device configurations are regularly backed up without manual intervention.
Customization and Flexibility
NetBak provides flexibility through its configuration files. You can customize various aspects of the backup process by modifying the variables.yaml file. For example, you can enable or disable specific backup tasks, set backup paths, and specify the maximum number of concurrent threads.
This customization allows you to tailor NetBak to your specific requirements and optimize its performance based on your environment.
sample variables.yaml
file
# gitlab variables
gitlab_url: https://gitlab.com
gitlab_token: glpat-CbXXXXXXXXXX
gitlab_repo: netbak/backup
# DNS variables
dns_host: 192.168.1.10
dns_username: dnssuser
dns_password: password
dns_backup_path: "DNS"
specific_dns_file_path: "/var/named/db.johnlam.io"
# Netbox variables
netbox_host: 192.168.1.11
netbox_username: netboxuser
netbox_password: netboxpassword
netbox_db_password: netboxdbpassword
netbox_backup_path: "netbox"
netbox_db_backup_file: "netbox-backup.sql"
# Switch variables
switch_backup_path: "switches"
# SSH variables
ssh_timeout: 60
ssh_read_timeout: 70
max_workers: 8
# Enable/Disable backup
enable_switch_backup: true
enable_dns_backup: true
enable_netbox_backup: true
Sample switches.yaml
file
switches:
- custom_name: "SWITCH-1"
hostname: "192.168.1.21"
vendor: "dell_z9100"
username: "admin"
password: "password"
- custom_name: "SWITCH-2"
hostname: "192.168.1.22"
vendor: "alcatet_sros"
username: "admin"
password: "password"
Usage
netbak -c switches.yaml -f variables.yaml
Example Output:
Executing command on 192.168.1.21: show running-config
Backup for 192.168.1.21 (SWITCH-1) uploaded to GitLab.
Executing command on 192.168.1.22: admin display-config
Backup for 192.168.1.21 (SWITCH-2) uploaded to GitLab.
Backing up DNS configuration...
DNS configuration backup completed.
Backing up specific DNS file...
Specific DNS file backup completed.
Backing up NetBox database...
NetBox database backup completed.
Backup process completed successfully!
Conclusion
Developing NetBak was a fun side project for me. It allowed me to explore Python programming, work with various libraries, and delve into the world of network automation. While it may not have all the bells and whistles of commercial solutions, it serves its purpose well and provides a lightweight and efficient way to backup network devices.