# NetBox Backup Guide

### Introduction

The NetBox backup process provides regular secure backups. In summary the Ansible playbook does the following:

1. Manages retention based on your preferences, e.g. deletes local backups older than 14 days old.
2. Backs up and encrypts the media directory.
3. Backs up and encrypts the `netbox` PostgreSQL database.
4. Captures the current state of NetBox from the `/api/status` endpoint and saves the output in a JSON file that will be compared agains the target state in a restore event.
5. Optionally SFTP's the three backup files to a secure remote SFTP server.

<p class="callout warning">The NetBox process is **stopped** during the backup to ensure no changes are made to the database during the backup, and to ensure data integrity. This is typically for less than a minute, but take this into account if you anything connecting to the NetBox, e.g. automation tools. Netos Pod (Airflow) has a retry mechanism that handles broken connectivity to NetBox.</p>

[![image.png](https://docs.netos.io/uploads/images/gallery/2024-09/scaled-1680-/G4UKByPY2Fo9R5ZR-image.png)](https://docs.netos.io/uploads/images/gallery/2024-09/G4UKByPY2Fo9R5ZR-image.png)

#### Cron Scheduling

The cron scheduler in Semaphore can be configured to backup the database at regular intervals, for example, at 02:30 every day.

[![image.png](https://docs.netos.io/uploads/images/gallery/2024-09/scaled-1680-/zGK0JFzkP9aHRrjq-image.png)](https://docs.netos.io/uploads/images/gallery/2024-09/zGK0JFzkP9aHRrjq-image.png)

<p class="callout danger">Note that there is a [bug in Ansible Semaphore UI](https://github.com/semaphoreui/semaphore/issues/2294) that causes the same task to run many times. The solution is to toggle the "Show cron format' button and use UNIX formatting like [here](https://crontab.guru/examples.html).</p>

On the server, you can see the daily 02:00 encrypted backups in the red boxes. Below that, you can see an example of the JSON status that was captured when the backup was executed, so you know the exact version of NetBox and installed plugins for a restore.

[![image.png](https://docs.netos.io/uploads/images/gallery/2024-09/scaled-1680-/Gz50nAMkDRfUO5hY-image.png)](https://docs.netos.io/uploads/images/gallery/2024-09/Gz50nAMkDRfUO5hY-image.png)

#### Backup File Rotation

You can set the retention period for backup files stored in `/netos/backups/netbox` in the **NetBox Backup Settings** Semaphore environment.

[![image.png](https://docs.netos.io/uploads/images/gallery/2024-09/scaled-1680-/X44Bpmbd52wn9qkb-image.png)](https://docs.netos.io/uploads/images/gallery/2024-09/X44Bpmbd52wn9qkb-image.png)

<p class="callout warning">Ensure you use the exact values of `days` or `weeks`, i.e. no capitals.</p>

#### Remote SFTP

To enable remote SFTP, change the `No` value in the `SFTP_ENABLED` variable to `Yes`, and set the `SFTP_HOST/USER/PASS` values accordingly.

[![image.png](https://docs.netos.io/uploads/images/gallery/2024-09/scaled-1680-/nHyM898RhDS7qZBt-image.png)](https://docs.netos.io/uploads/images/gallery/2024-09/nHyM898RhDS7qZBt-image.png)

### Restoring from Backup

The [NetBox Restore Process](https://docs.netos.io/books/semaphore-netbox/page/netbox-restoration-guide) is one way to restore. If you want to manually decrypt the `enc` files on your local workstation, use the following commands.

`openssl enc -aes-256-cbc -d -in BACKUP_FILE.enc -out OUTPUT.sql.gz/.tar.gz`

You will need the password set in the NetBox Backup Settings Semaphore environment variable `ENCRYPTION_KEY`. For example, by running this command and entering the password, we decrypt the NetBox media directory:

`openssl enc -aes-256-cbc -d -in netbox_media_09_25_2024_02_00.tar.gz.enc -out netbox_media_09_25_2024_02_00.tar.gz`

### NetBox Development Snapshots

#### Use Backups for Labs

The Netos team use the tools outlined in these guides to deploy, restore, develop, and manage dozens of NetBox development environments, typically with many changes and deployments each day.

<p class="callout info">The original backup below used the standard timestamp format `mm_dd_yyyy_HH_MM`. However, we renamed it so we can snapshot different useful development environments pre-populated with data. We can then have many backups and images deployed to different NetBox instances on Semaphore to test and stage.</p>

[![image.png](https://docs.netos.io/uploads/images/gallery/2024-10/scaled-1680-/LQ5C5kPTyFrJHqgv-image.png)](https://docs.netos.io/uploads/images/gallery/2024-10/LQ5C5kPTyFrJHqgv-image.png)

As long as you only replace the timestamp in the filename with some text, you can set this in the `RESTORE_FILE_STAMP`, variable in the **NetBox Backup Settings** Semaphore environment. Here are some examples:

<table border="1" id="bkmrk-before-after-netbox_" style="border-collapse: collapse; width: 100%; height: 118.778px;"><colgroup><col style="width: 50.0562%;"></col><col style="width: 50.0562%;"></col></colgroup><tbody><tr style="height: 29.6946px;"><td style="height: 29.6946px; background-color: rgb(206, 212, 217);"><span style="color: rgb(35, 111, 161);">**Before**</span></td><td style="height: 29.6946px; background-color: rgb(206, 212, 217);"><span style="color: rgb(35, 111, 161);">**After**</span></td></tr><tr style="height: 29.6946px;"><td style="height: 29.6946px;">`netbox_db_<strong>10_01_2024_15_24</strong>.sql.gz.enc`</td><td style="height: 29.6946px;">`netbox_db_<strong>01_Data_Feeds_and_Excel</strong>.sql.gz.enc`</td></tr><tr style="height: 29.6946px;"><td style="height: 29.6946px;">`netbox_media_<strong>10_01_2024_15_24</strong>.tar.gz.enc`</td><td style="height: 29.6946px;">`netbox_media_<strong>01_Data_Feeds_and_Excel</strong>.tar.gz.enc`</td></tr><tr style="height: 29.6946px;"><td style="height: 29.6946px;">`netbox_status_<strong>10_01_2024_15_24</strong>.json`</td><td style="height: 29.6946px;">`netbox_status_<strong>01_Data_Feeds_and_Excel</strong>.json`</td></tr></tbody></table>

[![image.png](https://docs.netos.io/uploads/images/gallery/2024-10/scaled-1680-/YaPT4NMWfNvHUbsQ-image.png)](https://docs.netos.io/uploads/images/gallery/2024-10/YaPT4NMWfNvHUbsQ-image.png)

Additionally, during the backup process the generated timestamp is displayed, for example; `10_01_2024_18_35`. You could use this as per the screenshot above to quickly roll back.

[![image.png](https://docs.netos.io/uploads/images/gallery/2024-10/scaled-1680-/n3I4JpuHwaC9pTAm-image.png)](https://docs.netos.io/uploads/images/gallery/2024-10/n3I4JpuHwaC9pTAm-image.png)