Subversion Repositories ALCASAR

Rev

Rev 873 | Rev 909 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log

#! /bin/bash
# $Id: alcasar-mysql.sh 904 2012-06-23 11:35:25Z franck $

# alcasar-mysql.sh
# by Franck BOUIJOUX, Pascal LEVANT and Richard REY
# This script is distributed under the Gnu General Public License (GPL)

# Gestion (sauvegarde / import / RAZ) de la base MySQL 'radius'. Fermeture des sessions de comptabilité ouvertes
# Management of mysql 'radius' database (save / import / RAZ). Close the accounting open sessions

rep_tr="/var/Save/base"                 # répertoire d'accueil des sauvegardes
ext="sql"                               # extension des fichiers de sauvegarde
DB_RADIUS="radius"
DB_USER="radius"
radiuspwd="9dthW7VG"
new="$(date +%G%m%d-%Hh%M)"             # date et heure des fichiers
fichier="$DB_RADIUS-$new.$ext"          # nom du fichier de sauvegarde

#USER="franck"
#MYSQLCMD="/usr/bin/mysql -u$DB_USER -p$radiuspwd $DB_RADIUS --exec"

stop_acct ()
{
        date_now=`date "+%F %X"`
        echo "UPDATE radacct SET acctstoptime = '$date_now', acctterminatecause = 'Admin-Reset' WHERE acctstoptime IS NULL" | mysql -u$DB_USER -p$radiuspwd $DB_RADIUS

}

expire_user ()
{
        MYSQL=`/usr/bin/mysql -u$DB_USER -p$radiuspwd $DB_RADIUS -ss --exec  "SELECT username FROM radcheck WHERE ( DATE_SUB(CURDATE(),INTERVAL 7 DAY) > STR_TO_DATE(value,'%d %M %Y')) AND attribute='Expiration';"`
        for u in $MYSQL
        do
                 /usr/bin/mysql -u$DB_USER -p$radiuspwd $DB_RADIUS --exec "DELETE FROM radusergroup WHERE username = '$u'; DELETE FROM radreply WHERE username = '$u'; DELETE FROM userinfo WHERE UserName = '$u'; DELETE FROM radcheck WHERE username = '$u'"
        done
}
usage="Usage: alcasar-mysql.sh { -d or --dump } | { -i or --import } | { -r or --raz } | { -acct_stop } | [ --expire_user ]"
nb_args=$#
args=$1
if [ $nb_args -eq 0 ]
then
        nb_args=1
        args="-h"
fi
case $args in
        -\? | -h* | --h*)
                echo "$usage"
                exit 0
                ;;
        -d | --dump | -dump)    
                [ -d $rep_tr ] || mkdir -p $rep_tr
                if [ -e  $fichier ];
                        then rm -f  $fichier 
                fi
                echo "Export de la base 'db_radius' dans le fichier : $fichier"
                mysqldump -u $DB_USER -p$radiuspwd --opt -BcQC  $DB_RADIUS > $rep_tr/$fichier
                echo "Fin de Sauvegarde mysql $( date "+%Hh %Mmn" )"
                ;;
        -i | --import | -import)
                if [ $nb_args -ne 2 ]
                        then
                        echo "Entrez le nom d'un fichier SQL (.sql)"
                        exit 0
                else
                        mysql -u $DB_USER -p$radiuspwd < $2
                        stop_acct       
                fi
                ;;
        -r | --raz | -raz)
                mysqldump -u $DB_USER -p$radiuspwd --opt -BcQC  $DB_RADIUS > $rep_tr/$fichier && \
                mysql -u$DB_USER -p$radiuspwd $DB_RADIUS < /etc/raddb/radiusd-db-vierge.sql
                ;;
        -acct_stop)
                stop_acct
                ;;
        --expire_user)
                expire_user     
                ;;
        *)
                echo "Argument inconnu :$1";
                echo "$usage"
                exit 1
                ;;
esac