anchor.diff:              {atomic,ok};
           Error ->
               Error   
  +    end.
  +
  +timestamp({Mega, Secs, Micro}) -> Mega*1000000000000 + Secs*1000000 + Micro.
  +
  +backup_snap() ->
  +    {_, SnapDir} = application:get_env(imem, imem_snapshot_dir),
  +    SnapFiles = filelib:wildcard(filename:join([SnapDir, "*.bkp"])),
  +    ZipCandidates =  [SF || SF <- SnapFiles, filelib:file_size(SF) > 0],
  +    if ZipCandidates =:= [] -> ok;        
  +        true ->
  +            ZipFileName = filename:join([SnapDir, "snapshot_"++integer_to_list(timestamp(erlang:now()))++".zip"]),
  +            zip:zip(ZipFileName, ZipCandidates),
  +            ?Log("~p old snapshots are backed up to ~p~n", [?MODULE, ZipFileName])
       end.
   
   %% ----- TESTS ------------------------------------------------
  
positive.code:         {atomic,ok} ->
              [Up] = ets:lookup(?MODULE, Table),
              true = ets:insert(?MODULE, Up#user_properties{last_write = erlang:now()}),
              {atomic,ok};
          Error ->
              Error   
  
negative.code:     end,
      Tabs = [T || T <- mnesia:system_info(tables), re:run(atom_to_list(T), "(.*@.*)|schema") =:= nomatch],
      [(fun() ->
          case ets:lookup(?MODULE, T) of
              [] -> ok;
              [#user_properties { table = T, last_write = Wt, last_snap = St} = Up | _] ->
                  LastWriteTime = timestamp(Wt),
                  LastSnapTime = timestamp(St),
                  if 
                      LastSnapTime < LastWriteTime ->
                          %if T =:= ddTable -> io:format(user, "snap ~p timestamps ~p ~p~n", [T, {LastWriteTime, LastSnapTime}, {Wt, St}]); true -> ok end,
                          mnesia:transaction(fun() ->
                                          Rows = mnesia:select(T, [{'$1', [], ['$1']}], write),
                                          BackFile = filename:join([SnapDir, atom_to_list(T)++".bkp"]),
                                          NewBackFile = filename:join([SnapDir, atom_to_list(T)++".bkp.new"]),
                                          ok = file:write_file(NewBackFile, term_to_binary(Rows)),
                                          {ok, _} = file:copy(NewBackFile, BackFile),
                                          ?Log("snap ~p -> ~p~n", [T, BackFile]),
                                          ok = file:delete(NewBackFile)
                                         end),
                          true = ets:insert(?MODULE, Up#user_properties{last_snap = erlang:now()});
                      true -> 
                          %if T =:= ddTable -> io:format(user, "nosnap ~p timestamps ~p ~p~n", [T, {LastWriteTime, LastSnapTime}, {Wt, St}]); true -> ok end,
                          ok % no backup needed
                  end
          end
        end)()
      || T <- Tabs],
  