[erlang-questions] Use Mnesia backup on other machines

Robert Raschke rtrlists@REDACTED
Mon Aug 31 19:34:47 CEST 2009


On Mon, Aug 31, 2009 at 5:43 PM, Paul Mineiro <paul-trapexit@REDACTED>wrote:

> Inline.
>
> -- p
>
> On Sun, 30 Aug 2009, ngocdaothanh wrote:
>
> > Hi everyone,
> >
> > I would like to ask about Mnesia backup:
> > 1. How to periodically create backup of Mnesia DB of a node?
>
> You can have a gen_server which uses timer:send_interval/2 combined with
> handle_info/2 to trigger the backups.  You can back up everything with
> mnesia:backup/1.  In case the backup takes a very long time you should
> twiddle something in the gen_server state to prevent starting a new backup
> while an old one is happening.
>
>
No need for the timer though, just use the gen_server's own timeout return
value. Combine this with calculating a timeout value using the
until_next_time() function from crone and you can have nicely scheduled
backups.

Minimal, untested and not even compiled example, started with something like
mybackup:start_link("DB.BAK", {daily, {every, {1, hr}, {between, {12, 0, 0,
am}, {11, 59, 59, pm}}}}) .

-module(mybackup).

-behaviour(gen_server).

-export([start_link/2, init/1, handle_info/2]).

start_link(Backup_File, Backup_When) ->
    gen_server:start_link({local, ?MODULE}, ?MODULE, [Backup_File,
Backup_When], []).

init([Backup_File, Backup_When]) ->
    {ok, {Backup_File, Backup_When},
1000*crone:until_next_time(Backup_When)}.

handle_info(timeout, {Backup_File, Backup_When} = State) ->
    mnesia:backup(Backup_File),
    % HACK: Just in case it took less than a second, make sure we don't do
it again right away.
    timer:sleep(1000),
    {noreply, State, 1000*crone:until_next_time(Backup_When)}.


Robby


More information about the erlang-questions mailing list