% this file is written in sections so you can take parts you like
% and leave the rest
%
% display page heading, node name, and ISO date and time
out(A) ->
{ok,Hostname} = inet:gethostname(),
NowYMDHMS = erlang:localtime(),
{{YYYY, MM, DD}, {H,M,S}} = NowYMDHMS,
IsoDateTime = io_lib:format(
"~4.10.0b-~2.10.0b-~2.10.0b ~2.10.0b:~2.10.0b:~2.10.0b",
[YYYY, MM, DD, H, M, S]),
{ehtml,[{h1,[],["backup_tables"]},
{pre,[],f("
node: ~s
now: ~s",
[atom_to_list(node()),IsoDateTime])}]}.
% list local tables, making each line a link to the table view
out(A) ->
Q = yaws_api:parse_query(A),
ServerPath=A#arg.server_path,
QryLink=fun(Tab)->
String=atom_to_list(Tab),
Link=ServerPath++"?t="++String,
[{a,[{href,Link}],String},f("\t(~p)\n",[mnesia:table_info(Tab,size)])]
end,
% local tables, excluding schema
LT=lists:sort(lists:delete(schema,mnesia:system_info(local_tables))),
{ehtml,[{h2,[],f("~p local tables",[length(LT)])},
{pre,[],lists:map(QryLink,LT)}
]}.
% display links to other yaws pages in this directory
out(A) ->
Dir=filename:dirname(A#arg.fullpath),
Y = fun(F) -> is_tuple(regexp:match(F,"\\.yaws$")) end,
{ok,FL}=file:list_dir(Dir),
FL2 = lists:filter(Y,FL),
Link=fun(String)->{a,[{href,String}],[String,"\n"]} end,
{ehtml,[{h2,[],"links"},
{pre,[],[lists:map(Link,lists:sort(FL2))]}
]}.
% if there is a query, ?t=tablename, backup that table to mnesia_dir
out(A) ->
Q = yaws_api:parse_query(A),
case Q of
[{_,Table}] ->
Day=calendar:day_of_the_week(date()),
BkName=Table++"-"++integer_to_list(Day),
Tab=list_to_atom(Table),
CPargs=[{name,BkName},{min,[Tab]},{allow_remote,false},{ram_overrides_dump,true}],
Dir=mnesia:system_info(directory),
File=filename:absname_join(Dir,BkName),
mnesia:activate_checkpoint(CPargs),
RBC = (catch mnesia:backup_checkpoint(BkName,File)),
mnesia:deactivate_checkpoint(BkName),
LS=os:cmd("ls -l "++File),
{ehtml,[{h2,[],f("Backup of Table ~s",[Table])},
{pre,[],f("~p\n~s\n",[RBC,LS])}
]};
_ ->
ok
end.