Browse code

reinstall option for transition to new database structures introduced

Jiri Kuthan authored on 13/04/2003 21:47:39
Showing 2 changed files
... ...
@@ -280,7 +280,12 @@ proceed, you need to make sure MySQL is installed on your box.
280 280
     or
281 281
     dpkg -i <package_name>
282 282
 3) create MySQL tables
283
-    /usr/sbin/ser_mysql.sh create
283
+	- if you have a previously installed SER on your system, use
284
+    	/usr/sbin/ser_mysql.sh create
285
+	  to convert your SER database into new structures
286
+	- otherwise, if this is your very first installation, use
287
+    	/usr/sbin/ser_mysql.sh create
288
+	  to create SER database structures
284 289
    (you will be promted for password of MySql "root" user)
285 290
 4) configure ser to use SQL
286 291
     uncomment all lines in configuration file ser.cfg which are related to 
... ...
@@ -18,6 +18,7 @@
18 18
 # 2003-01-25: USER_ID changed to user everywhere (janakj)
19 19
 # 2003-01-24: Changed realm column of subscriber and pending
20 20
 #             tables to domain (janakj)
21
+# 2003-04-14  reinstall introduced (jiri)
21 22
 #
22 23
 
23 24
 #################################################################
... ...
@@ -29,10 +30,11 @@ USERNAME=ser
29 29
 DEFAULT_PW=heslo
30 30
 ROUSER=serro
31 31
 RO_PW=47serro11
32
-CMD="mysql -h $DBHOST -p -u "
33
-BACKUP_CMD="mysqldump -h $DBHOST -p -c -t -u "
34
-TABLE_TYPE="TYPE=MyISAM"
35 32
 SQL_USER="root"
33
+CMD="mysql -h $DBHOST -u$SQL_USER "
34
+DUMP_CMD="mysqldump -h $DBHOST -u$SQL_USER -c -t "
35
+BACKUP_CMD="mysqldump -h $DBHOST -u$SQL_USER -c "
36
+TABLE_TYPE="TYPE=MyISAM"
36 37
 # user name column
37 38
 USERCOL="username"
38 39
 
... ...
@@ -48,6 +50,7 @@ usage: $COMMAND create
48 48
        $COMMAND backup (dumps current database to stdout)
49 49
 	   $COMMAND restore <file> (restores tables from a file)
50 50
        $COMMAND copy <new_db> (creates a new db from an existing one)
51
+       $COMMAND reinstalls (updates to a new SER database)
51 52
 
52 53
        if you want to manipulate database as other MySql user than
53 54
        root, want to change database name from default value "$DBNAME",
... ...
@@ -57,43 +60,87 @@ usage: $COMMAND create
57 57
 EOF
58 58
 } #usage
59 59
 
60
-ser_backup()  # pars: <database name> <sql_user>
60
+
61
+# read password 
62
+prompt_pw() 
61 63
 {
62
-if [ $# -ne 2 ] ; then
63
-	echo "ser_drop function takes two params"
64
-	exit 1
65
-fi
66
-$BACKUP_CMD $2 $1
64
+	savetty=`stty -g`
65
+	printf "MySql password for $SQL_USER: "
66
+	stty -echo
67
+	read PW
68
+	stty $savetty
69
+	echo
67 70
 }
68 71
 
69
-ser_restore() #pars: <database name> <sql_user> <filename>
72
+# execute sql command
73
+sql_query()
70 74
 {
71
-if [ $# -ne 3 ] ; then
72
-	echo "ser_drop function takes two params"
75
+	$CMD "-p$PW" "$@"
76
+}
77
+
78
+# dump all rows
79
+ser_dump()  # pars: <database name> 
80
+{
81
+	if [ $# -ne 1 ] ; then
82
+		echo "ser_dump function takes one param"
83
+		exit 1
84
+	fi
85
+	$DUMP_CMD "-p$PW" $1
86
+}
87
+
88
+
89
+# copy a database to database_bak
90
+ser_backup() # par: <database name> 
91
+{
92
+	if [ $# -ne 1 ] ; then
93
+		echo  "ser_backup function takes one param"
94
+		exit 1
95
+	fi
96
+	BU=/tmp/mysql_bup.$$
97
+	$BACKUP_CMD "-p$PW" $1 > $BU
98
+	if [ "$?" -ne 0 ] ; then
99
+		echo "ser backup dump failed"
100
+		exit 1
101
+	fi
102
+#XX
103
+	ser_drop $1_bak 
104
+#XX
105
+	sql_query <<EOF
106
+	create database $1_bak;
107
+EOF
108
+
109
+	ser_restore $1_bak $BU
110
+	if [ "$?" -ne 0 ]; then
111
+		echo "ser backup/restore failed"
112
+		rm $BU
113
+		exit 1
114
+	fi
115
+}
116
+
117
+ser_restore() #pars: <database name> <filename>
118
+{
119
+if [ $# -ne 2 ] ; then
120
+	echo "ser_restore function takes two params"
73 121
 	exit 1
74 122
 fi
75
-$CMD $2 $1 < $3
123
+sql_query $1 < $2
76 124
 }
77 125
 
78
-ser_drop()  # pars: <database name> <sql_user>
126
+ser_drop()  # pars: <database name> 
79 127
 {
80
-if [ $# -ne 2 ] ; then
128
+if [ $# -ne 1 ] ; then
81 129
 	echo "ser_drop function takes two params"
82 130
 	exit 1
83 131
 fi
84 132
 
85
-$CMD $2 << EOF
133
+sql_query << EOF
86 134
 drop database $1;
87 135
 EOF
88 136
 } #ser_drop
89 137
 
90
-ser_create () # pars: <database name> <sql_user> [<no_init_user>]
138
+ser_create () # pars: <database name> [<no_init_user>]
91 139
 {
92
-
93
-#test
94
-#cat > /tmp/sss <<EOF
95
-
96
-if [ $# -eq 2 ] ; then
140
+if [ $# -eq 1 ] ; then
97 141
 	# by default we create initial user
98 142
 	INITIAL_USER="INSERT INTO subscriber 
99 143
 		($USERCOL, password, first_name, last_name, phone, 
... ...
@@ -105,17 +152,17 @@ if [ $# -eq 2 ] ; then
105 105
 		'0239482f19d262f3953186a725a6f53b', 'iptel.org', 
106 106
 		'a84e8abaa7e83d1b45c75ab15b90c320', 
107 107
 		'65e397cda0aa8e3202ea22cbd350e4e9', 'admin' );"
108
-elif [ $# -eq 3 ] ; then
108
+elif [ $# -eq 2 ] ; then
109 109
 	# if 3rd param set, don't create any initial user
110 110
 	INITIAL_USER=""
111 111
 else
112
-	echo "ser_create function takes two or three params"
112
+	echo "ser_create function takes one or two params"
113 113
 	exit 1
114 114
 fi
115 115
 
116 116
 echo "creating database $1 ..."
117 117
 
118
-$CMD $2 <<EOF
118
+sql_query <<EOF
119 119
 create database $1;
120 120
 use $1;
121 121
 
... ...
@@ -511,33 +558,57 @@ EOF
511 511
 } # ser_create
512 512
 
513 513
 
514
+export PW
515
+prompt_pw
516
+
514 517
 case $1 in
515
-	renew)
516
-		# backup, drop, restore -- experimental; thought to
517
-		# deal with upgrade to new structures which include
518
-		# new tables or columns (troubles can appear if columns
519
-		# renamed); not tested too much yet
518
+	reinstall)
519
+
520
+		#1 create a backup database (named *_bak)
521
+		echo "creating backup database"
522
+		ser_backup $DBNAME 
523
+		if [ "$?" -ne 0 ] ; then
524
+			echo "reinstall: ser_backup failed"
525
+			exit 1
526
+		fi
527
+		#2 dump original database and change names in it
528
+		echo "dumping table content ($DBNAME)"
520 529
 		tmp_file=/tmp/ser_mysql.$$
521
-		ser_backup $DBNAME $SQL_USER > $tmp_file
522
-		ret=$?
523
-		if [ "$ret" -ne 0 ]; then
524
-			rm $tmp_file
525
-			exit $ret
530
+		ser_dump $DBNAME  > $tmp_file
531
+		if [ "$?" -ne 0 ] ; then
532
+			echo "reinstall: dumping original db failed"
533
+			exit 1
526 534
 		fi
527
-		ser_drop $DBNAME $SQL_USER
528
-		ret=$?
529
-		if [ "$ret" -ne 0 ]; then
530
-			exit $ret
535
+		sed "s/[uU][sS][eE][rR]_[iI][dD]/user/g" $tmp_file |
536
+			sed "s/[uU][sS][eE][rR]/$USERCOL/g" |
537
+			sed "s/[rR][eE][aA][lL][mM]/domain/g"> ${tmp_file}.2
538
+		#3 drop original database
539
+		echo "dropping table ($DBNAME)"
540
+		ser_drop $DBNAME 
541
+		if [ "$?" -ne 0 ] ; then
542
+			echo "reinstall: dropping table failed"
543
+			rm $tmp_file*
544
+			exit 1
531 545
 		fi
532
-		ser_create $DBNAME $SQL_USER no_init_user
533
-		ret=$?
534
-		if [ "$ret" -ne 0 ]; then
535
-			exit $ret
546
+		#4 change names in table definition and restore
547
+		echo "creating new structures"
548
+		ser_create $DBNAME no_init_user
549
+		if [ "$?" -ne 0 ] ; then
550
+			echo "reinstall: creating new table failed"
551
+			rm $tmp_file*
552
+			exit 1
536 553
 		fi
537
-		ser_restore $DBNAME $SQL_USER $tmp_file
538
-		ret=$?
539
-		rm $tmp_file
540
-		exit $ret
554
+		#5 restoring table content
555
+		echo "restoring table content" 
556
+		ser_restore $DBNAME ${tmp_file}.2
557
+		if [ "$?" -ne 0 ] ; then
558
+			echo "reinstall: restoring table failed"
559
+			rm $tmp_file*
560
+			exit 1
561
+		fi
562
+#XX
563
+#		rm $tmp_file*
564
+		exit 0
541 565
 		;;
542 566
 	copy)
543 567
 		# copy database to some other name
... ...
@@ -547,26 +618,26 @@ case $1 in
547 547
 			exit 1
548 548
 		fi
549 549
 		tmp_file=/tmp/ser_mysql.$$
550
-		ser_backup $DBNAME $SQL_USER > $tmp_file
550
+		ser_dump $DBNAME  > $tmp_file
551 551
 		ret=$?
552 552
 		if [ "$ret" -ne 0 ]; then
553 553
 			rm $tmp_file
554 554
 			exit $ret
555 555
 		fi
556
-		ser_create $1 $SQL_USER no_init_user
556
+		ser_create $1 no_init_user
557 557
 		ret=$?
558 558
 		if [ "$ret" -ne 0 ]; then
559 559
 			rm $tmp_file
560 560
 			exit $ret
561 561
 		fi
562
-		ser_restore $1 $SQL_USER $tmp_file
562
+		ser_restore $1 $tmp_file
563 563
 		ret=$?
564 564
 		rm $tmp_file
565 565
 		exit $ret
566 566
 		;;
567 567
 	backup)
568 568
 		# backup current database
569
-		ser_backup $DBNAME $SQL_USER
569
+		ser_dump $DBNAME 
570 570
 		exit $?
571 571
 		;;
572 572
 	restore)
... ...
@@ -576,7 +647,7 @@ case $1 in
576 576
 			usage
577 577
 			exit 1
578 578
 		fi
579
-		ser_restore $DBNAME $SQL_USER $1
579
+		ser_restore $DBNAME $1
580 580
 		exit $?
581 581
 		;;
582 582
 	create)
... ...
@@ -585,22 +656,22 @@ case $1 in
585 585
 		if [ $# -eq 1 ] ; then
586 586
 			DBNAME="$1"
587 587
 		fi
588
-		ser_create $DBNAME $SQL_USER
588
+		ser_create $DBNAME 
589 589
 		exit $?
590 590
 		;;
591 591
 	drop)
592 592
 		# delete ser database
593
-		ser_drop $DBNAME $SQL_USER
593
+		ser_drop $DBNAME 
594 594
 		exit $?
595 595
 		;;
596 596
 	reinit)
597 597
 		# delete database and create a new one
598
-		ser_drop $DBNAME $SQL_USER
598
+		ser_drop $DBNAME 
599 599
 		ret=$?
600 600
 		if [ "$ret" -ne 0 ]; then
601 601
 			exit $ret
602 602
 		fi
603
-		ser_create $DBNAME $SQL_USER
603
+		ser_create $DBNAME 
604 604
 		exit $?
605 605
 		;;
606 606
 	*)