anchor.diff:              erlang:whereis(?MODULE) ! imem_snap_loop
           end).
   -endif.
  -
  -fire_cluster_snap(Tables) ->
  -    IntervalMs = ?GET_CLUSTER_SNAPSHOT_INTERVAL,
  -    ?Info("starting ~p snapshot after ~pms", [Tables, IntervalMs]),
  -    erlang:send_after(IntervalMs, ?MODULE, {cluster_snap, Tables}).
   
   
   %% ----- SERVER INTERFACE ------------------------------------------------
  
positive.code: end.">>)).
  -define(GET_CLUSTER_SNAPSHOT,?GET_CONFIG(snapshotCluster,[],true)).
  -define(GET_CLUSTER_SNAPSHOT_TABLES,?GET_CONFIG(snapshotClusterTables,[],[ddAccount,ddRole,ddConfig])).
  -define(GET_CLUSTER_SNAPSHOT_INTERVAL,?GET_CONFIG(snapshotClusterInterval,[],86400000)).
  
  -ifdef(TEST).
      start_snap_loop() -> ok.
  
negative.code: 
      process_flag(trap_exit, true),
      start_snap_loop(),
      CST = ?GET_CLUSTER_SNAPSHOT_TABLES,
      ?Info("starting ~p snapshot after ~pms",
            [CST, ?CLUSTER_SNAP_CHECK_INTERVAL]),
      erlang:send_after(?CLUSTER_SNAP_CHECK_INTERVAL, ?MODULE,
                        {cluster_snap, CST, os:timestamp(),
                         create_clean_dir("backup_snapshot_")}),
      {ok,#state{snapdir = SnapshotDir}}.
  
  create_clean_dir(Prefix) ->
      {_,_,Us} = Now = os:timestamp(),
      {{Y,M,D},{H,Mn,S}} = calendar:now_to_local_time(Now),
      Sec = S + Us / 1000000,
      {_, SnapDir} = application:get_env(imem, imem_snapshot_dir),
      Dir = lists:flatten(io_lib:format("~s~4..0B~2..0B~2..0B_~2..0B~2..0B~9.6.0f", [Prefix,Y,M,D,H,Mn,Sec])),
      BackupDir = filename:join(filename:absname(SnapDir), Dir),
      case filelib:is_dir(BackupDir) of
          true ->
              lists:foreach(fun(F) ->
                                    ok = file:delete(F)
                            end, filelib:wildcard("*.*", BackupDir));
          false ->
              ?Info("creating dir ~s", [BackupDir]),
              ok = file:make_dir(BackupDir)
      end,
      BackupDir.
  
  cluster_snap([], StartTime, _Dir) ->
      CheckInterval = ?CLUSTER_SNAP_CHECK_INTERVAL
      - if StartTime /= '$replace_with_timestamp' ->
               PTime = timer:now_diff(os:timestamp(), StartTime) div 1000,
               ?Info("snapshot took ~pms", [PTime]),
               PTime;
           true -> 0
        end,
      ClusterSnapTables = ?GET_CLUSTER_SNAPSHOT_TABLES,
      ?Info("next snapshot of ~p after ~pms", [ClusterSnapTables, CheckInterval]),
      erlang:send_after(
        CheckInterval, ?MODULE,
        {cluster_snap, ClusterSnapTables, os:timestamp(),
         create_clean_dir("backup_snapshot_")});
  cluster_snap([T|Tabs], StartTime, Dir) ->
      NextTabs = case catch take_chunked(T, Dir) of                   
                     ok ->
                         ?Info("snapshot ~p", [T]),
                         Tabs;
                     [{error,Error}] ->
                         ?Error("cluster_snap failed for ~p : ~p", [T, Error]),
                         Tabs++[T];
                     Error ->
                         ?Error("cluster_snap failed for ~p : ~p", [T, Error]),
                         Tabs++[T]
                 end,
      erlang:send_after(1000, ?MODULE, {cluster_snap, NextTabs, StartTime, Dir}).
  
  handle_info({cluster_snap, Tables, StartTime, Dir}, State) ->
      case ?GET_CLUSTER_SNAPSHOT of
          true ->
              ClusterSnapHour = ?GET_CLUSTER_SNAPSHOT_TOD,
              case calendar:now_to_local_time(os:timestamp()) of
                  {{_,_,_},{ClusterSnapHour,_,_}} ->
                      spawn(fun() -> cluster_snap(Tables, StartTime, Dir) end);
                  _ -> nop
              end;
          false -> erlang:send_after(1000, ?MODULE, {cluster_snap, [], '$replace_with_timestamp', '$no_dir'})
      end,
      {noreply, State};
  handle_info(imem_snap_loop, #state{snapFun=SFun,snapHash=SHash} = State) ->
  