]> granicus.if.org Git - ejabberd/commitdiff
New command to dump a table to text file.
authorBadlop <badlop@process-one.net>
Thu, 23 Jul 2009 15:23:08 +0000 (15:23 +0000)
committerBadlop <badlop@process-one.net>
Thu, 23 Jul 2009 15:23:08 +0000 (15:23 +0000)
SVN Revision: 2389

src/ejabberd_admin.erl

index d7f00b18510df556c89333e2bfd497da88ef7600..2c03d10b697f8f689fe9f787c6ae113699f91c6d 100644 (file)
@@ -39,9 +39,9 @@
         delete_expired_messages/0, delete_old_messages/1,
         %% Mnesia
         backup_mnesia/1, restore_mnesia/1,
-        dump_mnesia/1, load_mnesia/1,
+        dump_mnesia/1, dump_table/2, load_mnesia/1,
         install_fallback_mnesia/1,
-        dump_to_textfile/1,
+        dump_to_textfile/1, dump_to_textfile/2,
         mnesia_change_nodename/4,
         restore/1 % Still used by some modules
        ]).
@@ -133,6 +133,10 @@ commands() ->
                        desc = "Dump the database to text file",
                        module = ?MODULE, function = dump_mnesia,
                        args = [{file, string}], result = {res, restuple}},
+     #ejabberd_commands{name = dump_table, tags = [mnesia],
+                       desc = "Dump a table to text file",
+                       module = ?MODULE, function = dump_table,
+                       args = [{file, string}, {table, string}], result = {res, restuple}},
      #ejabberd_commands{name = load, tags = [mnesia],
                        desc = "Restore the database from text file",
                        module = ?MODULE, function = load_mnesia,
@@ -320,8 +324,28 @@ module_tables(mod_shared_roster) -> [sr_group, sr_user];
 module_tables(mod_vcard) -> [vcard, vcard_search];
 module_tables(_Other) -> [].
 
+get_local_tables() ->
+    Tabs1 = lists:delete(schema, mnesia:system_info(local_tables)),
+    Tabs = lists:filter(
+            fun(T) ->
+                    case mnesia:table_info(T, storage_type) of
+                        disc_copies -> true;
+                        disc_only_copies -> true;
+                        _ -> false
+                    end
+            end, Tabs1),
+    Tabs.
+
 dump_mnesia(Path) ->
-    case dump_to_textfile(Path) of
+    Tabs = get_local_tables(),
+    dump_tables(Path, Tabs).
+
+dump_table(Path, STable) ->
+    Table = list_to_atom(STable),
+    dump_tables(Path, [Table]).
+
+dump_tables(Path, Tables) ->
+    case dump_to_textfile(Path, Tables) of
        ok ->
            {ok, ""};
        {error, Reason} ->
@@ -331,17 +355,12 @@ dump_mnesia(Path) ->
     end.
 
 dump_to_textfile(File) ->
-    dump_to_textfile(mnesia:system_info(is_running), file:open(File, [write])).
-dump_to_textfile(yes, {ok, F}) ->
-    Tabs1 = lists:delete(schema, mnesia:system_info(local_tables)),
-    Tabs = lists:filter(
-            fun(T) ->
-                    case mnesia:table_info(T, storage_type) of
-                        disc_copies -> true;
-                        disc_only_copies -> true;
-                        _ -> false
-                    end
-            end, Tabs1),
+    Tabs = get_local_tables(),
+    dump_to_textfile(File, Tabs).
+
+dump_to_textfile(File, Tabs) ->
+    dump_to_textfile(mnesia:system_info(is_running), Tabs, file:open(File, [write])).
+dump_to_textfile(yes, Tabs, {ok, F}) ->
     Defs = lists:map(
             fun(T) -> {T, [{record_name, mnesia:table_info(T, record_name)},
                            {attributes, mnesia:table_info(T, attributes)}]}
@@ -350,10 +369,10 @@ dump_to_textfile(yes, {ok, F}) ->
     io:format(F, "~p.~n", [{tables, Defs}]),
     lists:foreach(fun(T) -> dump_tab(F, T) end, Tabs),
     file:close(F);
-dump_to_textfile(_, {ok, F}) ->
+dump_to_textfile(_, _, {ok, F}) ->
     file:close(F),
     {error, mnesia_not_running};
-dump_to_textfile(_, {error, Reason}) ->
+dump_to_textfile(_, _, {error, Reason}) ->
     {error, Reason}.
 
 dump_tab(F, T) ->