Skip to content

Commit

Permalink
backup:change flag option to checkbox
Browse files Browse the repository at this point in the history
  • Loading branch information
rufengsuixing committed Dec 22, 2019
1 parent 2e3b880 commit 413f1f8
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 34 deletions.
36 changes: 27 additions & 9 deletions luasrc/model/cbi/AdGuardHome/base.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require("luci.sys")
require("luci.util")
require("io")
local m,s,o
local m,s,o,o1
local fs=require"nixio.fs"
local uci=require"luci.model.uci".cursor()
local configpath=uci:get("AdGuardHome","AdGuardHome","configpath") or "/etc/AdGuardHome.yaml"
Expand Down Expand Up @@ -202,16 +202,34 @@ o = s:option(Flag, "waitonboot", translate("Boot delay until network ok"))
o.default = 1
o.optional = true
---- backup workdir on shutdown
o = s:option(Flag, "backupwd", translate("Backup workdir when shutdown"))
o.default = 0
o.optional = true
local workdir=uci:get("AdGuardHome","AdGuardHome","workdir") or "/usr/bin/AdGuardHome"
o = s:option(MultiValue, "backupfile", translate("Backup workdir files when shutdown"))
o1 = s:option(Value, "backupwdpath", translate("Backup workdir path"))
local name
o:value("filters","filters")
o:value("stats.db","stats.db")
o:value("querylog.json","querylog.json")
o1:depends ("backupfile", "filters")
o1:depends ("backupfile", "stats.db")
o1:depends ("backupfile", "querylog.json")
for name in fs.glob(workdir.."/data/*")
do
name=fs.basename (name)
if name~="filters" and name~="stats.db" and name~="querylog.json" then
o:value(name,name)
o1:depends ("backupfile", name)
end
end
o.widget = "checkbox"
o.default = nil
o.optional=false
o.description=translate("Will be restore when workdir/data is empty")
----backup workdir path
o = s:option(Value, "backupwdpath", translate("Backup workdir path"))
o.default = "/usr/bin/AdGuardHome"
o.datatype = "string"
o.optional = true
o.validate=function(self, value)

o1.default = "/usr/bin/AdGuardHome"
o1.datatype = "string"
o1.optional = false
o1.validate=function(self, value)
if fs.stat(value,"type")=="reg" then
if m.message then
m.message =m.message.."\nerror!backup dir is a file"
Expand Down
75 changes: 50 additions & 25 deletions root/etc/init.d/AdGuardHome
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ STOP=01

CONFIGURATION=AdGuardHome
CRON_FILE=/etc/crontabs/root
EXTRA_COMMANDS="do_redirect"
EXTRA_HELP=" do_redirect 0 or 1"
EXTRA_COMMANDS="do_redirect testbackup"
EXTRA_HELP=" do_redirect 0 or 1\
testbackup backup or restore"
set_forward_dnsmasq()
{
local PORT="$1"
Expand Down Expand Up @@ -328,6 +329,47 @@ boot_service() {
fi
fi
}
testbackup(){
config_load "${CONFIGURATION}"
if [ "$1" == "backup" ]; then
backup
elif [ "$1" == "restore" ]; then
restore
fi
}
restore()
{
config_get workdir $CONFIGURATION workdir "/usr/bin/AdGuardHome"
config_get backupwdpath $CONFIGURATION backupwdpath "/usr/bin/AdGuardHome"
cp -u -r -f $backupwdpath/data $workdir
}
backup() {
config_get backupwdpath $CONFIGURATION backupwdpath "/usr/bin/AdGuardHome"
mkdir -p $backupwdpath/data
config_get workdir $CONFIGURATION workdir "/usr/bin/AdGuardHome"
config_get backupfile $CONFIGURATION backupfile ""
for one in $backupfile;
do
while :
do
if [ -d "$backupwdpath/data/$one" ]; then
cpret=$(cp -u -r -f $workdir/data/$one $backupwdpath/data 2>&1)
else
cpret=$(cp -u -r -f $workdir/data/$one $backupwdpath/data/$one 2>&1)
fi
echo "$cpret"
echo "$cpret" | grep "no space left on device"
if [ "$?" == "0" ]; then
echo "磁盘已满,删除log重试中"
del_querylog && continue
rm -f -r $backupwdpath/data/filters
rm -f -r $workdir/data/filters && continue
echo "backup failed"
fi
break
done
done
}
start_service() {
# Reading config
config_load "${CONFIGURATION}"
Expand All @@ -353,11 +395,10 @@ start_service() {

config_get workdir $CONFIGURATION workdir "/usr/bin/AdGuardHome"
ADDITIONAL_ARGS="$ADDITIONAL_ARGS -w $workdir"
config_get backupwd $CONFIGURATION backupwd "0"
config_get backupfile $CONFIGURATION backupfile ""
mkdir -p $workdir/data
if [ "$backupwd" == "1" ] && [ ! -d "$workdir/data" ]; then
config_get backupwdpath $CONFIGURATION backupwdpath "/usr/bin/AdGuardHome"
cp -u -r -f $backupwdpath/data $workdir
if [ -n "$backupfile" ] && [ ! -d "$workdir/data" ]; then
restore
fi
echo -e "$configpath\n$binpath">/lib/upgrade/keep.d/luci-app-adguardhome
# hack to save config file when upgrade system
Expand Down Expand Up @@ -475,25 +516,9 @@ stop_service()
config_load "${CONFIGURATION}"
do_redirect 0
do_crontab
config_get backupwd $CONFIGURATION backupwd "0"
if [ "$backupwd" == "1" ]; then
config_get backupwdpath $CONFIGURATION backupwdpath "/usr/bin/AdGuardHome"
mkdir -p $backupwdpath
config_get workdir $CONFIGURATION workdir "/usr/bin/AdGuardHome"
while :
do
cpret=$(cp -u -r -f $workdir/data $backupwdpath 2>&1)
echo "$cpret"
echo "$cpret" | grep "no space left on device"
if [ "$?" == "0" ]; then
echo "磁盘已满,删除log重试中"
del_querylog && continue
rm -f -r $backupwdpath/data/filters
rm -f -r $workdir/data/filters && continue
echo "backup failed"
fi
break
done
config_get backupfile $CONFIGURATION backupfile "0"
if [ -n "$backupfile" ]; then
backup
fi
echo "AdGuardHome turn off"
echo "enabled=$enabled"
Expand Down

0 comments on commit 413f1f8

Please sign in to comment.