ipset-goodies/save-restore.sh
corwin a402bc4882 add IP deduplication optons
Add $check new variable taken from environment; it can be set to
an integeter to generate the apprprite option to combine-saves.pl
2026-04-21 11:43:00 -05:00

75 lines
2.1 KiB
Bash
Executable file

#!/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
#
#################
##
# 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
folder=${folder:-}
savext=${savext:-.save}
db=${db:-name-maxelem.db}
dbtmp=${dbtmp:-$db.tmp}
ipset=${ipset:-$(which ipset)}
ipsetcmd=${ipsetcmd:-restore}
scriptdir=${scriptdir:-$(dirname $0)}
savecnt="${scriptdir}/save-count.pl"
sortcnt="${scriptdir}/combine-counts.pl"
loadset="${scriptdir}/combine-saves.pl"
files=${files:-"$@"}
if test -n "$check" ; then
check="--check=$checkdef";
fi
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" \
| $loadset $check $db \
| $ipset $ipsetcmd
RV=$?
rm -f $dbtmp 2>/dev/null
exit $RV;