Skip to content

Commit

Permalink
Add backup options (#2572)
Browse files Browse the repository at this point in the history
* Add backup options

this initial commit for #1999 , #2046 and #2563 issues

* Fix optional backup until can be restored through Config Manage as Backup File.

This fix will be applicable to restore from Config Manage page as "Backup File"

* add translations
  • Loading branch information
helmiau committed Jul 27, 2022
1 parent c88d03c commit 3538710
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 6 deletions.
70 changes: 70 additions & 0 deletions luci-app-openclash/luasrc/controller/openclash.lua
Expand Up @@ -39,6 +39,11 @@ function index()
entry({"admin", "services", "openclash", "write_netflix_domains"}, call("action_write_netflix_domains"))
entry({"admin", "services", "openclash", "restore"}, call("action_restore_config"))
entry({"admin", "services", "openclash", "backup"}, call("action_backup"))
entry({"admin", "services", "openclash", "backup_ex_core"}, call("action_backup_ex_core"))
entry({"admin", "services", "openclash", "backup_only_core"}, call("action_backup_only_core"))
entry({"admin", "services", "openclash", "backup_only_config"}, call("action_backup_only_config"))
entry({"admin", "services", "openclash", "backup_only_rule"}, call("action_backup_only_rule"))
entry({"admin", "services", "openclash", "backup_only_proxy"}, call("action_backup_only_proxy"))
entry({"admin", "services", "openclash", "remove_all_core"}, call("action_remove_all_core"))
entry({"admin", "services", "openclash", "one_key_update"}, call("action_one_key_update"))
entry({"admin", "services", "openclash", "one_key_update_check"}, call("action_one_key_update_check"))
Expand Down Expand Up @@ -1294,6 +1299,71 @@ function action_backup()
luci.ltn12.pump.all(reader, luci.http.write)
end

function action_backup_ex_core()
local config = luci.sys.call("cp /etc/config/openclash /etc/openclash/openclash >/dev/null 2>&1")
local reader = ltn12_popen("echo 'core' > /tmp/oc_exclude.txt && tar -C '/etc/openclash/' -X '/tmp/oc_exclude.txt' -cz . 2>/dev/null")

luci.http.header(
'Content-Disposition', 'attachment; filename="Backup-OpenClash-Exclude-Cores-%s.tar.gz"' %{
os.date("%Y-%m-%d-%H-%M-%S")
})

luci.http.prepare_content("application/x-targz")
luci.ltn12.pump.all(reader, luci.http.write)
end

function action_backup_only_config()
local config = luci.sys.call("cp /etc/config/openclash /etc/openclash/openclash >/dev/null 2>&1")
local reader = ltn12_popen("tar -C '/etc/openclash' -cz './config' 2>/dev/null")

luci.http.header(
'Content-Disposition', 'attachment; filename="Backup-OpenClash-Config-%s.tar.gz"' %{
os.date("%Y-%m-%d-%H-%M-%S")
})

luci.http.prepare_content("application/x-targz")
luci.ltn12.pump.all(reader, luci.http.write)
end

function action_backup_only_core()
local config = luci.sys.call("cp /etc/config/openclash /etc/openclash/openclash >/dev/null 2>&1")
local reader = ltn12_popen("tar -C '/etc/openclash' -cz './core' 2>/dev/null")

luci.http.header(
'Content-Disposition', 'attachment; filename="Backup-OpenClash-Cores-%s.tar.gz"' %{
os.date("%Y-%m-%d-%H-%M-%S")
})

luci.http.prepare_content("application/x-targz")
luci.ltn12.pump.all(reader, luci.http.write)
end

function action_backup_only_rule()
local config = luci.sys.call("cp /etc/config/openclash /etc/openclash/openclash >/dev/null 2>&1")
local reader = ltn12_popen("tar -C '/etc/openclash' -cz './rule_provider' 2>/dev/null")

luci.http.header(
'Content-Disposition', 'attachment; filename="Backup-OpenClash-Only-Rule-Provider-%s.tar.gz"' %{
os.date("%Y-%m-%d-%H-%M-%S")
})

luci.http.prepare_content("application/x-targz")
luci.ltn12.pump.all(reader, luci.http.write)
end

function action_backup_only_proxy()
local config = luci.sys.call("cp /etc/config/openclash /etc/openclash/openclash >/dev/null 2>&1")
local reader = ltn12_popen("tar -C '/etc/openclash' -cz './proxy_provider' 2>/dev/null")

luci.http.header(
'Content-Disposition', 'attachment; filename="Backup-OpenClash-Proxy-Provider-%s.tar.gz"' %{
os.date("%Y-%m-%d-%H-%M-%S")
})

luci.http.prepare_content("application/x-targz")
luci.ltn12.pump.all(reader, luci.http.write)
end

function ltn12_popen(command)

local fdi, fdo = nixio.pipe()
Expand Down
103 changes: 98 additions & 5 deletions luci-app-openclash/luasrc/view/openclash/update.htm
Expand Up @@ -75,11 +75,6 @@
<%:Collecting data...%>
</p>
</td>
<td width="25%">
<p align="center" id="backup">
<%:Collecting data...%>
</p>
</td>
<td width="25%">
<p align="center" id="remove_core">
<%:Collecting data...%>
Expand All @@ -93,6 +88,49 @@
</tr>
</table>
</fieldset>
<fieldset class="cbi-section">
<table width="100%">
<tr><td width="100%" colspan="4">
<p align="center">
<b><%:Backup Section%></b>
</p>
</td></tr>
<tr>
<td width="25%">
<p align="center" id="backup">
<%:Collecting data...%>
</p>
</td>
<td width="25%">
<p align="center" id="backup_ex_core">
<%:Collecting data...%>
</p>
</td>
<td width="25%">
<p align="center" id="backup_core_only">
<%:Collecting data...%>
</p>
</td>
</tr>
<tr>
<td width="25%">
<p align="center" id="backup_config_only">
<%:Collecting data...%>
</p>
</td>
<td width="25%">
<p align="center" id="backup_rule_only">
<%:Collecting data...%>
</p>
</td>
<td width="25%">
<p align="center" id="backup_proxy_only">
<%:Collecting data...%>
</p>
</td>
</tr>
</table>
</fieldset>

<script type="text/javascript">//<![CDATA[
var core_version = document.getElementById('CORE_VERSION');
Expand All @@ -117,6 +155,11 @@
var ma_op_up = document.getElementById('ma_op_up');
var restore = document.getElementById('restore');
var backup = document.getElementById('backup');
var backup_ex_core = document.getElementById('backup_ex_core');
var backup_core_only = document.getElementById('backup_core_only');
var backup_config_only = document.getElementById('backup_config_only');
var backup_rule_only = document.getElementById('backup_rule_only');
var backup_proxy_only = document.getElementById('backup_proxy_only');
var one_key_update = document.getElementById('one_key_update');
var remove_core = document.getElementById('remove_core');
var release_branch = document.getElementById('RELEASE_BRANCH');
Expand All @@ -132,6 +175,11 @@
one_key_update.innerHTML = '<input type="button" class="btn cbi-button cbi-button-reset" value="<%:One Click Check Update%>" onclick="return all_one_key_update(this)"/>';
remove_core.innerHTML = '<input type="button" class="btn cbi-button cbi-button-reset" value="<%:Remove Core%>" onclick="return remove_all_core(this)"/>';
backup.innerHTML = '<input type="button" class="btn cbi-button cbi-button-reset" value="<%:Backup OpenClash%>" onclick="return backup_all_file(this)"/>';
backup_ex_core.innerHTML = '<input type="button" class="btn cbi-button cbi-button-reset" value="<%:Backup Exclude Cores%>" onclick="return backup_no_core(this)"/>';
backup_core_only.innerHTML = '<input type="button" class="btn cbi-button cbi-button-reset" value="<%:Backup Core%>" onclick="return backup_only_core(this)"/>';
backup_config_only.innerHTML = '<input type="button" class="btn cbi-button cbi-button-reset" value="<%:Backup Config%>" onclick="return backup_only_config(this)"/>';
backup_rule_only.innerHTML = '<input type="button" class="btn cbi-button cbi-button-reset" value="<%:Backup Rule Provider%>" onclick="return backup_only_rule(this)"/>';
backup_proxy_only.innerHTML = '<input type="button" class="btn cbi-button cbi-button-reset" value="<%:Backup Proxy Provider%>" onclick="return backup_only_proxy(this)"/>';

XHR.get('<%=luci.dispatcher.build_url("admin", "services", "openclash", "update_info")%>', null, function(x, status) {
if ( x && x.status == 200 ) {
Expand Down Expand Up @@ -410,6 +458,51 @@
return false;
}

function backup_no_core(btn)
{
btn.value = '<%:Backup Exclude Cores%>';
btn.disabled = true;
window.location.href='<%="backup_ex_core"%>';
btn.disabled = false;
return false;
}

function backup_only_core(btn)
{
btn.value = '<%:Backup Core%>';
btn.disabled = true;
window.location.href='<%="backup_only_core"%>';
btn.disabled = false;
return false;
}

function backup_only_config(btn)
{
btn.value = '<%:Backup Config%>';
btn.disabled = true;
window.location.href='<%="backup_only_config"%>';
btn.disabled = false;
return false;
}

function backup_only_rule(btn)
{
btn.value = '<%:Backup Rule Provider%>';
btn.disabled = true;
window.location.href='<%="backup_only_rule"%>';
btn.disabled = false;
return false;
}

function backup_only_proxy(btn)
{
btn.value = '<%:Backup Proxy Provider%>';
btn.disabled = true;
window.location.href='<%="backup_only_proxy"%>';
btn.disabled = false;
return false;
}

function all_one_key_update(btn)
{
var v = core_version.value;
Expand Down
20 changes: 19 additions & 1 deletion luci-app-openclash/po/zh-cn/openclash.zh-cn.po
Expand Up @@ -3077,4 +3077,22 @@ msgid "Hongkong/Macau/Taiwan"
msgstr "港澳台限定"

msgid "Taiwan Only"
msgstr "台湾限定"
msgstr "台湾限定"

msgid "Backup Section"
msgstr "插件备份"

msgid "Backup Exclude Cores"
msgstr "不备份内核"

msgid "Backup Core"
msgstr "仅备份内核"

msgid "Backup Config"
msgstr "仅备份配置文件"

msgid "Backup Rule Provider"
msgstr "仅备份规则集"

msgid "Backup Proxy Provider"
msgstr "仅备份代理集"

0 comments on commit 3538710

Please sign in to comment.