Permalink
Browse files

Merge "[service-ng]add mnesia backup/restore script for rabbit"

  • Loading branch information...
2 parents dd6dec9 + c1c301a commit be49f4c51ac84f6d202bd5c6bc20bb6b53f3f4a2 @andyzh andyzh committed with Gerrit Code Review Feb 1, 2013
@@ -17,6 +17,8 @@ templates:
daylimit_ctl: bin/daylimit_ctl
daylimit.yml.erb: config/daylimit.yml
+ backup_or_restore.escript: bin/backup_or_restore.escript
+
packages:
- common
- erlang
@@ -0,0 +1,44 @@
+#!/usr/bin/env escript
+
+%%! -sname backup
+
+-record(amqqueue, {name, durable, auto_delete, exclusive_owner, arguments, pid, slave_pids, mirror_nodes}).
+-record(exchange, {name, type, durable, auto_delete, internal, arguments, scratch}).
+
+%% Mnesia restore only recover the db info for the queues and exchanges of rabbit, to make these queues and exchanges work, we still
+%% need to redeclare these old items after clear its mnesia record info
+main([Type, Name, Backup]) ->
+if Type == "restore" ->
+ Host = lists:concat([Name, "@localhost"]),
+ %% Restore the mnesia database
+ rpc:call(list_to_atom(Host), mnesia, restore, [Backup, []]),
+
+ %% Get queues and exchanges info from restored db
+ Queues = rpc:call(list_to_atom(Host), mnesia, dirty_select, [rabbit_durable_queue, [{#amqqueue{name='$1', durable='$2', auto_delete='$3', arguments='$4', _='_'}, [], ['$$']}]]),
+ Exchanges = rpc:call(list_to_atom(Host), mnesia, dirty_select, [rabbit_durable_exchange, [{#exchange{name='$1', type='$2', durable='$3', auto_delete='$4', internal='$5', arguments='$6', _='_'}, [], ['$$']}]]),
+
+ %% Remove all the queues and exchanges info in db
+ Fdel = fun() ->
+ QueueKeys = mnesia:all_keys(rabbit_durable_queue),
+ lists:foreach(
+ fun(Key) -> mnesia:delete(rabbit_durable_queue, Key, write) end,
+ QueueKeys),
+ ExchangeKeys = mnesia:all_keys(rabbit_durable_exchange),
+ lists:foreach(
+ fun(Key) -> mnesia:delete(rabbit_durable_exchange, Key, write) end,
+ ExchangeKeys)
+ end,
+ rpc:call(list_to_atom(Host), mnesia, transaction, [Fdel]),
+
+ %% Redeclare old queues and exchanges
+ lists:foreach(fun(Queue) ->
+ rpc:call(list_to_atom(Host), rabbit_amqqueue, declare, lists:append(Queue, [none])) end,
+ Queues),
+ lists:foreach(fun(Exchange) ->
+ rpc:call(list_to_atom(Host), rabbit_exchange, declare, Exchange) end,
+ Exchanges);
+ Type == "backup" ->
+ Host = lists:concat([Name, "@localhost"]),
+ %% Backup the mnesia database
+ rpc:call(list_to_atom(Host), mnesia, backup, [Backup])
+end.
@@ -37,6 +37,7 @@ case $1 in
mkdir -p $COMMON_DIR/bin
rsync -arl $JOB_DIR/bin/warden_service_ctl $COMMON_DIR/bin
+ rsync -arl $JOB_DIR/bin/backup_or_restore.escript $COMMON_DIR/bin
rsync -arl /var/vcap/packages/common/utils.sh $COMMON_DIR/bin
chmod -R 755 $COMMON_DIR/bin

0 comments on commit be49f4c

Please sign in to comment.