#!/bin/bash

##
#
# Vaisala software source code file
#
# Copyright (c) Vaisala Oyj 2015. All rights reserved.
#
##
##
# Takes daily backups of Vaisala radar software's configuration and database.
# Deletes old backups.
##

. $(dirname $0)/backup-init

DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )

# Don't fail the script if one of these fails.
set +e
$(dirname $0)/back-up-configuration
CONF_BACKUP_EXIT_CODE=$?
$(dirname $0)/back-up-database
DB_BACKUP_EXIT_CODE=$?
set -e

[ $CONF_BACKUP_EXIT_CODE -ne 0 ] && log "ERROR" "Configuration backup failed! Not cleaning up old backups."
[ $DB_BACKUP_EXIT_CODE -ne 0 ] && log "ERROR" "Database backup failed! Not cleaning old backups."

if [ $CONF_BACKUP_EXIT_CODE -ne 0 ] || [ $DB_BACKUP_EXIT_CODE -ne 0 ]; then
    exit 1
else
    set +e
    $(dirname $0)/delete-old-backups
    DELETE_EXIT_CODE=$?
    set -e
    if [ $DELETE_EXIT_CODE -ne 0 ]; then
	log "ERROR" "Backup cleanup failed!"
	exit $DELETE_EXIT_CODE
    fi
fi
