2022-04-29 16:51:37 +08:00
|
|
|
--Copyright: https://github.com/coolsnowwolf/luci/tree/master/applications/luci-app-filetransfer
|
|
|
|
|
--Extended support: https://github.com/ophub/luci-app-amlogic
|
|
|
|
|
--Function: Download files
|
|
|
|
|
|
|
|
|
|
local io = require "io"
|
|
|
|
|
local os = require "os"
|
|
|
|
|
local fs = require "nixio.fs"
|
2022-07-25 20:19:04 +08:00
|
|
|
local b, c, x
|
2022-04-19 07:42:06 +08:00
|
|
|
|
2022-04-29 16:51:37 +08:00
|
|
|
-- Checks wheather the given path exists and points to a directory.
|
|
|
|
|
function isdirectory(dirname)
|
|
|
|
|
return fs.stat(dirname, "type") == "dir"
|
|
|
|
|
end
|
|
|
|
|
|
2022-07-25 20:19:04 +08:00
|
|
|
-- Check if a file or directory exists
|
|
|
|
|
function file_exists(path)
|
|
|
|
|
local file = io.open(path, "rb")
|
|
|
|
|
if file then file:close() end
|
|
|
|
|
return file ~= nil
|
|
|
|
|
end
|
|
|
|
|
|
2022-04-19 07:42:06 +08:00
|
|
|
--SimpleForm for Backup Config
|
|
|
|
|
b = SimpleForm("backup", nil)
|
|
|
|
|
b.title = translate("Backup Firmware Config")
|
|
|
|
|
b.description = translate("Backup OpenWrt config (openwrt_config.tar.gz). Use this file to restore the config in [Manually Upload Update].")
|
|
|
|
|
b.reset = false
|
|
|
|
|
b.submit = false
|
|
|
|
|
|
|
|
|
|
s = b:section(SimpleSection, "", "")
|
|
|
|
|
|
2023-07-04 14:01:23 +08:00
|
|
|
-- Button for customize backup list
|
|
|
|
|
my = s:option(Button, "customize", translate("Edit List:"))
|
|
|
|
|
my.template = "amlogic/other_button"
|
|
|
|
|
|
|
|
|
|
my.render = function(self, section, scope)
|
|
|
|
|
self.section = true
|
|
|
|
|
scope.display = ""
|
2023-07-04 20:09:57 +08:00
|
|
|
self.inputtitle = translate("Open List")
|
2023-07-04 14:01:23 +08:00
|
|
|
self.inputstyle = "save"
|
|
|
|
|
Button.render(self, section, scope)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
my.write = function(self, section, scope)
|
|
|
|
|
local handle = io.popen("[[ -s /etc/amlogic_backup_list.conf ]] || sed -n \"/BACKUP_LIST='/,/.*'$/p\" /usr/sbin/openwrt-backup | tr -d \"BACKUP_LIST=|'\" >/etc/amlogic_backup_list.conf 2>/dev/null")
|
|
|
|
|
local result = handle:read("*a")
|
|
|
|
|
handle:close()
|
|
|
|
|
luci.http.redirect(luci.dispatcher.build_url("admin", "system", "amlogic", "backuplist"))
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Button for download backup config
|
|
|
|
|
o = s:option(Button, "download", translate("Backup Config:"))
|
2022-04-19 07:42:06 +08:00
|
|
|
o.template = "amlogic/other_button"
|
|
|
|
|
|
|
|
|
|
um = s:option(DummyValue, "", nil)
|
|
|
|
|
um.template = "amlogic/other_dvalue"
|
|
|
|
|
|
|
|
|
|
o.render = function(self, section, scope)
|
|
|
|
|
self.section = true
|
|
|
|
|
scope.display = ""
|
|
|
|
|
self.inputtitle = translate("Download Backup")
|
|
|
|
|
self.inputstyle = "save"
|
|
|
|
|
Button.render(self, section, scope)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
o.write = function(self, section, scope)
|
|
|
|
|
local x = luci.sys.exec("chmod +x /usr/sbin/openwrt-backup 2>/dev/null")
|
|
|
|
|
local r = luci.sys.exec("/usr/sbin/openwrt-backup -b > /tmp/amlogic/amlogic.log && sync 2>/dev/null")
|
|
|
|
|
|
|
|
|
|
local sPath, sFile, fd, block
|
|
|
|
|
sPath = "/.reserved/openwrt_config.tar.gz"
|
2022-04-29 16:51:37 +08:00
|
|
|
sFile = fs.basename(sPath)
|
|
|
|
|
if isdirectory(sPath) then
|
|
|
|
|
fd = io.popen('tar -C "%s" -cz .' % { sPath }, "r")
|
2022-04-19 07:42:06 +08:00
|
|
|
sFile = sFile .. ".tar.gz"
|
|
|
|
|
else
|
|
|
|
|
fd = nixio.open(sPath, "r")
|
|
|
|
|
end
|
|
|
|
|
if not fd then
|
|
|
|
|
um.value = translate("Couldn't open file:") .. sPath
|
|
|
|
|
return
|
|
|
|
|
else
|
|
|
|
|
um.value = translate("The file Will download automatically.") .. sPath
|
|
|
|
|
end
|
|
|
|
|
|
2022-04-29 16:51:37 +08:00
|
|
|
luci.http.header('Content-Disposition', 'attachment; filename="%s"' % { sFile })
|
|
|
|
|
luci.http.prepare_content("application/octet-stream")
|
2022-04-19 07:42:06 +08:00
|
|
|
while true do
|
|
|
|
|
block = fd:read(nixio.const.buffersize)
|
2022-04-29 16:51:37 +08:00
|
|
|
if (not block) or (#block == 0) then
|
2022-04-19 07:42:06 +08:00
|
|
|
break
|
|
|
|
|
else
|
2022-04-29 16:51:37 +08:00
|
|
|
luci.http.write(block)
|
2022-04-19 07:42:06 +08:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
fd:close()
|
2022-04-29 16:51:37 +08:00
|
|
|
luci.http.close()
|
2022-04-19 07:42:06 +08:00
|
|
|
end
|
|
|
|
|
|
2023-07-04 14:01:23 +08:00
|
|
|
-- Button for restore backup list
|
|
|
|
|
r = s:option(Button, "restore", translate("Restore Backup:"))
|
|
|
|
|
r.template = "amlogic/other_button"
|
|
|
|
|
|
|
|
|
|
r.render = function(self, section, scope)
|
|
|
|
|
self.section = true
|
|
|
|
|
scope.display = ""
|
|
|
|
|
self.inputtitle = translate("Upload Backup")
|
|
|
|
|
self.inputstyle = "save"
|
|
|
|
|
Button.render(self, section, scope)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
r.write = function(self, section, scope)
|
|
|
|
|
luci.http.redirect(luci.dispatcher.build_url("admin", "system", "amlogic", "upload"))
|
|
|
|
|
end
|
|
|
|
|
|
2022-04-19 07:42:06 +08:00
|
|
|
-- SimpleForm for Create Snapshot
|
|
|
|
|
c = SimpleForm("snapshot", nil)
|
|
|
|
|
c.title = translate("Snapshot Management")
|
|
|
|
|
c.description = translate("Create a snapshot of the current system configuration, or restore to a snapshot.")
|
|
|
|
|
c.reset = false
|
|
|
|
|
c.submit = false
|
|
|
|
|
|
|
|
|
|
d = c:section(SimpleSection, "", nil)
|
|
|
|
|
|
2023-07-04 14:01:23 +08:00
|
|
|
w = d:option(Button, "create_snapshot", "")
|
2022-04-19 07:42:06 +08:00
|
|
|
w.template = "amlogic/other_button"
|
|
|
|
|
w.render = function(self, section, scope)
|
|
|
|
|
self.section = true
|
|
|
|
|
scope.display = ""
|
|
|
|
|
self.inputtitle = translate("Create Snapshot")
|
|
|
|
|
self.inputstyle = "save"
|
|
|
|
|
Button.render(self, section, scope)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
w.write = function(self, section, scope)
|
2022-07-28 20:21:20 +08:00
|
|
|
local x = luci.sys.exec("btrfs subvolume snapshot -r /etc /.snapshots/etc-" .. os.date("%m.%d.%H%M%S") .. " 2>/dev/null && sync")
|
2022-04-29 16:51:37 +08:00
|
|
|
luci.http.redirect(luci.dispatcher.build_url("admin", "system", "amlogic", "backup"))
|
2022-04-19 07:42:06 +08:00
|
|
|
end
|
|
|
|
|
w = d:option(TextValue, "snapshot_list", nil)
|
|
|
|
|
w.template = "amlogic/other_snapshot"
|
|
|
|
|
|
2022-07-25 20:19:04 +08:00
|
|
|
--KVM virtual machine switching dual partition
|
|
|
|
|
if file_exists("/boot/efi/EFI/BOOT/grub.cfg.prev") then
|
2023-07-04 14:01:23 +08:00
|
|
|
x = SimpleForm("kvm", nil)
|
|
|
|
|
x.title = translate("KVM dual system switching")
|
2022-07-25 20:19:04 +08:00
|
|
|
x.description = translate("You can freely switch between KVM dual partitions, using OpenWrt systems in different partitions.")
|
2023-07-04 14:01:23 +08:00
|
|
|
x.reset = false
|
|
|
|
|
x.submit = false
|
2022-07-25 20:19:04 +08:00
|
|
|
|
|
|
|
|
x:section(SimpleSection).template = "amlogic/other_kvm"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return b, c, x
|