Backup and restore of mnesia database

From Notes_Wiki

Home > Erlang > Mnesia > Backup and restore of mnesia database

Taking backup of mnesia database

To take backup use:

mnesia:backup("<backup-file>")


Restore backup of mnesia database from backup-file

To restore backup from file use:

mnesia:restore("<backup-file>", [{default_op, recreate_tables}])


Restore backup on different node

To restore backup on different create a module test.erl with following contents:

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

change_node_name(Mod, From, To, Source, Target) ->
    Switch =
        fun(Node) when Node == From -> To;
           (Node) when Node == To -> throw({error, already_exists});
           (Node) -> Node
        end,
    Convert =
        fun({schema, db_nodes, Nodes}, Acc) ->
                {[{schema, db_nodes, lists:map(Switch,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, lists:map(Switch, 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).

and then use following steps:

  1. Start erl shell using
    erl -sname node1
  2. Compile test module using
    c(test).
  3. Start mnesia on local node using
    mnesia:create_schema([node()]).
    mnesia:start().
  4. Try to restore original backup using
    mnesia:restore(<backup-file>, [{default_op, recreate_tables}]).
    and note the error message which should be like
    {aborted,{'EXIT',{aborted,{bad_commit,{missing_lock,<original-node-name>}}}}}
  5. Now rename node-name in backup using
    test:change_node_name(mnesia_backup, <original-node-name>, node1@localhost, "<backup-file>", "<new-backup-file>").
  6. Now restore the new-backup-file using mnesia:restore which should succeed this time
    mnesia:restore(<new-backup-file>, [{default_op, recreate_tables}]).
  7. If Graphical access is available check backup using observer
    observer:start()


Home > Erlang > Mnesia > Backup and restore of mnesia database