Subversion Repositories ALCASAR

Rev

Rev 873 | Rev 909 | 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 904 2012-06-23 11:35:25Z 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
{
31
	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';"`
32
	for u in $MYSQL
33
	do
34
		 /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'"
35
	done
36
}
37
usage="Usage: alcasar-mysql.sh { -d or --dump } | { -i or --import } | { -r or --raz } | { -acct_stop } | [ --expire_user ]"
1 root 38
nb_args=$#
39
args=$1
40
if [ $nb_args -eq 0 ]
41
then
42
	nb_args=1
43
	args="-h"
44
fi
45
case $args in
46
	-\? | -h* | --h*)
47
		echo "$usage"
48
		exit 0
49
		;;
873 franck 50
	-d | --dump | -dump)	
1 root 51
		[ -d $rep_tr ] || mkdir -p $rep_tr
52
		if [ -e  $fichier ];
53
			then rm -f  $fichier 
54
		fi
55
		echo "Export de la base 'db_radius' dans le fichier : $fichier"
56
		mysqldump -u $DB_USER -p$radiuspwd --opt -BcQC  $DB_RADIUS > $rep_tr/$fichier
57
		echo "Fin de Sauvegarde mysql $( date "+%Hh %Mmn" )"
58
		;;
873 franck 59
	-i | --import | -import)
1 root 60
		if [ $nb_args -ne 2 ]
61
			then
62
			echo "Entrez le nom d'un fichier SQL (.sql)"
63
			exit 0
64
		else
613 richard 65
			mysql -u $DB_USER -p$radiuspwd < $2
66
			stop_acct	
1 root 67
		fi
68
		;;
873 franck 69
	-r | --raz | -raz)
417 franck 70
		mysqldump -u $DB_USER -p$radiuspwd --opt -BcQC  $DB_RADIUS > $rep_tr/$fichier && \
1 root 71
		mysql -u$DB_USER -p$radiuspwd $DB_RADIUS < /etc/raddb/radiusd-db-vierge.sql
72
		;;
613 richard 73
	-acct_stop)
74
		stop_acct
75
		;;
904 franck 76
	--expire_user)
77
		expire_user	
78
		;;
1 root 79
	*)
80
		echo "Argument inconnu :$1";
81
		echo "$usage"
82
		exit 1
83
		;;
84
esac