Search code examples
erlangmnesia

Transfer Mnesia Backup To Smaller Cluster


I have a backup from a 3 node Mnesia cluster (a production cluster) that I want to restore to a 2 node cluster (which I use for development purposes to reproduce bugs). If the cluster had the same number of nodes, one would follow the recipe outlined here. How does one remove a node from the backup? Is this possible?

I tried changing the clause ({schema, db_nodes, Nodes}, Acc) to return a smaller list of nodes, but that doesn't seem to work (or maybe it works, but is insufficient).


Solution

  • Turns out this can be done pretty easily:

    #!/usr/bin/env escript
    
    change_node_name(Mod, Source, Target) ->
    
        % Change this to whatever you want the new node list to be,
        % with however many nodes, whatever names, etc.
        ChangeNodes =
            fun(_Nodes) ->
                ['[email protected]','[email protected]']
            end,
    
        Convert =
            fun({schema, db_nodes, Nodes}, Acc) ->
                    {[{schema, db_nodes, ChangeNodes(Nodes)}], Acc};
               ({schema, version, Version}, Acc) ->
                    {[{schema, version, Version}], Acc};
               ({schema, cookie, Cookie}, Acc) ->
                    {[{schema, cookie, Cookie}], Acc};
               ({schema, Tab, CreateList}, Acc) ->
                    Keys = [ram_copies, disc_copies, disc_only_copies],
                    OptSwitch =
                        fun({Key, Val}) ->
                                case lists:member(Key, Keys) of
                                    true -> {Key, ChangeNodes(Val)};
                                    false-> {Key, Val}
                                end
                        end,
                    {[{schema, Tab, lists:map(OptSwitch, CreateList)}], Acc};
               (Other, Acc) ->
                    {[Other], Acc}
            end,
        mnesia:traverse_backup(Source, Mod, Target, Mod, Convert, switched).
    
    main(_Args)->
        change_node_name(mnesia_backup,"/tmp/source-backup","dest-backup").