How to export and import Book nodes?
Posted June 24th, 2008 by DrupalaceI wanted to move a Book from one site to another. I see a couple of modules that look up to the task – Book Import and Export DocBook – but they're for 4.x only. For 5.x, I see Export DXML. Yet the scant description online (http://drupal.org/project/export_dxml) says that Book Import (the 4.x module) is used to then import the exported content. Hmm.
So I tried anyway exporting from 5.x using Export DXML, and then tried to figure out a way to import... but upon installing Export DXML, I got only a setting for whether or not PHP is also exported. That's all – no commands I can find to actually initiate an export, nothing. The module's documentation says zilch too. What should I be looking for in Export DXML to actually perform an export?
Or, given the lack of 5.x modules for import/export, perhaps there's some built-in method for importing and exporting Books in 5.x that doesn't require third-party modules. Am I missing something obvious?
Question answer
This plea of mine on the Drupal.org forums got no response but the dolorous sigh of a forsaken wind. Just as I was about to retreat to bitter existence's sole comforting shoulder – I'll always be there for you too, Wild Turkey – the kindly and knowledgeable GR stepped in by email with ideas.
I headed to phpMyAdmin (which used to scare me but I'm better now, thank you), called up my donor database that held the Book captive, and used this SQL command:
select * from 'node' where type='book'
which selects all records from the node table, of type 'book'. I exported the results into a file.
Then I went to the hot-with-anticipation recipient database and attempted to import the file. The returned message started as follows:
INSERT INTO `node` ( `nid` , `vid` , `type` , `title` , `uid` ,
`status` , `created` , `changed` , `comment` , `promote` ,
`moderate` , `sticky` )
VALUES ( 13, 13, 'book', 'Understanding These Instructions', 1, 1,
1168408293, 1188227828, 2, 0, 0, 0 ) ,...
... and so on, for 40 or so book records within VALUES. But it then all failed with the error:
#1062 - Duplicate entry '13-13' for key 1
Apparently, the first record, which has nid (node ID) 13 and vid (version ID) 13, ran up against an existing node that also has nid 13 and vid 13. Like two Jean-Claude Van Dammes in Timecop, these cannot exist in the same place at the same time without results most distressing.
Interestingly, the clash didn't involve any existing Book nodes, as the site in question (this one!) had no Book nodes. So the existing node fervently laying claim to nid/vid 13 wasn't a Book node, but apparently that makes no difference.
I figured that I needed some way to change the nid/vid numbers of the old Book nodes before exporting them. Back to the donor site's database. GR helped out with the SQL here:
update node_revisions set nid=nid + 1000000 where nid in (select nid from node where type='book');
then
update node set nid=nid + 1000000 where type='book'
(I like the audacity of the numbers! Works for me: if we're going to artificially boost the ID numbers, let's kick the buggers up into the bloody ionosphere.)
That done, I went and did the same for vid too (replace nid with vid in the above). And once again,
select * from 'node' where type='book'
and export.
GR reminded me that I need to export and import not just from the nodes table, but from the node_revision table as well. So I did the same updating of nid/vid within that table, then the export procedure, all replacing node with node_revision.
Next up: you guessed it, head to the recipient database and import my files containing the above exports.
Solved?
Alas... things weren't quite licked. The nodes didn't function properly as Book nodes. They showed up as content, but the imported Book structure didn't show up in any way under admin/content/book. And when editing or creating a Book node, none of my imported Book nodes showed up under the drop-down Parent field – i.e., there wasn't a way to recreate the actual structure of the book. The nodes were there, but they weren't right. ("The nodes are sour", a crusty Maine character might deliver in a Stephen King yarn.)
Well, around there I cheated, and brought over my Book the really old-fashioned way: cut'n'paste from the donor site into fresh new Book nodes on the recipient site. (Those are, by the way, the Easy Drupal Admin Manual.) I had a lot of editing in store for each node anyway, so a one-step mass import wasn't an utmost requirement. Still, a chagrined Ace I am.
Sleuthster GR suggests that I left a third vital table out of the process: the books table, which also needs its nids/vids changed, and its content exported and imported, like the other tables above. Hmm, a table called books... that does sound suspiciously important to the task at hand. Chagrined and red-cheeked I am.
That extra step may have done the deed, but I had already embarked upon my more Luddite methods, and since then have not had cause to attempt a fresh export and import of a Book. So I'm going to leave this one an Unanswered Question for now, simply because I can't yet verify the whole proper process. But I hope this tale, and GR's assistance, will help someone else complete the task. Please write in if you have experience or ideas here!
Another method?
The helpful boris-doesborg suggests here that two modules that may provide a solution:
http://drupal.org/project/importexportapi
and
http://drupal.org/project/node_import
I don't have need or opportunity to experiment with these now, but if anyone out there does, please report!












