Netbox: Backing up production data.

Created on 21 Sep 2016  路  5Comments  路  Source: netbox-community/netbox

Hey,

Just wanting to know what is recommend the best way to export/backup the data that has been placed into netbox system?

The wiki and docs doesn't really talk about it.

thanks.

Most helpful comment

In case it's helpful, this is the bash script I'm using to push database exports to Dropbox. It's smart enough to compare the checksum against the last backup and only save & push dumps that are changed from the last one.

I cloned Dropbox-Uploader to /opt/Dropbox-Uploader and followed his installation steps.

If you don't have zip installed, install it with sudo apt install zip

Then I created this file in the home directory of the postgres user (/var/lib/postgresql/backup.sh). Make it executable with chmod +x /var/lib/postgresql/backup.sh.

#!/bin/bash

time=$(date +%Y%m%d-%H-%M-%S)
mkdir /tmp/netbox-backups
pathtobackup=/tmp/netbox-backups/netbox-$time.sql
pathtochecksum=/tmp/netbox-backups/latest.md5

echo "Running Netbox backup at $time"

# backup the database
/usr/bin/pg_dump netbox > $pathtobackup

# generate a checksum of the backup
md5=($(md5sum $pathtobackup))

# compare against last checksum
lastmd5="`cat $pathtochecksum`"
if [ "$md5" = "$lastmd5" ]; then
        echo "Backups match, removing this instance"
        rm $pathtobackup
else
        echo "New checksum, zipping and pushing to Dropbox"
        /usr/bin/zip -9 $pathtobackup.zip $pathtobackup
        /opt/Dropbox-Uploader/dropbox_uploader.sh upload $pathtobackup.zip .
        echo "$md5" > $pathtochecksum
fi

To automate the backup, I switched over to the postgres user (su - postgres) and added this to its crontab (crontab -e). This example runs it every 30 minutes:

00,30 * * * * /var/lib/postgresql/backup.sh >> /tmp/netbox-backup/backup.log 2>&1

Per @mchlumsky, this script doesn't include the media or reports directories, so consider other ways to save those.

All 5 comments

Because the data is stored in PostgreSQL, the best thing to do is become familiar with standard PostgreSQL backup and restore procedures. As for exports, that could be implemented as a CSV download link from Netbox.

As @eronlloyd said, a simple PostgreSQL dump will do it. For example:

sudo -u postgres pg_dump netbox > netbox.sql

To restore the data to an empty database:

sudo -u postgres psql netbox < netbox.sql

Hi,
I am trying to migrate netbox but I am failing at the restoring part.
I ran the command "sudo -u postgres psql netbox < netbox.sql" for restoring and I am getting the following error. I think it has to do with some permissions but not sure. I am not much familiar with postgres db, docker etc. but somehow I was able to get netbox running. Can anyone please help?

Thanks.

After I run the command I am getting the following error

sudo -u postgres psql netbox < db_dump_2018-12-03.sql
could not change directory to "/root/netbox-docker"
SET
SET
ERROR: unrecognized configuration parameter "idle_in_transaction_session_timeout"
SET
SET
SET
SET
ERROR: unrecognized configuration parameter "row_security"
S

Just for completeness, you should probably also backup the media and report directories.

In case it's helpful, this is the bash script I'm using to push database exports to Dropbox. It's smart enough to compare the checksum against the last backup and only save & push dumps that are changed from the last one.

I cloned Dropbox-Uploader to /opt/Dropbox-Uploader and followed his installation steps.

If you don't have zip installed, install it with sudo apt install zip

Then I created this file in the home directory of the postgres user (/var/lib/postgresql/backup.sh). Make it executable with chmod +x /var/lib/postgresql/backup.sh.

#!/bin/bash

time=$(date +%Y%m%d-%H-%M-%S)
mkdir /tmp/netbox-backups
pathtobackup=/tmp/netbox-backups/netbox-$time.sql
pathtochecksum=/tmp/netbox-backups/latest.md5

echo "Running Netbox backup at $time"

# backup the database
/usr/bin/pg_dump netbox > $pathtobackup

# generate a checksum of the backup
md5=($(md5sum $pathtobackup))

# compare against last checksum
lastmd5="`cat $pathtochecksum`"
if [ "$md5" = "$lastmd5" ]; then
        echo "Backups match, removing this instance"
        rm $pathtobackup
else
        echo "New checksum, zipping and pushing to Dropbox"
        /usr/bin/zip -9 $pathtobackup.zip $pathtobackup
        /opt/Dropbox-Uploader/dropbox_uploader.sh upload $pathtobackup.zip .
        echo "$md5" > $pathtochecksum
fi

To automate the backup, I switched over to the postgres user (su - postgres) and added this to its crontab (crontab -e). This example runs it every 30 minutes:

00,30 * * * * /var/lib/postgresql/backup.sh >> /tmp/netbox-backup/backup.log 2>&1

Per @mchlumsky, this script doesn't include the media or reports directories, so consider other ways to save those.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

soer7022 picture soer7022  路  3Comments

markve-sa picture markve-sa  路  4Comments

cloos picture cloos  路  3Comments

hellerve picture hellerve  路  3Comments

VictorJ76 picture VictorJ76  路  3Comments