Subversion Repositories ALCASAR

Rev

Details | Last modification | View Log

Rev Author Line No. Line
1 root 1
#! /bin/bash
63 franck 2
# $Id: alcasar-mysql.sh 1056 2013-03-26 21:46:36Z stephane $
3
 
672 richard 4
# alcasar-mysql.sh
5
# by Franck BOUIJOUX, Pascal LEVANT and Richard REY
6
# This script is distributed under the Gnu General Public License (GPL)
7
 
8
# Gestion (sauvegarde / import / RAZ) de la base MySQL 'radius'. Fermeture des sessions de comptabilité ouvertes
9
# Management of mysql 'radius' database (save / import / RAZ). Close the accounting open sessions
10
 
5 franck 11
rep_tr="/var/Save/base" 	 	# répertoire d'accueil des sauvegardes
386 franck 12
ext="sql"  				# extension des fichiers de sauvegarde
904 franck 13
DB_RADIUS="radius"
14
DB_USER="radius"
955 richard 15
radiuspwd="iDTxxBGa"
865 richard 16
new="$(date +%G%m%d-%Hh%M)"  		# date et heure des fichiers
5 franck 17
fichier="$DB_RADIUS-$new.$ext"		# nom du fichier de sauvegarde
1 root 18
 
904 franck 19
 
613 richard 20
stop_acct ()
21
{
22
	date_now=`date "+%F %X"`
23
	echo "UPDATE radacct SET acctstoptime = '$date_now', acctterminatecause = 'Admin-Reset' WHERE acctstoptime IS NULL" | mysql -u$DB_USER -p$radiuspwd $DB_RADIUS
24
}
955 richard 25
check ()
26
{
27
	echo "check (and repair if needed) the database :"
28
	mysqlcheck --databases $DB_RADIUS -u $DB_USER -p$radiuspwd --auto-repair
29
}
904 franck 30
 
31
expire_user ()
32
{
909 franck 33
	del_date=`date +%F`
904 franck 34
	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';"`
35
	for u in $MYSQL
36
	do
37
		 /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'"
909 franck 38
		if [ $? = 0 ]
39
		then
40
			echo "User $u was deleted $del_date" >> /var/log/mysqld/delete_user.log
41
		else
42
			echo "Delete User $u : Error $del_date" >> /var/log/mysqld/delete_user.log
43
		fi
44
 	 done
904 franck 45
}
955 richard 46
usage="Usage: alcasar-mysql.sh { -d or --dump } | { -c or --check } | { -i or --import } | { -r or --raz } | { -acct_stop } | [ --expire_user ]"
1 root 47
nb_args=$#
48
args=$1
49
if [ $nb_args -eq 0 ]
50
then
51
	nb_args=1
52
	args="-h"
53
fi
54
case $args in
55
	-\? | -h* | --h*)
56
		echo "$usage"
57
		exit 0
58
		;;
873 franck 59
	-d | --dump | -dump)	
1 root 60
		[ -d $rep_tr ] || mkdir -p $rep_tr
61
		if [ -e  $fichier ];
62
			then rm -f  $fichier 
63
		fi
955 richard 64
		check
65
		echo "Export the database in file : $fichier"
1 root 66
		mysqldump -u $DB_USER -p$radiuspwd --opt -BcQC  $DB_RADIUS > $rep_tr/$fichier
955 richard 67
		echo "End of export $( date "+%Hh %Mmn" )"
1 root 68
		;;
955 richard 69
	-c | --check | -check)	
70
		check
71
		;;
873 franck 72
	-i | --import | -import)
1 root 73
		if [ $nb_args -ne 2 ]
74
			then
955 richard 75
				echo "Enter a SQL file name (.sql)"
1 root 76
			exit 0
77
		else
613 richard 78
			mysql -u $DB_USER -p$radiuspwd < $2
79
			stop_acct	
1 root 80
		fi
81
		;;
873 franck 82
	-r | --raz | -raz)
417 franck 83
		mysqldump -u $DB_USER -p$radiuspwd --opt -BcQC  $DB_RADIUS > $rep_tr/$fichier && \
1 root 84
		mysql -u$DB_USER -p$radiuspwd $DB_RADIUS < /etc/raddb/radiusd-db-vierge.sql
85
		;;
613 richard 86
	-acct_stop)
87
		stop_acct
88
		;;
904 franck 89
	--expire_user)
90
		expire_user	
91
		;;
1 root 92
	*)
955 richard 93
		echo "Unknown argument :$1";
1 root 94
		echo "$usage"
95
		exit 1
96
		;;
97
esac