2010-09-14 30 views
13

我有一些disc_only_copies類型的大表。 現在我需要更改短節點名稱長,但不能與RAM限制...如何備份/恢復從/到mnesia的單個表?

我可以部分使用備份/恢復數據庫(按表)嗎?

+0

我通過操作schema.DAT解決了我的麻煩。我用dets模塊打開它並做一些替換。這是從節點到節點進行遷移的最快方式。 – vinnitu 2010-09-23 13:05:18

+2

你能發表一個答案並接受它嗎?這個問題有足夠的upvotes,我不想刪除它,但它可能不應該顯示在未答覆的列表。謝謝。 – 2010-10-01 16:35:00

回答

0
-module(test). 
    -compile(export_all). 

    -record(tab, {first, second}). 

    do() -> 
      mnesia:create_schema([node()]), 
      mnesia:start(), 
      mnesia:create_table(tab, [{disc_copies, [node()]}, {attributes, record_info(fields, tab)}]), 
      mnesia:dirty_write({tab, 1, 2}), 
      mnesia:dirty_write({tab, a, b}), 
      mnesia:stop(). 

    change_node('[email protected]') -> 
      '[email protected]'; 
    change_node('[email protected]') -> 
      '[email protected]'; 
    change_node(Node) -> 
      Node. 

    handle_nodes(Nodes) -> 
      lists:map(fun(Node) -> 
        change_node(Node) 
      end, Nodes -- [[email protected], [email protected]]). 

    handle_cookie({TS, Node}) -> 
      {TS, change_node(Node)}. 

    handle_version_value([]) -> 
      []; 
    handle_version_value({'[email protected]', _}) -> 
      []; 
    handle_version_value({'[email protected]', _}) -> 
      []; 
    handle_version_value({Node, TS}) -> 
      {change_node(Node), TS}. 

    handle_version({Key, Value}) -> 
      {Key, handle_version_value(Value)}. 

    handle_def(Def) -> 
      lists:map(fun({Key, Value} = Property) -> 
        case Key of 
        ram_copies -> 
          {Key, handle_nodes(Value)}; 
        disc_copies -> 
          {Key, handle_nodes(Value)}; 
        disc_only_copies -> 
          {Key, handle_nodes(Value)}; 
        cookie -> 
          {Key, handle_cookie(Value)}; 
        version -> 
          {Key, handle_version(Value)}; 
        _ -> 
          Property 
        end 

     end, Def). 

go() -> 
     {ok, N} = dets:open_file(schema, [{file, "./schema.DAT"},{repair,false}, {keypos, 2}]), 
     do2(N), 
     dets:sync(N), 
     dets:close(N). 

do2(N) -> 
     do2(N, dets:first(N)). 

do2(_N, '$end_of_table') -> 
     ok; 
do2(N, Key) -> 
     io:format("process: ~p~n", [Key]), 
     [{N, Tab, Def}] = dets:lookup(N, Key), 
     NewDef = handle_def(Def), 
     dets:insert(N, {N, Tab, NewDef}), 
%  file:write_file("schema.txt", io_lib:format("~p~n", [{N, Tab, NewDef}]), [append]), 
     do2(N, dets:next(N, Key)). 
+0

它有助於我的情況 – vinnitu 2010-10-06 07:56:00