DBサーバーAからDBサーバーBに移行するのを
itamaeで書いたんだけどいつも忘れるのでメモです
itamaeとは
サーバー構成ツールで、chefにインスパイアされたツールです
chefに比べて学習コストが少ないのがいいところです
前提
・DBはmysqlです。同じバージョンを使っています
・ちがう場合はコンバージョン作業が必要です
・メンテナンスにしてユーザーからのアクセスがない状態とする
・ユーザーのアクセスがある状態の移行はitamaeでは無理ゲーです
事前作業
移行はSCPコマンドでおこなうのでその時に使うキーを事前に作成する
移行先のサーバーにrootでログインする
ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: 2f:6a:11:0f:49:51:25:84:e9:e4:1c:c5:de:43:cd:00 root@bln001 The key's randomart image is: +--[ RSA 2048]----+ | .OEoo+ | | * ... o | | * + o | | B . o | | +S . | | . .. | | .. . | | .. . | | .. | +-----------------+ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys chmod 600 ~/.ssh/authorized_keys
このコマンドで作成した
/root/.ssh/id_rsaを
移行元のDBサーバーAの/var/iko/ikou_B_id_rsaに保存する
パーミッションは600にしておく
chmod 600 /var/iko/ikou_B_id_rsa
一度つながるのか試してみる
ssh DBサーバーBのIP -i /var/iko/ikou_B_id_rsa
もし
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
と表示されたらパーミッションが間違えている可能性がある
バックアップとDBのコピー
DBサーバーA(移行元)でバックアップとDBのコピーする
itamaeのプログラム
%w(target_db1 target_db2 target_db3 target_db4).each do |site| execute "mysql backup " + site do cwd "/var/iko/" command "/usr/local/mysql/bin/mysqldump -u root --password=dbpassword " + site + " >" + site + ".sql" end execute "scp file A to B 1 " + site do cwd "/var/iko/" command "scp -i /var/iko/ikou_B_id_rsa /var/iko/" + site + ".sql root@DBサーバーBのIP:/var/iko/" + site + "_iko.sql " end execute "mysql data size" do command "/usr/local/mysql/bin/mysql -u root -pdbpassword -e \"use " + site + "; select table_schema, sum(data_length+index_length) /1024 /1024 as MB from information_schema.tables where table_schema = database();\"" end execute "mysql table count" do command "/usr/local/mysql/bin/mysql -u root -pdbpassword -e \"use " + site + "; select table_name, table_rows from information_schema.TABLES where table_schema = '" + site + "' ; ;\"" end execute "mysql table count2" do command "/usr/local/mysql/bin/mysql -u root -pdbpassword -e \"use " + site + "; select count(*) from WEB_SESSION ; ;\"" end end
実際のitamae起動コマンド
itamae ssh -h DBサーバーAのIP -p ssh_port -u root db_backup_and_copy.rb -l debug | tee /var/recipe/iko/db_backup_and_copy.txt
プログラムの補足
・teeとは
コマンドの結果をファイルに出力したいけど、標準出力でも出力を見たい!」、という欲張りな要望に答えてくれる意外と頼もしいコマンド。
http://qiita.com/wnoguchi/items/2fc3ec11043d139dc6bb
・確認作業のためにDBのサイズとかレコード件数を出していますが
information_schema.TABLESはあくまでも目安で程度です
移行先と移行元がぴったり一致しない事がある
・「target_db1 target_db2 target_db3 target_db4」はスペース区切りでDB名を記載していく
リストア
DBサーバーB(移行先)でリストアのコマンドを実行する
itamaeのプログラム
%w(target_db1 target_db2 target_db3 target_db4).each do |site| execute "mysql restore " + site do cwd "/var/iko/" command "/usr/local/mysql/bin/mysql -u root -pdbpassword " + site + " < " + site + "_iko.sql" end execute "mysql data size" do command "/usr/local/mysql/bin/mysql -u root -pdbpassword -e \"use " + site + "; select table_schema, sum(data_length+index_length) /1024 /1024 as MB from information_schema.tables where table_schema = database();\"" end execute "mysql table count" do command "/usr/local/mysql/bin/mysql -u root -pdbpassword -e \"use " + site + "; select table_name, table_rows from information_schema.TABLES where table_schema = '" + site + "' ; ;\"" end execute "mysql table count" do command "/usr/local/mysql/bin/mysql -u root -pdbpassword -e \"use " + site + "; select count(*) from WEB_SESSION ; ;\"" end end
実際のitamae起動コマンド
itamae ssh -h DBサーバーBのIP -p ssh_port -u root db_restore.rb -l debug | tee /var/recipe/iko/db_restore.txt
感想
手作業でやるのは、本当に怖いのでできれば移行プログラムと検証用プログラムを作ってやりたい
ただその移行プログラムの工数があまりにもかかりすぎる場合は上司と相談かな