Subversion Repositories ALCASAR

Rev

Details | Last modification | View Log

Rev Author Line No. Line
3202 rexy 1
#!/bin/bash
2
#
3
# $Id: alcasar-migration-3.3.1_dbRadiusAttrs.sh 2664 2018-11-19 17:35:12Z tom.houdayer $
4
#
5
# alcasar-migration_db-3.3.1.sh
6
# by Tom HOUDAYER
7
#
8
# This script is distributed under the Gnu General Public License (GPL)
9
#
10
# Migrate user database to ALCASAR 3.3.1
11
# Changes:
12
# - Move "CoovaChilli-Max-Total-Octets" RADIUS attribute from radreply to radcheck
13
# - Delete "CoovaChilli-Max-Input-Octets" and "CoovaChilli-Max-Output-Octets" RADIUS attributes
14
 
15
PASSWD_FILE="/root/ALCASAR-passwords.txt"
16
DB_PASS=$(grep ^db_root= $PASSWD_FILE | cut -d'=' -f2-)
17
 
18
DRY_RUN=false
19
 
20
if [ $# -eq 1 ] && [ "$1" == "--simulation" ]; then
21
	DRY_RUN=true
22
fi
23
 
24
db_query () {
25
	if $DRY_RUN && [[ ! "$1" =~ ^'SELECT ' ]]; then
26
		echo "[SQL] request: \"$1\""
27
	else
28
		mysql -u root -p"$DB_PASS" -D radius -e "$1" -Bs
29
		[ $? -ne 0 ] && echo "[SQL] ERROR (\"$1\")"
30
	fi
31
}
32
 
33
for step in $(seq 1 2); do
34
	if [ $step -eq 1 ]; then
35
		tableNameCheck='radcheck'
36
		tableNameReply='radreply'
37
		loginName='username'
38
	else
39
		tableNameCheck='radgroupcheck'
40
		tableNameReply='radgroupreply'
41
		loginName='groupname'
42
	fi
43
 
44
	# Move "CoovaChilli-Max-Total-Octets" RADIUS attribute from radreply to radcheck
45
	db_res=$(db_query "SELECT $loginName, value FROM $tableNameReply WHERE attribute = 'CoovaChilli-Max-Total-Octets';")
46
	if [ -n "$db_res" ]; then
47
		echo "$(echo "$db_res" | wc -l) \"CoovaChilli-Max-Total-Octets\" found in table \"$tableNameReply\"."
48
		while read -r line; do
49
			login=$(echo "$line" | cut -f1)
50
			value=$(echo "$line" | cut -f2)
51
			echo " $login..."
52
 
53
			db_query "INSERT INTO $tableNameCheck ($loginName, attribute, value, op) VALUES ('$login','CoovaChilli-Max-Total-Octets','$value', ':=');"
54
 
55
		done <<< "$db_res"
56
		db_query "DELETE FROM $tableNameReply WHERE attribute = 'CoovaChilli-Max-Total-Octets';"
57
	fi
58
 
59
	# Delete "CoovaChilli-Max-Input-Octets" and "CoovaChilli-Max-Output-Octets" RADIUS attributes
60
	db_query "DELETE FROM $tableNameReply WHERE attribute = 'CoovaChilli-Max-Input-Octets';"
61
	db_query "DELETE FROM $tableNameReply WHERE attribute = 'CoovaChilli-Max-Output-Octets';"
62
done