move_status_network.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/bin/bash
  2. # live fast! die young!
  3. set -e
  4. # move_status_network.sh nickname newserver
  5. export nickname="$1"
  6. export newdbhost="$2"
  7. source /etc/statusnet/setup.cfg
  8. function set_maintenance_mode()
  9. {
  10. local nickname=$1;
  11. php $PHPBASE/scripts/settag.php $nickname maintenancemode;
  12. }
  13. function get_current_db_info()
  14. {
  15. local nickname=$1;
  16. #FIXME I couldn't make this work better
  17. export dbhost=`mysql -NB -h $SITEDBHOSTNAME -u $ADMIN --password=$ADMINPASS -e "SELECT dbhost FROM status_network WHERE nickname = '$nickname'" $SITEDB`
  18. export dbuser=`mysql -NB -h $SITEDBHOSTNAME -u $ADMIN --password=$ADMINPASS -e "SELECT dbuser FROM status_network WHERE nickname = '$nickname'" $SITEDB`
  19. export dbpass=`mysql -NB -h $SITEDBHOSTNAME -u $ADMIN --password=$ADMINPASS -e "SELECT dbpass FROM status_network WHERE nickname = '$nickname'" $SITEDB`
  20. export dbname=`mysql -NB -h $SITEDBHOSTNAME -u $ADMIN --password=$ADMINPASS -e "SELECT dbname FROM status_network WHERE nickname = '$nickname'" $SITEDB`
  21. }
  22. function create_empty_db()
  23. {
  24. local newdbhost=$1;
  25. local dbuser=$2;
  26. local dbpass=$3;
  27. local dbname=$4;
  28. mysqladmin -h $newdbhost -u $ADMIN --password=$ADMINPASS create $dbname;
  29. mysql -h $newdbhost -u $ADMIN --password=$ADMINPASS -e "GRANT ALL ON $dbname.* TO '$dbuser'@'localhost' IDENTIFIED BY '$dbpass';" $dbname;
  30. mysql -h $newdbhost -u $ADMIN --password=$ADMINPASS -e "GRANT ALL ON $dbname.* TO '$dbuser'@'%' IDENTIFIED BY '$dbpass';" $dbname;
  31. }
  32. function transfer_data()
  33. {
  34. local dbhost=$1;
  35. local newdbhost=$2;
  36. local dbuser=$3;
  37. local dbpass=$4;
  38. local dbname=$5;
  39. local dumpfile=`mktemp`;
  40. mysqldump -h $dbhost -u $ADMIN --password=$ADMINPASS $dbname > $dumpfile;
  41. mysql -h $newdbhost -u $ADMIN --password=$ADMINPASS $dbname < $dumpfile;
  42. rm $dumpfile;
  43. }
  44. function update_routing_table()
  45. {
  46. local nickname=$1;
  47. local newdbhost=$2;
  48. mysql -h $SITEDBHOSTNAME -u $ADMIN --password=$ADMINPASS -e "UPDATE status_network set dbhost = '$newdbhost' where nickname = '$nickname'" $SITEDB
  49. }
  50. function flush_site()
  51. {
  52. local nickname=$1;
  53. php $PHPBASE/scripts/flushsite.php -s$nickname.$WILDCARD
  54. }
  55. function unset_maintenance_mode()
  56. {
  57. local nickname=$1;
  58. php $PHPBASE/scripts/settag.php -d $nickname maintenancemode;
  59. }
  60. echo -n Setting maintenance mode on $nickname...
  61. set_maintenance_mode $nickname
  62. echo DONE.
  63. echo -n Getting current database info...
  64. get_current_db_info $nickname
  65. echo DONE.
  66. echo -n Creating empty $dbname database on server $newdbhost...
  67. create_empty_db $newdbhost $dbuser $dbpass $dbname
  68. echo DONE
  69. echo -n Copying $dbname database from $dbhost to $newdbhost...
  70. transfer_data $dbhost $newdbhost $dbuser $dbpass $dbname
  71. echo DONE
  72. echo -n Updating the routing table for $nickname to use $dbname on $newdbhost...
  73. update_routing_table $nickname $newdbhost
  74. echo DONE
  75. echo -n Flushing $nickname site from cache...
  76. flush_site $nickname
  77. echo DONE
  78. echo -n Turning off maintenance mode on $nickname...
  79. unset_maintenance_mode $nickname
  80. echo DONE.