Add web backup upload

This commit is contained in:
kzolotarev95 committed 2026-06-28 17:34:57 +04:00
1 parent 7c640d4338
commit da902635b7
6 files changed
+161 -1

No files matched your search

+1
View File
@@ -9,6 +9,7 @@ Makefile text eol=lf
install.sh text eol=lf install.sh text eol=lf
uninstall.sh text eol=lf uninstall.sh text eol=lf
files/usr/sbin/owrt-full-backup text eol=lf files/usr/sbin/owrt-full-backup text eol=lf
files/usr/sbin/owrt-full-backup-upload text eol=lf
files/etc/config/fullbackup text eol=lf files/etc/config/fullbackup text eol=lf
files/www/cgi-bin/owrt-full-backup text eol=lf files/www/cgi-bin/owrt-full-backup text eol=lf
files/usr/lib/lua/luci/controller/owrt_full_backup.lua text eol=lf files/usr/lib/lua/luci/controller/owrt_full_backup.lua text eol=lf
+2
View File
@@ -7,6 +7,7 @@
## Что устанавливается ## Что устанавливается
- `/usr/sbin/owrt-full-backup` - консольная команда; - `/usr/sbin/owrt-full-backup` - консольная команда;
- `/usr/sbin/owrt-full-backup-upload` - загрузка архивов из веб-панели;
- `/www/cgi-bin/owrt-full-backup` - веб-панель; - `/www/cgi-bin/owrt-full-backup` - веб-панель;
- `/usr/lib/lua/luci/controller/owrt_full_backup.lua` - пункт меню LuCI; - `/usr/lib/lua/luci/controller/owrt_full_backup.lua` - пункт меню LuCI;
- `/etc/config/fullbackup` - настройки по умолчанию; - `/etc/config/fullbackup` - настройки по умолчанию;
@@ -55,6 +56,7 @@ http://192.168.1.1/cgi-bin/owrt-full-backup?key=SECRET
## Возможности веб-панели ## Возможности веб-панели
- создать полный архив бэкапа; - создать полный архив бэкапа;
- загрузить архив бэкапа с компьютера на роутер;
- скачать готовый архив; - скачать готовый архив;
- посмотреть содержимое/метаданные архива; - посмотреть содержимое/метаданные архива;
- восстановить настройки; - восстановить настройки;
+119
View File
@@ -0,0 +1,119 @@
#!/usr/bin/lua
local default_dir = arg[1] or "/tmp"
local content_type = os.getenv("CONTENT_TYPE") or ""
local content_length = tonumber(os.getenv("CONTENT_LENGTH") or "0") or 0
local function fail(message)
io.stderr:write("ОШИБКА: " .. message .. "\n")
os.exit(1)
end
local function ok(message)
io.stdout:write(message .. "\n")
end
local function valid_path(path)
return type(path) == "string" and path:match("^/[A-Za-z0-9%._/%-]+$") ~= nil
end
local function shell_quote(value)
return "'" .. tostring(value):gsub("'", "'\\''") .. "'"
end
local function safe_filename(name)
name = tostring(name or ""):gsub("\\", "/")
name = name:match("([^/]+)$") or ""
name = name:gsub("[^A-Za-z0-9%._%-]", "_")
return name
end
local boundary = content_type:match('boundary="?([^";]+)"?')
if not boundary or boundary == "" then
fail("multipart boundary не найден")
end
if content_length <= 0 then
fail("пустой upload")
end
local body = io.stdin:read(content_length)
if not body or #body ~= content_length then
fail("не удалось прочитать upload полностью")
end
local upload_dir = default_dir
local file_name
local file_data
local marker = "--" .. boundary
local pos = 1
while true do
local boundary_start, boundary_end = body:find(marker, pos, true)
if not boundary_start then
break
end
local after = boundary_end + 1
if body:sub(after, after + 1) == "--" then
break
end
if body:sub(after, after + 1) == "\r\n" then
after = after + 2
end
local header_end = body:find("\r\n\r\n", after, true)
if not header_end then
break
end
local headers = body:sub(after, header_end - 1)
local data_start = header_end + 4
local next_boundary = body:find("\r\n" .. marker, data_start, true)
if not next_boundary then
break
end
local data = body:sub(data_start, next_boundary - 1)
local name = headers:match('name="([^"]+)"')
local filename = headers:match('filename="([^"]*)"')
if name == "upload_dir" and filename == nil then
upload_dir = data:gsub("[%z\r\n]", "")
elseif name == "archive" and filename and filename ~= "" then
file_name = safe_filename(filename)
file_data = data
end
pos = next_boundary + 2
end
if not valid_path(upload_dir) then
fail("неверная папка для загрузки: " .. tostring(upload_dir))
end
if not file_name or file_name == "" then
fail("файл архива не выбран")
end
if not file_name:match("%.tar%.gz$") then
fail("можно загружать только архивы .tar.gz")
end
if not file_data or #file_data == 0 then
fail("файл пустой")
end
os.execute("mkdir -p " .. shell_quote(upload_dir))
local target = upload_dir:gsub("/+$", "") .. "/" .. file_name
local out = io.open(target, "wb")
if not out then
fail("не удалось открыть файл для записи: " .. target)
end
out:write(file_data)
out:close()
ok("Архив загружен: " .. target)
ok("Размер: " .. tostring(#file_data) .. " байт")
+37 -1
View File
@@ -60,6 +60,9 @@ read_request() {
local post_data local post_data
: >"$PARAMS_FILE" : >"$PARAMS_FILE"
parse_params "${QUERY_STRING:-}" parse_params "${QUERY_STRING:-}"
case "${CONTENT_TYPE:-}" in
multipart/form-data*) return ;;
esac
if [ "${REQUEST_METHOD:-GET}" = "POST" ] && [ -n "${CONTENT_LENGTH:-}" ]; then if [ "${REQUEST_METHOD:-GET}" = "POST" ] && [ -n "${CONTENT_LENGTH:-}" ]; then
post_data="$(dd bs=1 count="$CONTENT_LENGTH" 2>/dev/null)" post_data="$(dd bs=1 count="$CONTENT_LENGTH" 2>/dev/null)"
parse_params "$post_data" parse_params "$post_data"
@@ -188,6 +191,20 @@ handle_delete_backup() {
fi fi
} }
handle_upload_backup() {
RESULT_TITLE="Загрузка архива"
RESULT_FILE="${TMPDIR:-/tmp}/owrt-full-backup-result.$$"
if ! command -v owrt-full-backup-upload >/dev/null 2>&1; then
RESULT_CODE=127
printf 'Помощник загрузки не установлен: owrt-full-backup-upload\n' >"$RESULT_FILE"
return
fi
owrt-full-backup-upload "$(config_get output_dir /tmp)" >"$RESULT_FILE" 2>&1
RESULT_CODE="$?"
}
run_backend() { run_backend() {
RESULT_TITLE="$1" RESULT_TITLE="$1"
shift shift
@@ -308,12 +325,23 @@ handle_action() {
restore) handle_restore ;; restore) handle_restore ;;
save_defaults) handle_save_defaults ;; save_defaults) handle_save_defaults ;;
delete_backup) handle_delete_backup ;; delete_backup) handle_delete_backup ;;
upload_backup) handle_upload_backup ;;
esac esac
} }
system_value() { system_value() {
local value
case "$1" in case "$1" in
host) hostname 2>/dev/null || printf 'openwrt' ;; host)
if command -v uci >/dev/null 2>&1; then
value="$(uci -q get system.@system[0].hostname 2>/dev/null || true)"
[ -n "$value" ] && {
printf '%s' "$value"
return
}
fi
hostname 2>/dev/null || printf 'openwrt'
;;
release) release)
if [ -r /etc/openwrt_release ]; then if [ -r /etc/openwrt_release ]; then
. /etc/openwrt_release 2>/dev/null . /etc/openwrt_release 2>/dev/null
@@ -398,6 +426,7 @@ button, .button { display: inline-flex; align-items: center; justify-content: ce
.small { min-height: 30px; padding: 6px 9px; font-size: 12px; } .small { min-height: 30px; padding: 6px 9px; font-size: 12px; }
.danger { background: var(--bad); } .danger { background: var(--bad); }
.muted { color: var(--muted); } .muted { color: var(--muted); }
.file-input { width: 100%; border: 1px dashed var(--line); border-radius: 6px; padding: 12px; color: var(--muted); }
code, pre { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; } 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; } pre { overflow: auto; background: rgba(127,127,127,.12); border-radius: 6px; padding: 10px; }
table { width: 100%; border-collapse: collapse; } table { width: 100%; border-collapse: collapse; }
@@ -458,6 +487,13 @@ EOF
<button type="submit">Создать бэкап</button> <button type="submit">Создать бэкап</button>
</form> </form>
<form class="panel span12" method="post" enctype="multipart/form-data" action="$(self_url)?key=$key&amp;action=upload_backup">
<h2>Загрузить архив бэкапа</h2>
<p>Выбери архив <code>.tar.gz</code> с компьютера. Он будет сохранен в $(h "$output_dir") и появится в списке архивов.</p>
<input class="file-input" type="file" name="archive" accept=".tar.gz,application/gzip,application/x-gzip">
<button type="submit">Загрузить архив</button>
</form>
<div class="panel span12"> <div class="panel span12">
<h2>Архивы в $(h "$output_dir")</h2> <h2>Архивы в $(h "$output_dir")</h2>
<table><thead><tr><th>Файл</th><th>Размер</th><th>Дата</th><th>Действие</th></tr></thead><tbody> <table><thead><tr><th>Файл</th><th>Размер</th><th>Дата</th><th>Действие</th></tr></thead><tbody>
+1
View File
@@ -102,6 +102,7 @@ router_ip() {
} }
install_file "usr/sbin/owrt-full-backup" 0755 install_file "usr/sbin/owrt-full-backup" 0755
install_file "usr/sbin/owrt-full-backup-upload" 0755
ln -sf owrt-full-backup "$(target_path usr/sbin/owrt-backup)" ln -sf owrt-full-backup "$(target_path usr/sbin/owrt-backup)"
install_config install_config
install_file "www/cgi-bin/owrt-full-backup" 0755 install_file "www/cgi-bin/owrt-full-backup" 0755
+1
View File
@@ -14,6 +14,7 @@ rmf() {
} }
rmf usr/sbin/owrt-full-backup rmf usr/sbin/owrt-full-backup
rmf usr/sbin/owrt-full-backup-upload
rmf usr/sbin/owrt-backup rmf usr/sbin/owrt-backup
rmf www/cgi-bin/owrt-full-backup rmf www/cgi-bin/owrt-full-backup
rmf usr/lib/lua/luci/controller/owrt_full_backup.lua rmf usr/lib/lua/luci/controller/owrt_full_backup.lua