diff --git a/.gitattributes b/.gitattributes index 88d5837..bb73904 100644 --- a/.gitattributes +++ b/.gitattributes @@ -9,6 +9,7 @@ Makefile text eol=lf install.sh text eol=lf uninstall.sh 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/www/cgi-bin/owrt-full-backup text eol=lf files/usr/lib/lua/luci/controller/owrt_full_backup.lua text eol=lf diff --git a/README.md b/README.md index a4c5a6f..4b0f12b 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ ## Что устанавливается - `/usr/sbin/owrt-full-backup` - консольная команда; +- `/usr/sbin/owrt-full-backup-upload` - загрузка архивов из веб-панели; - `/www/cgi-bin/owrt-full-backup` - веб-панель; - `/usr/lib/lua/luci/controller/owrt_full_backup.lua` - пункт меню LuCI; - `/etc/config/fullbackup` - настройки по умолчанию; @@ -55,6 +56,7 @@ http://192.168.1.1/cgi-bin/owrt-full-backup?key=SECRET ## Возможности веб-панели - создать полный архив бэкапа; +- загрузить архив бэкапа с компьютера на роутер; - скачать готовый архив; - посмотреть содержимое/метаданные архива; - восстановить настройки; diff --git a/files/usr/sbin/owrt-full-backup-upload b/files/usr/sbin/owrt-full-backup-upload new file mode 100644 index 0000000..c8c41fb --- /dev/null +++ b/files/usr/sbin/owrt-full-backup-upload @@ -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) .. " байт") diff --git a/files/www/cgi-bin/owrt-full-backup b/files/www/cgi-bin/owrt-full-backup index 660e3ee..b97e9da 100644 --- a/files/www/cgi-bin/owrt-full-backup +++ b/files/www/cgi-bin/owrt-full-backup @@ -60,6 +60,9 @@ read_request() { local post_data : >"$PARAMS_FILE" parse_params "${QUERY_STRING:-}" + case "${CONTENT_TYPE:-}" in + multipart/form-data*) return ;; + esac if [ "${REQUEST_METHOD:-GET}" = "POST" ] && [ -n "${CONTENT_LENGTH:-}" ]; then post_data="$(dd bs=1 count="$CONTENT_LENGTH" 2>/dev/null)" parse_params "$post_data" @@ -188,6 +191,20 @@ handle_delete_backup() { 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() { RESULT_TITLE="$1" shift @@ -308,12 +325,23 @@ handle_action() { restore) handle_restore ;; save_defaults) handle_save_defaults ;; delete_backup) handle_delete_backup ;; + upload_backup) handle_upload_backup ;; esac } system_value() { + local value 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) if [ -r /etc/openwrt_release ]; then . /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; } .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); } 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; } @@ -458,6 +487,13 @@ EOF +
+| Файл | Размер | Дата | Действие |
|---|