您在這裡

合併兩個 drupal 站到同一個 database

dennys's 的頭像
dennys 在 2009-03-23 (週一) 15:10 發表

最近把兩個 drupal 6 的 database 合併, 但外觀要維持一樣, 大概的作法如下:
1. 本來兩個站用的就是 multi-site 的架構, 因此檔案的部份不需處理 (參考: https://drupaltaiwan.org/forum/20090112/2982)
2. 內容 (node, comment, files ...) 的部份: 因為沒有工具, 只好用 SQL 一個 table 給轉換到另一個資料庫.
3. 外觀的部份 (theme 等等), 可使用 settings.php 的 $conf (參考 https://drupaltaiwan.org/forum/20070705/1379), block 是利用不同的 theme 達到分開的目的, 因為我都用 Garland, 所以弄了個簡單的空殼子 sub-theme (參考: https://drupaltaiwan.org/forum/20090321/3242)
4. primary menu 的部份, 因為修改 settings.php 沒用, 所以我裝了 Domain Access 模組的 Domain Configuration 來指定不同站使用不同的 primary menu. (我的問題貼在這: https://drupaltaiwan.org/forum/20070705/1379#comment-10525)
5. 最後權限的部份, 就直接利用 Domain Access 模組了, 這個其實可以不用, 但缺點就是各 site 就可以互相看來看去.

最後成果如下, 從 site name, menu, theme, block, node, comment 都是分開的了:
http://sunnys.tiger2.net/ => 這個站不動
http://cathysunnys.blogsite.org/ => 這個站合併到上面那個

漏了一個, 因為都沒有設定 path, 所以 node 會重複, 在 .htaccess 加上下列 rewrite 就可以了. (每個 node 都得加兩行, 我只列前兩個當範例, 而且得一直重複 RewriteCond, 還在想看有沒有辦法解決)

RewriteCond %{HTTP_HOST} ^cathysunnys.blogsite.org$
RewriteRule ^node/142$ /node/915 [R=301,L]
RewriteCond %{HTTP_HOST} ^cathysunnys.blogsite.org$
RewriteRule ^node/2$ /node/775 [R=301,L]

btw, 我本來以為會有不少人對這個主題有興趣耶...

2. 內容 (node, comment, files ...) 的部份: 因為沒有工具, 只好用 SQL 一個 table 給轉換到另一個資料庫.

可以详细介绍一下这个部分是如何操作的吗?

我现在也由此需求.

我尝试phpmyadmin,但是总是有一些小问题,主要是插件上的数据有点错误.例如论坛没有分区,所有都是版块了.

這是之前整理的筆記, 有點亂... 應該是說非常亂 :p 可能再找時間整理一下.

SiteS: Source site
SiteT: Target site

Pre-check:

  • The uid of the 2 site should be the same
  • The filter format of the 2 site should be the same, you can check filter_formats table or see it from http://example.com/admin/settings/filters. But because we need to make sure the format id ("format" field filter_formats table) of is the same, suggest to check table.

Vocabulary:
1. Migrate {vocabulary} table

Export {vocabulary} table from SiteS and this is the original SQL:
INSERT INTO dr_vocabulary (vid, name, description, help, relations, hierarchy, multiple, required, tags, module, weight) VALUES(1, 'Category', 'Category', '', 0, 1, 1, 0, 0, 'taxonomy', 0);

Change to this SQL and insert into SiteT
INSERT INTO dr_vocabulary (name, description, help, relations, hierarchy, multiple, required, tags, module, weight) VALUES('Category', 'Category', '', 0, 1, 1, 0, 0, 'taxonomy', 0);

ps: because the 1st field is an AUTO_INCREMENT, please remove it and MySQL will generate it.

2. Migrate {vocabulary_node_types} table

Export {vocabulary_node_types} table from SiteS and this is the original SQL:
INSERT INTO dr_vocabulary_node_types VALUES(1, 'image');

Change to this SQL and insert into SiteT
INSERT INTO dr_vocabulary_node_types VALUES(2, 'image');
先到 SiteS 檢查 {vocabulary} 從哪一個編號開始是這次新增的, 然後就把 {vocabulary_node_types} 改成對應的編號.

Taxonomy:
Export {term_data}, remove the tid column and change vid to above new data.
INSERT INTO dr_term_data (vid, name, description, weight) VALUES(2, 'Wedding', '', -8);
INSERT INTO dr_term_data (vid, name, description, weight) VALUES(2, '', '', 0);
INSERT INTO dr_term_data (vid, name, description, weight) VALUES(2, 'Japan trip', '', 0);
INSERT INTO dr_term_data (vid, name, description, weight) VALUES(2, '領子歪歪 不太專業的西服照', '照完後好久 才發現領子不正拉', 0);
INSERT INTO dr_term_data (vid, name, description, weight) VALUES(2, 'Necklace and Ring', '傳統的', 0);
INSERT INTO dr_term_data (vid, name, description, weight) VALUES(2, 'PhotoGraph', '法國巴黎', 0);
INSERT INTO dr_term_data (vid, name, description, weight) VALUES(2, 'High Speed Railway', '高鐵商務艙', 0);
INSERT INTO dr_term_data (vid, name, description, weight) VALUES(2, 'Cathy''s Dishes', '', 0);
INSERT INTO dr_term_data (vid, name, description, weight) VALUES(2, 'Cookies', '', 0);
注意: 如果之前有某些 taxonomy被刪除了, 還是得給他一筆空的號碼 (如上面的第二筆), 之後再刪除, 這樣後面才不會亂掉.

Use this SQL to generate data for {term_hierarchy}

SELECT tid+40 tid, CASE parent WHEN 0 THEN 0 ELSE parent+40 END parent FROM dr_term_hierarchy WHERE 1

說明: "40" 是從 {term_data} 裡面抓到的新增編號開頭, 0 必須特別處理因為他代表沒有 parent.

Check Point:
http://example.com/admin/content/taxonomy

Node:
1. Use this SQL to get the next nid from SiteS

SELECT MAX( nid ) FROM dr_node

請把這個數字記下來, 譬如說 773, 則後面和 nid (node id) 有關的所有欄位都要加上 773

2. Use this SQL to generate node date, please change the @value to the above max(nid)
如果想直接用流水號, 可以這樣

SET @a=773;
SELECT @a:=@a+1 nid, vid, type, language, title, uid, status, created, changed, comment, promote, moderate, sticky, tnid, translate FROM dr_node a
ORDER BY nid;

如果考慮跳號, 可以這樣

SELECT nid + 773 nid, vid + 773 vid, type, language, title, uid, status, created, changed, comment, promote, moderate, sticky, tnid, translate FROM dr_node a
ORDER BY nid;

注意:
uid 我已經先把兩個 site 的都改成一樣了
這裡的 vid 指的是 {node_revision} 的 reVision ID, 而不是 vocabulary id
phpMyAdmin 的 export 產生出來的 INSERT 怪怪的, type, status, changed, comment 等好多個欄位都不見了, 得自己補上去.

上述 SQL 產生的最大 nid 如果是 930, 則在 SiteT 執行下列 SQL 修改 AUTO_INCREMENT 的值

ALTER TABLE dr_node AUTO_INCREMENT =931
ALTER TABLE dr_node_revisions AUTO_INCREMENT =931


SELECT nid + 773 nid, vid + 773 vid,uid,title,body,teaser,log,timestamp, CASE format WHEN 0 THEN 0 WHEN 2 THEN 3 END format
FROM dr_node_revisions a
ORDER BY nid

以下為 node + taxonomy
SELECT nid+773 nid, tid+40 tid, nid+773 vid
FROM dr_term_node

Comment:


SET @a=344;
SELECT n2.*,
c.cid, c.pid, c.nid, c.uid, c.subject, c.comment, c.hostname, c.timestamp, c.status, c.format, c.thread, c.name, c.mail, c.homepage
FROM (SELECT @a:=@a+1 nid, nid old_nid, type FROM dr_node ORDER BY nid) as n2, dr_comments c
WHERE n2.old_nid = c.nid

pid: parent comment id

thread: http://drupal.org/node/264774 (check natrio - January 26, 2009 - 19:00)

Post-check:
Paste a new node, command and image and check node, comment tables to make sure it's correct

301 Redirect

SELECT concat('RewriteRule ^node/', nid, '$ /node/', nid+773, ' [R=301,L]') FROM `dr_node` WHERE 1