Backing up production data. #444

Closed
opened 2025-12-29 16:22:14 +01:00 by adam · 5 comments
Owner

Originally created by @lachlanjholmes on GitHub (Sep 21, 2016).

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.

Originally created by @lachlanjholmes on GitHub (Sep 21, 2016). 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.
adam closed this issue 2025-12-29 16:22:14 +01:00
Author
Owner

@eronlloyd commented on GitHub (Sep 21, 2016):

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.

@eronlloyd commented on GitHub (Sep 21, 2016): Because the data is stored in PostgreSQL, the best thing to do is become familiar with standard [PostgreSQL backup and restore procedures](https://www.postgresql.org/docs/9.5/static/backup.html). As for exports, that could be implemented as a CSV download link from Netbox.
Author
Owner

@jeremystretch commented on GitHub (Sep 21, 2016):

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
@jeremystretch commented on GitHub (Sep 21, 2016): 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 ```
Author
Owner

@Ra1esh commented on GitHub (Dec 4, 2018):

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

@Ra1esh commented on GitHub (Dec 4, 2018): 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
Author
Owner

@mchlumsky commented on GitHub (Jan 21, 2019):

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

@mchlumsky commented on GitHub (Jan 21, 2019): Just for completeness, you should probably also backup the media and report directories.
Author
Owner

@AllenEllis commented on GitHub (Feb 2, 2019):

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.

@AllenEllis commented on GitHub (Feb 2, 2019): 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](https://github.com/andreafabrizi/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`. ```bash #!/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: ```bash 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.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#444