Subversion Repositories ALCASAR

Rev

Rev 904 | Rev 955 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
1 root 1
#! /bin/bash
63 franck 2
# $Id: alcasar-mysql.sh 909 2012-06-25 07:16:02Z franck $
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"
15
radiuspwd="9dthW7VG"
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
#USER="franck"
20
#MYSQLCMD="/usr/bin/mysql -u$DB_USER -p$radiuspwd $DB_RADIUS --exec"
21
 
613 richard 22
stop_acct ()
23
{
24
	date_now=`date "+%F %X"`
25
	echo "UPDATE radacct SET acctstoptime = '$date_now', acctterminatecause = 'Admin-Reset' WHERE acctstoptime IS NULL" | mysql -u$DB_USER -p$radiuspwd $DB_RADIUS
26
 
27
}
904 franck 28
 
29
expire_user ()
30
{
909 franck 31
	del_date=`date +%F`
904 franck 32
	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';"`
33
	for u in $MYSQL
34
	do
35
		 /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 36
		if [ $? = 0 ]
37
		then
38
			echo "User $u was deleted $del_date" >> /var/log/mysqld/delete_user.log
39
		else
40
			echo "Delete User $u : Error $del_date" >> /var/log/mysqld/delete_user.log
41
		fi
42
 	 done
904 franck 43
}
44
usage="Usage: alcasar-mysql.sh { -d or --dump } | { -i or --import } | { -r or --raz } | { -acct_stop } | [ --expire_user ]"
1 root 45
nb_args=$#
46
args=$1
47
if [ $nb_args -eq 0 ]
48
then
49
	nb_args=1
50
	args="-h"
51
fi
52
case $args in
53
	-\? | -h* | --h*)
54
		echo "$usage"
55
		exit 0
56
		;;
873 franck 57
	-d | --dump | -dump)	
1 root 58
		[ -d $rep_tr ] || mkdir -p $rep_tr
59
		if [ -e  $fichier ];
60
			then rm -f  $fichier 
61
		fi
62
		echo "Export de la base 'db_radius' dans le fichier : $fichier"
63
		mysqldump -u $DB_USER -p$radiuspwd --opt -BcQC  $DB_RADIUS > $rep_tr/$fichier
64
		echo "Fin de Sauvegarde mysql $( date "+%Hh %Mmn" )"
65
		;;
873 franck 66
	-i | --import | -import)
1 root 67
		if [ $nb_args -ne 2 ]
68
			then
69
			echo "Entrez le nom d'un fichier SQL (.sql)"
70
			exit 0
71
		else
613 richard 72
			mysql -u $DB_USER -p$radiuspwd < $2
73
			stop_acct	
1 root 74
		fi
75
		;;
873 franck 76
	-r | --raz | -raz)
417 franck 77
		mysqldump -u $DB_USER -p$radiuspwd --opt -BcQC  $DB_RADIUS > $rep_tr/$fichier && \
1 root 78
		mysql -u$DB_USER -p$radiuspwd $DB_RADIUS < /etc/raddb/radiusd-db-vierge.sql
79
		;;
613 richard 80
	-acct_stop)
81
		stop_acct
82
		;;
904 franck 83
	--expire_user)
84
		expire_user	
85
		;;
1 root 86
	*)
87
		echo "Argument inconnu :$1";
88
		echo "$usage"
89
		exit 1
90
		;;
91
esac