Add async backup progress console
This commit is contained in:
1 parent
575c095e81
commit
ed018c06eb
1 file changed
+175
-3
@@ -6,6 +6,8 @@ PARAMS_FILE="${TMPDIR:-/tmp}/owrt-full-backup-cgi.$$"
|
||||
RESULT_FILE=""
|
||||
RESULT_TITLE=""
|
||||
RESULT_CODE=""
|
||||
JOB_DIR="${JOB_DIR:-/tmp/owrt-full-backup-jobs}"
|
||||
ACTIVE_JOB=""
|
||||
PROJECT_URL="${PROJECT_URL:-https://github.com/kzolotarev95/luci-app-owrt-full-backup}"
|
||||
RAW_URL="${RAW_URL:-https://raw.githubusercontent.com/kzolotarev95/luci-app-owrt-full-backup/main}"
|
||||
|
||||
@@ -121,6 +123,21 @@ valid_abs_path() {
|
||||
printf '%s' "$1" | grep -Eq '^/[A-Za-z0-9._/-]+$'
|
||||
}
|
||||
|
||||
valid_job_id() {
|
||||
case "$1" in
|
||||
''|*[!A-Za-z0-9._-]*) return 1 ;;
|
||||
*) return 0 ;;
|
||||
esac
|
||||
}
|
||||
|
||||
shell_quote() {
|
||||
printf "'%s'" "$(printf '%s' "$1" | sed "s/'/'\\\\''/g")"
|
||||
}
|
||||
|
||||
job_file() {
|
||||
printf '%s/%s.%s' "$JOB_DIR" "$1" "$2"
|
||||
}
|
||||
|
||||
download_file() {
|
||||
local file base
|
||||
file="$(param file)"
|
||||
@@ -177,12 +194,14 @@ handle_delete_backup() {
|
||||
esac
|
||||
|
||||
if [ ! -f "$file" ]; then
|
||||
RESULT_TITLE="Бэкап уже удален"
|
||||
RESULT_CODE=0
|
||||
printf 'Архив уже удален или его нет в папке: %s\n' "$file" >"$RESULT_FILE"
|
||||
return
|
||||
fi
|
||||
|
||||
if rm -f "$file" "$file.sha256" 2>"$RESULT_FILE"; then
|
||||
RESULT_TITLE="Бэкап удален"
|
||||
RESULT_CODE=0
|
||||
printf 'Удалено: %s\n' "$file" >"$RESULT_FILE"
|
||||
else
|
||||
@@ -205,6 +224,67 @@ handle_upload_backup() {
|
||||
RESULT_CODE="$?"
|
||||
}
|
||||
|
||||
start_backend_job() {
|
||||
local title id started log status meta script cmd arg
|
||||
title="$1"
|
||||
shift
|
||||
|
||||
if [ ! -x "$BACKEND" ]; then
|
||||
RESULT_TITLE="$title"
|
||||
RESULT_CODE=127
|
||||
RESULT_FILE="${TMPDIR:-/tmp}/owrt-full-backup-result.$$"
|
||||
printf 'Backend не установлен: %s\n' "$BACKEND" >"$RESULT_FILE"
|
||||
return
|
||||
fi
|
||||
|
||||
mkdir -p "$JOB_DIR"
|
||||
id="$(date +%s)-$$"
|
||||
started="$(date +%s)"
|
||||
log="$(job_file "$id" log)"
|
||||
status="$(job_file "$id" status)"
|
||||
meta="$(job_file "$id" meta)"
|
||||
script="$(job_file "$id" sh)"
|
||||
|
||||
ACTIVE_JOB="$id"
|
||||
printf 'running\n' >"$status"
|
||||
{
|
||||
printf 'title=%s\n' "$title"
|
||||
printf 'started=%s\n' "$started"
|
||||
} >"$meta"
|
||||
|
||||
cmd="$(shell_quote "$BACKEND")"
|
||||
for arg in "$@"; do
|
||||
cmd="$cmd $(shell_quote "$arg")"
|
||||
done
|
||||
|
||||
cat >"$script" <<EOF
|
||||
#!/bin/sh
|
||||
echo "Старт: \$(date)"
|
||||
echo "Создание бэкапа запущено."
|
||||
$cmd
|
||||
code="\$?"
|
||||
echo "Код выхода: \$code"
|
||||
echo "\$code" > "$(job_file "$id" code)"
|
||||
date +%s > "$(job_file "$id" finished)"
|
||||
if [ "\$code" = "0" ]; then
|
||||
echo done > "$status"
|
||||
else
|
||||
echo failed > "$status"
|
||||
fi
|
||||
rm -f "$script"
|
||||
EOF
|
||||
chmod 0700 "$script"
|
||||
if command -v start-stop-daemon >/dev/null 2>&1 && start-stop-daemon -S -b -x /bin/sh -- "$script" >"$log" 2>&1; then
|
||||
:
|
||||
elif command -v setsid >/dev/null 2>&1; then
|
||||
setsid sh "$script" >"$log" 2>&1 </dev/null &
|
||||
elif command -v nohup >/dev/null 2>&1; then
|
||||
nohup sh "$script" >"$log" 2>&1 </dev/null &
|
||||
else
|
||||
sh "$script" >"$log" 2>&1 </dev/null &
|
||||
fi
|
||||
}
|
||||
|
||||
run_backend() {
|
||||
RESULT_TITLE="$1"
|
||||
shift
|
||||
@@ -238,7 +318,7 @@ handle_create() {
|
||||
has_param include_rom && set -- "$@" --include-rom
|
||||
has_param include_raw_mtd && set -- "$@" --include-raw-mtd
|
||||
[ -n "$firmware" ] && set -- "$@" --firmware-image "$firmware"
|
||||
run_backend "Создание бэкапа" "$@"
|
||||
start_backend_job "Создание бэкапа" "$@"
|
||||
}
|
||||
|
||||
handle_inspect() {
|
||||
@@ -382,14 +462,87 @@ backup_rows() {
|
||||
done
|
||||
}
|
||||
|
||||
render_job_panel() {
|
||||
local id status meta log code_file started now elapsed title state_class state_text job_note button_text
|
||||
id="$1"
|
||||
valid_job_id "$id" || return
|
||||
|
||||
status="$(cat "$(job_file "$id" status)" 2>/dev/null || printf 'missing')"
|
||||
meta="$(job_file "$id" meta)"
|
||||
log="$(job_file "$id" log)"
|
||||
code_file="$(job_file "$id" code)"
|
||||
started="$(sed -n 's/^started=//p' "$meta" 2>/dev/null | sed -n '1p')"
|
||||
title="$(sed -n 's/^title=//p' "$meta" 2>/dev/null | sed -n '1p')"
|
||||
[ -n "$title" ] || title="Создание бэкапа"
|
||||
case "$started" in ''|*[!0-9]*) started="$(date +%s)" ;; esac
|
||||
now="$(date +%s)"
|
||||
elapsed="$((now - started))"
|
||||
|
||||
case "$status" in
|
||||
running)
|
||||
state_class="running"
|
||||
state_text="Выполняется"
|
||||
job_note="Страница обновляется каждые 2 секунды. После завершения список архивов ниже обновится автоматически."
|
||||
button_text="Обновить лог"
|
||||
;;
|
||||
done)
|
||||
state_class="ok"
|
||||
state_text="Завершено"
|
||||
job_note="Готово. Список архивов ниже уже обновлен."
|
||||
button_text="Обновить список"
|
||||
;;
|
||||
failed)
|
||||
state_class="error"
|
||||
state_text="Ошибка"
|
||||
job_note="Создание бэкапа завершилось с ошибкой. Подробности в журнале ниже."
|
||||
button_text="Обновить список"
|
||||
;;
|
||||
*)
|
||||
state_class="error"
|
||||
state_text="Задача не найдена"
|
||||
job_note="Эта задача уже недоступна. Можно обновить страницу и посмотреть список архивов."
|
||||
button_text="Обновить список"
|
||||
;;
|
||||
esac
|
||||
|
||||
printf '<section class="panel span12 job-card %s">' "$state_class"
|
||||
printf '<div class="section-head"><div><h2>%s</h2><p>Статус: %s · Время: <span id="job-timer" data-start="%s">%s сек</span>' "$(h "$title")" "$(h "$state_text")" "$(h "$started")" "$(h "$elapsed")"
|
||||
if [ -r "$code_file" ]; then
|
||||
printf ' · Код выхода: %s' "$(h "$(cat "$code_file")")"
|
||||
fi
|
||||
printf '</p></div><a class="button small" href="%s?key=%s">%s</a></div>' "$(self_url)" "$(h "$(param key)")" "$button_text"
|
||||
printf '<p>%s</p><div class="console-title">Журнал выполнения</div>' "$(h "$job_note")"
|
||||
printf '<pre class="console">'
|
||||
if [ -r "$log" ]; then
|
||||
tail -n 120 "$log" | html_escape
|
||||
else
|
||||
printf 'Лог пока пуст. Подожди пару секунд.\n' | html_escape
|
||||
fi
|
||||
printf '</pre>'
|
||||
printf '<script>(function(){var el=document.getElementById("job-timer");if(!el)return;var start=parseInt(el.dataset.start,10)||0;function tick(){var now=Math.floor(Date.now()/1000);el.textContent=Math.max(0,now-start)+" сек";}tick();setInterval(tick,1000);}());</script>'
|
||||
printf '</section>'
|
||||
}
|
||||
|
||||
page() {
|
||||
local key output_dir include_overlay include_rom include_raw_mtd keep_days
|
||||
local key output_dir include_overlay include_rom include_raw_mtd keep_days job_id job_status refresh_meta result_class result_status
|
||||
key="$(h "$(param key)")"
|
||||
output_dir="$(config_get output_dir /tmp)"
|
||||
include_overlay="$(config_get include_overlay 1)"
|
||||
include_rom="$(config_get include_rom 0)"
|
||||
include_raw_mtd="$(config_get include_raw_mtd 0)"
|
||||
keep_days="$(config_get keep_days 0)"
|
||||
job_id="$ACTIVE_JOB"
|
||||
if [ -z "$job_id" ] && valid_job_id "$(param job)"; then
|
||||
job_id="$(param job)"
|
||||
fi
|
||||
job_status=""
|
||||
refresh_meta=""
|
||||
if [ -n "$job_id" ] && valid_job_id "$job_id"; then
|
||||
job_status="$(cat "$(job_file "$job_id" status)" 2>/dev/null || true)"
|
||||
if [ "$job_status" = "running" ]; then
|
||||
refresh_meta="<meta http-equiv=\"refresh\" content=\"2;url=$(self_url)?key=$key&job=$job_id\">"
|
||||
fi
|
||||
fi
|
||||
|
||||
http_html
|
||||
cat <<EOF
|
||||
@@ -398,6 +551,7 @@ page() {
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
$refresh_meta
|
||||
<title>OpenWrt Full Backup</title>
|
||||
<style>
|
||||
:root { color-scheme: light dark; --bg: #f6f7f9; --panel: #fff; --text: #15181c; --muted: #667085; --line: #d9dee7; --accent: #1769e0; --bad: #b42318; --ok: #067647; }
|
||||
@@ -427,6 +581,13 @@ button, .button { display: inline-flex; align-items: center; justify-content: ce
|
||||
.danger { background: var(--bad); }
|
||||
.muted { color: var(--muted); }
|
||||
.file-input { width: 100%; border: 1px dashed var(--line); border-radius: 6px; padding: 12px; color: var(--muted); }
|
||||
.result-card, .job-card { border-left: 4px solid var(--accent); }
|
||||
.result-card.ok, .job-card.ok { border-left-color: var(--ok); }
|
||||
.result-card.error, .job-card.error { border-left-color: var(--bad); }
|
||||
.job-card.running { border-left-color: var(--accent); }
|
||||
.result-output, .console { white-space: pre-wrap; overflow-wrap: anywhere; word-break: break-word; }
|
||||
.console { max-height: 340px; }
|
||||
.console-title { margin: 12px 0 6px; font-weight: 700; }
|
||||
code, pre { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
|
||||
pre { overflow: auto; background: rgba(127,127,127,.12); border-radius: 6px; padding: 10px; }
|
||||
table { width: 100%; border-collapse: collapse; }
|
||||
@@ -453,11 +614,22 @@ td, th { border-bottom: 1px solid var(--line); padding: 8px 6px; text-align: lef
|
||||
EOF
|
||||
|
||||
if [ -n "$RESULT_FILE" ]; then
|
||||
printf '<section class="panel span12"><div class="section-head"><div><h2>%s</h2><p>Код выхода: %s</p></div><a class="button small" href="%s?key=%s">Скрыть сообщение</a></div><pre>' "$(h "$RESULT_TITLE")" "$(h "$RESULT_CODE")" "$(self_url)" "$key"
|
||||
if [ "$RESULT_CODE" = "0" ]; then
|
||||
result_class="ok"
|
||||
result_status="Готово"
|
||||
else
|
||||
result_class="error"
|
||||
result_status="Ошибка, код выхода: $RESULT_CODE"
|
||||
fi
|
||||
printf '<section class="panel span12 result-card %s"><div class="section-head"><div><h2>%s</h2><p>%s</p></div><a class="button small" href="%s?key=%s">Скрыть сообщение</a></div><pre class="result-output">' "$result_class" "$(h "$RESULT_TITLE")" "$(h "$result_status")" "$(self_url)" "$key"
|
||||
html_escape <"$RESULT_FILE"
|
||||
printf '</pre></section>'
|
||||
fi
|
||||
|
||||
if [ -n "$job_id" ]; then
|
||||
render_job_panel "$job_id"
|
||||
fi
|
||||
|
||||
cat <<EOF
|
||||
<section class="grid">
|
||||
<div class="panel span4">
|
||||
|
||||
Reference in new issue
Block a user