#! /bin/sh
############################################################################
###    (C)opyright 2003 - 2008 RIPE NCC
###    This file is part of DNSMon
###
###    DNSMon 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.
###
###    DNSMon 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 DNSMon.  If not, see <http://www.gnu.org/licenses/>.
############################################################################

#
# This script moves data from dns/data to dns/archive/year/month
#
#

datadir="/path/to/data"
archivebase="/path/to/archive"
minage="17"
compress="bzip2"

while [ $# -gt 0 ]; do
	case ${1} in
	-n)
		EXEC="echo"
		VERBOSE=yes
		echo "Dry run, also setting -v"
		;;
	-v)
		VERBOSE=yes
		;;
	-d)
		minage=${2}
		shift
		;;
	*) 
		echo "Unrecognized option ${1}"
		;;
	-h)
		echo "Usage: $0 -n -v -d <days> -h"
		;;
	esac
	shift;
done

(
for file in `find -L ${datadir} -type f -mtime +${minage} 2> /dev/null`; do
	archivedir=`echo $file | sed -e 's/.*\.\([0-9][0-9][0-9][0-9]\)\([0-9][0-9]\)\([0-9][0-9]\).*$/\1\/\2\/\3/'`
	test -d "${archivebase}/${archivedir}" || ${EXEC} mkdir -p "${archivebase}/${archivedir}"
	if [ -f "${archivebase}/${archivedir}/$(basename $file).bz2" ]; then
		test "X${VERBOSE}" = "Xyes" && echo "$(basename $file) already archived, overwriting"
	fi
	test "X${VERBOSE}" = "Xyes" && echo -n "Archiving $file to ${archivebase}/${archivedir}..."
	$EXEC mv $file "${archivebase}/${archivedir}"
	$EXEC ${compress} -9f "${archivebase}/${archivedir}/"`basename $file` 2> /dev/null
	test "X${VERBOSE}" = "Xyes" && echo " done"
done
) 
