# Manage & Operate NetBox

Backup, restore, and perform health checks and maintenance on your NetBox instance

# 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)

# NetBox Restoration Guide

### Introduction

The restore process works by taking a backup generated by the [NetBox Backup](https://docs.netos.io/books/semaphore-netbox/page/netbox-backup-guide), decrypting it, and restoring it. Check the [NetBox Development Snapshots](https://docs.netos.io/books/semaphore-netbox/page/netbox-backup-guide#bkmrk-netbox-development-s) section in the backup guide for an approach on using this feature for dev/test.

<p class="callout warning">The `ENCRYPTION_KEY` value in the **NetBox Backup Settings** Semaphore environment is used in both the backup and restore process. Be sure to keep track as if you change it on the target system, all future backups will use the new key.</p>

#### Configure the Restore

The NetBox backup process will create three timestamped (`mm_dd_yyyy_HH_MM`) files like these. The `enc` files are encrypted using the `ENCRYPTION_KEY` variable.

```
netbox_db_03_18_2024_01_00.sql.gz.enc
netbox_media_03_18_2024_01_00.tar.gz.enc
netbox_status_03_18_2024_01_00.json
```

<p class="callout info">You can manually decrypt the files using the command openssl `enc -aes-256-cbc -d -in BACKUP_FILE.enc -out OUTPUT.sql.gz.tar.gz`</p>

The restore file prefix is set in the NetBox Backup Settings environment and is the timestamp used for all three files. Set this to match the timestamp of the files that you place in the `/netos/netbox/backups` directory.

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

#### Plugin Version Validation Logic

By default, when the backup runs it will check the **NetBox version** and **plugin versions** of the target system by comparing the output of the `/api/status` endpoint collected from the backup with the version of the target system.

This is an example of the relevant JSON:

```json
    "netbox-version": "3.7.8",
    "plugins": {
        "netos": "1.4.6",
        "netos_fabric": "1.3.3",
        "netos_model_builder": "1.2.37",
        "netos_reporting": "1.3.16"
    }
```

In the event of a mismatch, you will see a warning like this that details the discrepancies between the backup and target system.

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

To disable this check, change `BYPASS_PLUGIN_CHECK` to `Yes`. Disabling may be acceptable when restoring to a slightly different target system, for example, where the Netos Insights plugin has increased from minor version `1.2.24` to `1.2.26`.

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

When `BYPASS_PLUGIN_CHECK` is set to `Yes` the restore process will look like this:

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

<p class="callout info">Any [NetBox Open Source Plugins](https://docs.netos.io/books/semaphore-netbox/page/deploy-netbox-community-plugins) or [Netos Enterprise NetBox Plugins](https://docs.netos.io/books/semaphore-netbox/page/deploy-netos-enterprise-plugins) installed using Semaphore will be named according to their version and saved in `/netos/working-dir/netbox-plugins` and `/netos/working-dir/netos-plugins`. The `/netos/working-dir` directory is [backed up each day](https://docs.netos.io/books/semaphore-netbox/page/netbox-backup-guide), assuming you enable the schedule in Semaphore.</p>

# NetBox Health Checks & Maintenance

### Introduction

In order to keep your NetBox instance operating smoothly you can run housekeeping and maintenance scripts.

#### NetBox Housekeeping Script

NetBox includes a `housekeeping` management command that should be run nightly. This command handles:

- Clearing expired authentication sessions from the database
- Deleting changelog records older than the configured [retention time](https://github.com/netbox-community/netbox/blob/develop/docs/configuration/miscellaneous.md#changelog_retention)
- Deleting job result records older than the configured [retention time](https://github.com/netbox-community/netbox/blob/develop/docs/configuration/miscellaneous.md#job_retention)
- Check for new NetBox releases (if [`RELEASE_CHECK_URL`](https://github.com/netbox-community/netbox/blob/develop/docs/configuration/miscellaneous.md#release_check_url) is set)

<p class="callout info">The official documentation is here [netbox/docs/administration/housekeeping.md at develop · netbox-community/netbox (github.com)](https://github.com/netbox-community/netbox/blob/develop/docs/administration/housekeeping.md)</p>

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

As per the NetBox developer suggestions, you can schedule the housekeeping script to run each day by enabling the schedule job in Semaphore.

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

#### NetBox Health Check

The NetBox Health Check script runs some useful commands to quickly capture the state of your NetBox instance, for example, checking processes are running, checking system logs, etc.

```bash
systemctl is-active netbox
systemctl is-active netbox-rq
python3 /opt/netbox/netbox/manage.py check
grep -i 'error' /var/log/nginx/netbox.error.log || true
grep -i 'warning' /var/log/nginx/netbox.error.log || true
journalctl -u netbox --since '24 hours ago' -n 250 | grep -i 'error' || true
journalctl -u netbox-rq --since '24 hours ago' -n 250 | grep -i 'error' || true

```

When you run the script, you'll see output like this:

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

In this scenario we can quickly see there is an error when starting NetBox, which pointed us in the right direction for troubleshooting a syntax issue in `settings.py` (using the commands detailed above).

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