#!/bin/sh

BACKUPDIR="/var/netflow"
BACKUPFILE="/conf/netflow.tgz"
BACKUPOFF="<netflowbackup>-1</netflowbackup>"

# find out if the service is running and stop/start it only if needed
RCSCRIPT="/usr/local/etc/rc.d/flowd_aggregate"
if ! ${RCSCRIPT} status > /dev/null; then
	RCSCRIPT="true"
fi

if [ "$(grep -c "${BACKUPOFF}" /conf/config.xml)" != "0" ]; then
	rm -f ${BACKUPFILE}
elif [ "${1}" = "restore" ]; then
	if [ -f "${BACKUPFILE}" ]; then
		${RCSCRIPT} stop
		tar -C / -xzpf "${BACKUPFILE}"
		${RCSCRIPT} start
	fi
else
	if [ -d "${BACKUPDIR}" ]; then
		${RCSCRIPT} stop
		tar -C / -czf "${BACKUPFILE}" ."${BACKUPDIR}"
		${RCSCRIPT} start
	fi
fi
