PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/voms-2.0.8/src/replica/voms_replica_slave_setup.sh

#
Shell | 233 lines | 191 code | 33 blank | 9 comment | 27 complexity | 8a8a30e0f08e9a382426198ea9bb29ba MD5 | raw file
Possible License(s): Apache-2.0
  1. #!/bin/bash
  2. #
  3. # Default prefix
  4. CERTDIR=${CERTDIR:-/etc/grid-security/certificates} #CERTDIR
  5. SSLPROG="openssl" #openssl
  6. MYSQL_HOME=/usr # MySQL install prefi
  7. master_host="" # Master
  8. mysql_username_admin="replica" # Master MySQL admin user
  9. mysql_password_admin="" # Master MySQL admin pass
  10. master_log_file="" # Master LOG file
  11. master_log_pos="" # Master LOG file
  12. mysql_username_admin="root" # MySQL admin username
  13. mysql_password_admin="" # MySQL admin pass
  14. mysql_replica_user="replica" # user for replication
  15. mysql_conf_file="/etc/my.cnf"
  16. ssl_capath="/etc/grid-security/certificates"
  17. ssl_mysqlcert="/etc/grid-security/mysqlcert.pem"
  18. ssl_mysqlkey="/etc/grid-security/mysqlkey.pem"
  19. slaveid="2"
  20. verbose=""
  21. force="n" # avoid asking questions
  22. dryrun="n"
  23. use_ssl="n"
  24. TEMP=`getopt -o hv --long help,force,mysql-conf-file:,mysql-home:,slave-id:,mysql-admin:,mysql-pwd:,replica-user:,replica-user-pwd:,master-host:,log-file:,log-file-position:,master-db:,ssl-capath:,ssl-mysqlcert:,ssl-mysqlkey:,master-host:,use-ssl,help,ignore:,verbose -n 'voms_install_replica' -- "$@"`
  25. ECHO=`which echo`
  26. if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
  27. eval set -- "$TEMP"
  28. while true ; do
  29. case "$1" in
  30. --mysql-home) MYSQL_HOME=$2 ; shift 2 ;;
  31. --force) do_force="y" ; shift ;;
  32. --mysql-admin) mysql_username_admin=$2 ; shift 2 ;;
  33. --mysql-pwd) mysql_password_admin=$2 ; shift 2 ;;
  34. --replica-user) mysql_replica_user=$2 ; shift 2 ;;
  35. --replica-user-pwd) mysql_replica_user_pwd=$2 ; shift 2 ;;
  36. --master-host) master_host=$2 ; shift 2 ;;
  37. --mysql-conf-file) mysql_conf_file=$2 ; shift 2 ;;
  38. --master-db) master_db=$2 ; shift 2 ;;
  39. --ssl-capath) ssl_capath=$2 ; shift 2 ;;
  40. --ssl-mysqlcert) ssl_mysqlcert=$2 ; shift 2 ;;
  41. --ssl-mysqlkey) ssl_mysqlkey=$2 ; shift 2 ;;
  42. --use-ssl) require_ssl="y" ; shift ;;
  43. --log-file) log_file=$2 ; shift 2 ;;
  44. --log-file-position) log_file_pos=$2 ; shift 2 ;;
  45. --ignore) ignore_tables=$2 ; shift 2 ;;
  46. --dry-run) dryrun="y" ; shift ;;
  47. --slave-id) slaveid=$2 ; shift 2 ;;
  48. -v) verbose="1" ; shift ;;
  49. --verbose) verbose="1" ; shift ;;
  50. -h) help="yes" ; shift ;;
  51. --help) help="yes" ; shift ;;
  52. --) shift ; break ;;
  53. *) echo "Unknown Option:$1" >&2 ; exit 1 ;;
  54. esac
  55. done
  56. if test "x$help" = "xyes" ; then
  57. $ECHO "USAGE: voms-replica-slave-setup.sh [--option value] ... [--option value]"
  58. $ECHO ""
  59. $ECHO "Where --option may be:"
  60. $ECHO " --mysql-home <path> Where the MySQL installation is based."
  61. $ECHO " Defaults to \$MYSQL_HOME if set, otherwise"
  62. $ECHO " assumes that the executables can be"
  63. $ECHO " found in \$PATH"
  64. $ECHO " --force Skips the initial warning."
  65. $ECHO " --mysql-admin <name> The MySQL Admin account. Defaults to 'root'"
  66. $ECHO " --mysql-pwd <password> The password of the MySQL Admin account."
  67. $ECHO " Does not have a default."
  68. $ECHO " --replica-user <name> The user which will be setup for replication."
  69. $ECHO " Defaults to 'replica'"
  70. $ECHO " --replica-user-pwd <pwd> The password of the above account. No defaults,"
  71. $ECHO " this MUST be specified."
  72. $ECHO " --master-host <hostname> The fully qualified hostname to which the"
  73. $ECHO " replica will connect."
  74. $ECHO " --mysql-conf-file <path> The location of the MySQL configuration file."
  75. $ECHO " Defaults to /etc/my.cnf"
  76. $ECHO " --master-db <dbname> The name of the DB to replicate. No defaults."
  77. $ECHO " MUST be specified."
  78. $ECHO " --use-ssl Activates the following three options, and"
  79. $ECHO " specifies a SSL connection for the replication."
  80. $ECHO " --ssl-capath <path> The location where the CA certificates will be found."
  81. $ECHO " Defaults to '/etc/grid-security/certificates'"
  82. $ECHO " --ssl-mysqlcert <file> The location where the host certificate for MySQL"
  83. $ECHO " will be found."
  84. $ECHO " Defaults to '/etc/grid-security/mysqlcert.pem'"
  85. $ECHO " --ssl-mysqlkey <file> The location where the key of the certificate will"
  86. $ECHO " be found. Defaults to '/etc/grid-security/mysqlkey.pem"
  87. $ECHO " --log-file <file> Specifies the master's log file from which to"
  88. $ECHO " replicate transactions."
  89. $ECHO " --log-file-position <pos> Specifies the position in the log file from which"
  90. $ECHO " to start replication."
  91. $ECHO " --ignore <tables> Comma-separated list of tables to ignore during"
  92. $ECHO " replication."
  93. $ECHO " --dry-run Do not actually modify anything."
  94. $ECHO " --slave-id <number> Must be a number >=2, a different number for"
  95. $ECHO " slave. Defaults to 2."
  96. $ECHO " -h, --help This test"
  97. exit 0;
  98. fi
  99. if test "x$do_force" != "xy" ; then
  100. echo "WARNING: This script assumes that it can thrash the current server configuration"
  101. echo "If instead you wish to keep it, read the documentation and perform the procedure by hand."
  102. echo "Do you wish to continue? type YES if it is so."
  103. read answer
  104. if test "z$answer" != "zYES" ; then
  105. exit 1;
  106. fi
  107. fi
  108. if test "x$mysql_replica_user_pwd" = "x" ; then
  109. echo "Did not specify the replication password.";
  110. exit 1;
  111. fi
  112. if test "x$log_file_pos" = "x" ; then
  113. echo "Did not specify the mater log file position.";
  114. echo "is this intentional? Type YES if it is so"
  115. read answer
  116. if test "z$answer" != "zYES" ; then
  117. exit 1;
  118. fi
  119. fi
  120. if test "x$master_db" = "x" ; then
  121. echo "Did not specify which db to replicate";
  122. exit 1;
  123. fi
  124. ###############################################################################
  125. #CREATE USER
  126. if test "x$dryrun" = "xy" ; then
  127. MYSQL=echo
  128. elif test "x$mysql_password_admin" = "x" ; then
  129. MYSQL="$MYSQL_HOME/bin/mysql -u$mysql_username_admin"
  130. else
  131. MYSQL="$MYSQL_HOME/bin/mysql -u$mysql_username_admin -p$mysql_password_admin"
  132. fi
  133. MYSQLDUMP=$MYSQL_HOME/bin/mysqldump
  134. if test -e /etc/rc.d/init.d/mysqld ; then
  135. MYSQLINIT=/etc/rc.d/init.d/mysqld
  136. elif test -e /etc/rc.d/init.d/mysql ; then
  137. MYSQLINIT=/etc/rc.d/init.d/mysql
  138. fi
  139. if test "x$dryrun" = "xn" ; then
  140. $MYSQLINIT stop
  141. #GET MUST PRESERVE DATA
  142. set datadir=`cat $mysql_conf_file|grep -E '[\t ]*datadir[\t ]*='`
  143. set socket=`cat $mysql_conf_file|grep -E '[\t ]*socket[\t ]*='`
  144. set oldpass=`cat $mysql_conf_file|grep -E '[\t ]*old_passwords[\t ]*='`
  145. set replicate=`cat $mysql_conf_file|grep -E '[\t ]*replicate-do-db'`
  146. set ignore=`cat $mysql_conf_file|grep -E '[\t ]*replicate-ignore-table'`
  147. cat >$mysql_conf_file <<EOF
  148. [mysqld]
  149. $datadir
  150. $socket
  151. # Default to using old password format for compatibility with mysql 3.x
  152. # clients (those using the mysqlclient10 compatibility package).
  153. $oldpass
  154. log-bin
  155. server-id=$slaveid
  156. sync_binlog=1
  157. #innodb-safe-binlog
  158. EOF
  159. if test "z$require_ssl" = "zy" ; then
  160. cat >>$mysql_conf_file <<EOF
  161. ssl
  162. ssl-capath=$ssl_capath
  163. ssl-cert=$ssl_mysqlcert
  164. ssl-key=$ssl_mysqlkey
  165. EOF
  166. fi
  167. if test "x$replicate" = "x" ; then
  168. cat >>$mysql_conf_file <<EOF
  169. replicate-do-db=$master_db
  170. EOF
  171. else
  172. cat >>$mysql_conf_file <<EOF
  173. $replicate
  174. replicate-do-db=$master_db
  175. EOF
  176. fi
  177. cat >>$mysql_conf_file <<EOF
  178. $ignore
  179. EOF
  180. if test "x$ignore_tables" != "x" ; then
  181. echo "$ignore_tables" | tr "," "\n" | tr -d " " | MAST=$master_db awk ' BEGIN { mst=ENVIRON["MAST"]; OFS="" } { print "replicate-ignore-table=", mst, ".", $1 }' >>$mysql_conf_file
  182. fi
  183. cat >>$mysql_conf_file <<EOF
  184. [client]
  185. $socket
  186. ssl
  187. ssl-capath=$ssl_capath
  188. ssl-cert=$ssl_mysqlcert
  189. ssl-key=$ssl_mysqlkey
  190. EOF
  191. $MYSQLINIT start
  192. fi
  193. $MYSQL < $master_db.dump
  194. if test "x$require_ssl" = "xy" ; then
  195. $MYSQL -e "STOP SLAVE; CHANGE MASTER TO MASTER_HOST='$master_host', MASTER_USER='$mysql_replica_user', MASTER_PASSWORD='$mysql_replica_user_pwd', MASTER_LOG_FILE='$log_file', MASTER_LOG_POS=$log_file_pos, MASTER_SSL=1, MASTER_SSL_CAPATH='$ssl_capath', MASTER_SSL_CERT='$ssl_mysqlcert', MASTER_SSL_KEY='$ssl_mysqlkey'; START SLAVE;"
  196. else
  197. $MYSQL -e "STOP SLAVE; CHANGE MASTER TO MASTER_HOST='$master_host', MASTER_USER='$mysql_replica_user', MASTER_PASSWORD='$mysql_replica_user_pwd', MASTER_LOG_FILE='$log_file', MASTER_LOG_POS=$log_file_pos; START SLAVE;"
  198. fi
  199. $MYSQL -e "STOP SLAVE;"
  200. $MYSQL -e "START SLAVE;"