ipset-goodies/save-restore.sh
corwin 23aabee673 add 1-step wrapper script
USAGE: save-restore.sh [ ipset-save-file(s) ]

Expects to be called with some filenames or in a folder with
files matching *.save.  Orchistrates save-count, ccombine-counts,
and combine-saves to load all of the unique ip addresses in
all the ipset save files into sets that fit them.
2026-04-21 07:16:59 -05:00

60 lines
1.7 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
#
#################
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 -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 $db \
| $ipset $ipsetcmd
RV=$?
rm -f $dbtmp 2>/dev/null
exit $RV;