A clean and reliable set of shell scripts to back up your NetBox PostgreSQL database.
(Works perfectly for any other PostgreSQL database as well.)
| File | Description |
|---|---|
| pg_backup.config | Main configuration file |
| pg_backup.sh | Basic PostgreSQL backup script |
| pg_backup_rotated.sh | Backup with rotation and automatic cleanup |
| README.md | Documentation (this file) |
- Automated PostgreSQL backups
- Optional weekly/monthly rotation
- Configurable retention periods
- Supports plain and custom-format dumps
- Safe permission model for secure operation
- Fully compatible with NetBox installations
- A running PostgreSQL server
- Scripts executed as the postgres user
- A writable backup directory
Below is a clean and optimized example configuration for NetBox installations:
##############################
## POSTGRESQL BACKUP CONFIG ##
##############################
# Script should always run as postgres
BACKUP_USER=postgres
# PostgreSQL host (usually local)
HOSTNAME=localhost
# DB user performing the backup
USERNAME=postgres
# Backup directory (created if missing)
BACKUP_DIR=/data/pgbackup/
# Empty for typical NetBox deployments
SCHEMA_ONLY_LIST=""
# Backup formats
ENABLE_CUSTOM_BACKUPS=no
ENABLE_PLAIN_BACKUPS=yes
# Include roles/permissions?
ENABLE_GLOBALS_BACKUPS=no
######### ROTATION SETTINGS #########
# Weekly backup day (1–7 = Mon–Sun)
DAY_OF_WEEK_TO_KEEP=7
# Retention periods
DAYS_TO_KEEP=14
WEEKS_TO_KEEP=8
#####################################USERNAME— PostgreSQL user (NetBox-specific user orpostgres)BACKUP_DIR— location where backups should be savedENABLE_GLOBALS_BACKUPS— set toyesto include roles & privileges
Ensure only the postgres user can access and execute the scripts:
chown postgres:postgres /data/pg_backup.config /data/pg_backup.sh /data/pg_backup_rotated.sh
chmod 700 /data/pg_backup.sh /data/pg_backup_rotated.sh
chmod 600 /data/pg_backup.config
mkdir -p /data/pgbackup
chown postgres:postgres /data/pgbackup
chmod 700 /data/pgbackupOpen the crontab of the postgres user:
crontab -u postgres -eAdd the following entry:
# NetBox/PostgreSQL backup with rotation
0 2 * * * /data/pg_backup_rotated.sh -c /data/pg_backup.config >> /var/log/pg_backup.log 2>&1🔸 Runs every day at 02:00
🔸 Uses the config file
🔸 Writes output to /var/log/pg_backup.log
If log files grow too large, configure logrotate.
sudo -u postgres /data/pg_backup_rotated.sh -c /data/pg_backup.configA successful run should create a directory in your BACKUP_DIR containing:
netbox.sql.gz
su postgres
psql -c 'drop database netbox'
psql -c 'create database netbox'
# For gzip-compressed dumps:
zcat /path/to/backup/netbox.sql.gz | psql netbox
# For uncompressed dumps:
psql netbox < netbox.sqlsu postgres
psql -c 'drop database if exists netbox_test'
psql -c 'create database netbox_test'
zcat /path/to/backup/netbox.sql.gz | psql netbox_testIf everything loads correctly and NetBox functions normally, your backup strategy is validated.
- These scripts are generic and work for any PostgreSQL database.
- For non‑NetBox environments, simply adjust DB name, user, and paths in
pg_backup.config.
Your backups are now safe, rotated, and easy to restore.
If you need further enhancements (systemd timers, email alerts, encryption, S3 uploads), just let me know!