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 30
 DEFAULT_PW=heslo
30 31
 ROUSER=serro
31 32
 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 33
 SQL_USER="root"
34
+CMD="mysql -h $DBHOST -u$SQL_USER "
35
+DUMP_CMD="mysqldump -h $DBHOST -u$SQL_USER -c -t "
36
+BACKUP_CMD="mysqldump -h $DBHOST -u$SQL_USER -c "
37
+TABLE_TYPE="TYPE=MyISAM"
36 38
 # user name column
37 39
 USERCOL="username"
38 40
 
... ...
@@ -48,6 +50,7 @@ usage: $COMMAND create
48 50
        $COMMAND backup (dumps current database to stdout)
49 51
 	   $COMMAND restore <file> (restores tables from a file)
50 52
        $COMMAND copy <new_db> (creates a new db from an existing one)
53
+       $COMMAND reinstalls (updates to a new SER database)
51 54
 
52 55
        if you want to manipulate database as other MySql user than
53 56
        root, want to change database name from default value "$DBNAME",
... ...
@@ -57,43 +60,87 @@ usage: $COMMAND create
57 60
 EOF
58 61
 } #usage
59 62
 
60
-ser_backup()  # pars: <database name> <sql_user>
63
+
64
+# read password 
65
+prompt_pw() 
61 66
 {
62
-if [ $# -ne 2 ] ; then
63
-	echo "ser_drop function takes two params"
64
-	exit 1
65
-fi
66
-$BACKUP_CMD $2 $1
67
+	savetty=`stty -g`
68
+	printf "MySql password for $SQL_USER: "
69
+	stty -echo
70
+	read PW
71
+	stty $savetty
72
+	echo
67 73
 }
68 74
 
69
-ser_restore() #pars: <database name> <sql_user> <filename>
75
+# execute sql command
76
+sql_query()
70 77
 {
71
-if [ $# -ne 3 ] ; then
72
-	echo "ser_drop function takes two params"
78
+	$CMD "-p$PW" "$@"
79
+}
80
+
81
+# dump all rows
82
+ser_dump()  # pars: <database name> 
83
+{
84
+	if [ $# -ne 1 ] ; then
85
+		echo "ser_dump function takes one param"
86
+		exit 1
87
+	fi
88
+	$DUMP_CMD "-p$PW" $1
89
+}
90
+
91
+
92
+# copy a database to database_bak
93
+ser_backup() # par: <database name> 
94
+{
95
+	if [ $# -ne 1 ] ; then
96
+		echo  "ser_backup function takes one param"
97
+		exit 1
98
+	fi
99
+	BU=/tmp/mysql_bup.$$
100
+	$BACKUP_CMD "-p$PW" $1 > $BU
101
+	if [ "$?" -ne 0 ] ; then
102
+		echo "ser backup dump failed"
103
+		exit 1
104
+	fi
105
+#XX
106
+	ser_drop $1_bak 
107
+#XX
108
+	sql_query <<EOF
109
+	create database $1_bak;
110
+EOF
111
+
112
+	ser_restore $1_bak $BU
113
+	if [ "$?" -ne 0 ]; then
114
+		echo "ser backup/restore failed"
115
+		rm $BU
116
+		exit 1
117
+	fi
118
+}
119
+
120
+ser_restore() #pars: <database name> <filename>
121
+{
122
+if [ $# -ne 2 ] ; then
123
+	echo "ser_restore function takes two params"
73 124
 	exit 1
74 125
 fi
75
-$CMD $2 $1 < $3
126
+sql_query $1 < $2
76 127
 }
77 128
 
78
-ser_drop()  # pars: <database name> <sql_user>
129
+ser_drop()  # pars: <database name> 
79 130
 {
80
-if [ $# -ne 2 ] ; then
131
+if [ $# -ne 1 ] ; then
81 132
 	echo "ser_drop function takes two params"
82 133
 	exit 1
83 134
 fi
84 135
 
85
-$CMD $2 << EOF
136
+sql_query << EOF
86 137
 drop database $1;
87 138
 EOF
88 139
 } #ser_drop
89 140
 
90
-ser_create () # pars: <database name> <sql_user> [<no_init_user>]
141
+ser_create () # pars: <database name> [<no_init_user>]
91 142
 {
92
-
93
-#test
94
-#cat > /tmp/sss <<EOF
95
-
96
-if [ $# -eq 2 ] ; then
143
+if [ $# -eq 1 ] ; then
97 144
 	# by default we create initial user
98 145
 	INITIAL_USER="INSERT INTO subscriber 
99 146
 		($USERCOL, password, first_name, last_name, phone, 
... ...
@@ -105,17 +152,17 @@ if [ $# -eq 2 ] ; then
105 152
 		'0239482f19d262f3953186a725a6f53b', 'iptel.org', 
106 153
 		'a84e8abaa7e83d1b45c75ab15b90c320', 
107 154
 		'65e397cda0aa8e3202ea22cbd350e4e9', 'admin' );"
108
-elif [ $# -eq 3 ] ; then
155
+elif [ $# -eq 2 ] ; then
109 156
 	# if 3rd param set, don't create any initial user
110 157
 	INITIAL_USER=""
111 158
 else
112
-	echo "ser_create function takes two or three params"
159
+	echo "ser_create function takes one or two params"
113 160
 	exit 1
114 161
 fi
115 162
 
116 163
 echo "creating database $1 ..."
117 164
 
118
-$CMD $2 <<EOF
165
+sql_query <<EOF
119 166
 create database $1;
120 167
 use $1;
121 168
 
... ...
@@ -511,33 +558,57 @@ EOF
511 558
 } # ser_create
512 559
 
513 560
 
561
+export PW
562
+prompt_pw
563
+
514 564
 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
565
+	reinstall)
566
+
567
+		#1 create a backup database (named *_bak)
568
+		echo "creating backup database"
569
+		ser_backup $DBNAME 
570
+		if [ "$?" -ne 0 ] ; then
571
+			echo "reinstall: ser_backup failed"
572
+			exit 1
573
+		fi
574
+		#2 dump original database and change names in it
575
+		echo "dumping table content ($DBNAME)"
520 576
 		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
577
+		ser_dump $DBNAME  > $tmp_file
578
+		if [ "$?" -ne 0 ] ; then
579
+			echo "reinstall: dumping original db failed"
580
+			exit 1
526 581
 		fi
527
-		ser_drop $DBNAME $SQL_USER
528
-		ret=$?
529
-		if [ "$ret" -ne 0 ]; then
530
-			exit $ret
582
+		sed "s/[uU][sS][eE][rR]_[iI][dD]/user/g" $tmp_file |
583
+			sed "s/[uU][sS][eE][rR]/$USERCOL/g" |
584
+			sed "s/[rR][eE][aA][lL][mM]/domain/g"> ${tmp_file}.2
585
+		#3 drop original database
586
+		echo "dropping table ($DBNAME)"
587
+		ser_drop $DBNAME 
588
+		if [ "$?" -ne 0 ] ; then
589
+			echo "reinstall: dropping table failed"
590
+			rm $tmp_file*
591
+			exit 1
531 592
 		fi
532
-		ser_create $DBNAME $SQL_USER no_init_user
533
-		ret=$?
534
-		if [ "$ret" -ne 0 ]; then
535
-			exit $ret
593
+		#4 change names in table definition and restore
594
+		echo "creating new structures"
595
+		ser_create $DBNAME no_init_user
596
+		if [ "$?" -ne 0 ] ; then
597
+			echo "reinstall: creating new table failed"
598
+			rm $tmp_file*
599
+			exit 1
536 600
 		fi
537
-		ser_restore $DBNAME $SQL_USER $tmp_file
538
-		ret=$?
539
-		rm $tmp_file
540
-		exit $ret
601
+		#5 restoring table content
602
+		echo "restoring table content" 
603
+		ser_restore $DBNAME ${tmp_file}.2
604
+		if [ "$?" -ne 0 ] ; then
605
+			echo "reinstall: restoring table failed"
606
+			rm $tmp_file*
607
+			exit 1
608
+		fi
609
+#XX
610
+#		rm $tmp_file*
611
+		exit 0
541 612
 		;;
542 613
 	copy)
543 614
 		# copy database to some other name
... ...
@@ -547,26 +618,26 @@ case $1 in
547 618
 			exit 1
548 619
 		fi
549 620
 		tmp_file=/tmp/ser_mysql.$$
550
-		ser_backup $DBNAME $SQL_USER > $tmp_file
621
+		ser_dump $DBNAME  > $tmp_file
551 622
 		ret=$?
552 623
 		if [ "$ret" -ne 0 ]; then
553 624
 			rm $tmp_file
554 625
 			exit $ret
555 626
 		fi
556
-		ser_create $1 $SQL_USER no_init_user
627
+		ser_create $1 no_init_user
557 628
 		ret=$?
558 629
 		if [ "$ret" -ne 0 ]; then
559 630
 			rm $tmp_file
560 631
 			exit $ret
561 632
 		fi
562
-		ser_restore $1 $SQL_USER $tmp_file
633
+		ser_restore $1 $tmp_file
563 634
 		ret=$?
564 635
 		rm $tmp_file
565 636
 		exit $ret
566 637
 		;;
567 638
 	backup)
568 639
 		# backup current database
569
-		ser_backup $DBNAME $SQL_USER
640
+		ser_dump $DBNAME 
570 641
 		exit $?
571 642
 		;;
572 643
 	restore)
... ...
@@ -576,7 +647,7 @@ case $1 in
576 647
 			usage
577 648
 			exit 1
578 649
 		fi
579
-		ser_restore $DBNAME $SQL_USER $1
650
+		ser_restore $DBNAME $1
580 651
 		exit $?
581 652
 		;;
582 653
 	create)
... ...
@@ -585,22 +656,22 @@ case $1 in
585 656
 		if [ $# -eq 1 ] ; then
586 657
 			DBNAME="$1"
587 658
 		fi
588
-		ser_create $DBNAME $SQL_USER
659
+		ser_create $DBNAME 
589 660
 		exit $?
590 661
 		;;
591 662
 	drop)
592 663
 		# delete ser database
593
-		ser_drop $DBNAME $SQL_USER
664
+		ser_drop $DBNAME 
594 665
 		exit $?
595 666
 		;;
596 667
 	reinit)
597 668
 		# delete database and create a new one
598
-		ser_drop $DBNAME $SQL_USER
669
+		ser_drop $DBNAME 
599 670
 		ret=$?
600 671
 		if [ "$ret" -ne 0 ]; then
601 672
 			exit $ret
602 673
 		fi
603
-		ser_create $DBNAME $SQL_USER
674
+		ser_create $DBNAME 
604 675
 		exit $?
605 676
 		;;
606 677
 	*)