Add web backup upload
This commit is contained in:
1 parent
7c640d4338
commit
da902635b7
6 files changed
+161
-1
No files matched your search
@@ -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) .. " байт")
|
||||
@@ -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
|
||||
<button type="submit">Создать бэкап</button>
|
||||
</form>
|
||||
|
||||
<form class="panel span12" method="post" enctype="multipart/form-data" action="$(self_url)?key=$key&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">
|
||||
<h2>Архивы в $(h "$output_dir")</h2>
|
||||
<table><thead><tr><th>Файл</th><th>Размер</th><th>Дата</th><th>Действие</th></tr></thead><tbody>
|
||||
|
||||
Reference in new issue
Block a user