Database Merge

109 views
Skip to first unread message

Jason Kopacko

unread,
May 26, 2020, 1:51:21 PM5/26/20
to NetBox
I have two desperate NetBox instances by the same customer (yea I know).

Is there a way (easy) that I can merge the two databases into 1 single NetBox instance?

Mahomed Hussein

unread,
May 27, 2020, 6:17:09 AM5/27/20
to NetBox
I suspect your best option will be to export out of one database and import into the other. If you try to merge, you'll probably get conflicts with the IDs of different objects.

Brian Candler

unread,
May 27, 2020, 11:46:37 AM5/27/20
to NetBox
There's no comprehensive "export" and "import" functionality in Netbox - just a few piecemeal CSV things which are only a small subset of the job in hand. In particular, they can't keep references to things where those things are unnamed, or the names overlap.

I had to mess with low-level SQL updates to Netbox in the old days - converting Device instances into VirtualMachine instances.  (The details should be in the group archives if you search hard enough).

The Netbox SQL schema is decent with proper foreign key relationships, so in principle it should be doable.

The problem will be the overlapping ids.  The way you could approach it is:
1. take a copy of the smaller database
2. renumber all the ids into ranges which are not used in the larger database.
3. copy the objects into the larger database
4. reset all the sequence numbers so they start beyond the new range (there's a Django utility which does this)

Step (2) is the tricky one, because whenever you change the id of an object, at the same time you have to change all the foreign key references to it.  Maybe setting ON UPDATE CASCADE is all you need:

Otherwise, I think you'd have to disable FK constraints and re-enable them later; or duplicate the objects under new IDs, change the FKs, and then delete the original objects.

Derek Ragona

unread,
May 27, 2020, 3:38:28 PM5/27/20
to NetBox
I maintain two different instances of netbox with separate databases.  One instance is used by developers to test code and API calls.  I will at times "refresh" the developer's instance with a fresh copy from our production instance.  When I do this refresh I dump the development database and recreate it from the production copy.  The only issue I have found in my refreshes is the perms and users are included and if there were different permissions in the development DB those have to be re-done after the import.
Reply all
Reply to author
Forward
0 new messages