2026-04-21 07:16:59 -05:00
|
|
|
#!/usr/bin/bash
|
|
|
|
|
# save-restore - restore files made with ipset(1) save
|
|
|
|
|
# Copyright (C)2026 Corwin Brust <corwin@bru.st>
|
|
|
|
|
#
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
#
|
|
|
|
|
#################
|
|
|
|
|
#
|
|
|
|
|
# Work in the current directory
|
|
|
|
|
# 1. Look for files named on the command line
|
|
|
|
|
# 1.1 ..on the command line or, otherwise
|
|
|
|
|
# 1.2 .. in CWD as *.save
|
|
|
|
|
# 2. Create a flat-file "DB" with lines in the form NAME MAX
|
|
|
|
|
# 3. Sort and reduce DB combining counts for non-empty sets
|
|
|
|
|
# 4. Filter files into ipset restore
|
|
|
|
|
#
|
|
|
|
|
#################
|
|
|
|
|
|
2026-04-21 11:43:00 -05:00
|
|
|
##
|
|
|
|
|
# options to control duplicate IP checks:
|
|
|
|
|
# 0 - no checking
|
|
|
|
|
# 1 - in restore, append -exist to ipset add
|
|
|
|
|
# 2 - in parse, run ipcheck test check
|
|
|
|
|
# 3 - in both restore and parse, exist & test
|
|
|
|
|
check=${check:-} # take default from combine-saves.pl
|
|
|
|
|
|
2026-04-21 07:16:59 -05:00
|
|
|
folder=${folder:-}
|
|
|
|
|
|
|
|
|
|
savext=${savext:-.save}
|
|
|
|
|
|
|
|
|
|
db=${db:-name-maxelem.db}
|
2026-04-21 11:43:00 -05:00
|
|
|
|
2026-04-21 07:16:59 -05:00
|
|
|
dbtmp=${dbtmp:-$db.tmp}
|
2026-04-21 11:43:00 -05:00
|
|
|
|
2026-04-21 07:16:59 -05:00
|
|
|
ipset=${ipset:-$(which ipset)}
|
|
|
|
|
ipsetcmd=${ipsetcmd:-restore}
|
2026-04-21 11:43:00 -05:00
|
|
|
|
2026-04-21 07:16:59 -05:00
|
|
|
scriptdir=${scriptdir:-$(dirname $0)}
|
|
|
|
|
savecnt="${scriptdir}/save-count.pl"
|
|
|
|
|
sortcnt="${scriptdir}/combine-counts.pl"
|
|
|
|
|
loadset="${scriptdir}/combine-saves.pl"
|
|
|
|
|
|
|
|
|
|
files=${files:-"$@"}
|
|
|
|
|
|
2026-04-21 11:43:00 -05:00
|
|
|
if test -n "$check" ; then
|
|
|
|
|
check="--check=$checkdef";
|
|
|
|
|
fi
|
|
|
|
|
|
2026-04-21 07:16:59 -05:00
|
|
|
if test -z "$files" ; then
|
|
|
|
|
files=$(ls -1 ${folder}*${savext} 2>/dev/null)
|
|
|
|
|
fi
|
|
|
|
|
if test -z "$files" ; then
|
|
|
|
|
echo "No ipset save files.";
|
|
|
|
|
exit 1;
|
|
|
|
|
fi
|
|
|
|
|
#echo "'$files'"
|
|
|
|
|
|
|
|
|
|
cat "$files" | \
|
|
|
|
|
$savecnt >$dbtmp \
|
|
|
|
|
&& $sortcnt <$dbtmp >$db \
|
|
|
|
|
&& cat "$files" \
|
2026-04-21 11:43:00 -05:00
|
|
|
| $loadset $check $db \
|
2026-04-21 07:16:59 -05:00
|
|
|
| $ipset $ipsetcmd
|
|
|
|
|
RV=$?
|
|
|
|
|
rm -f $dbtmp 2>/dev/null
|
|
|
|
|
exit $RV;
|