名前もお好きに、とりあえず、wp_update_siteurl.sh とかで。EC2のメタデータを取得して、その情報でデータベースを更新するようにしました。
#!/bin/bash _id=`curl -s 169.254.169.254/latest/meta-data/instance-id/ | sed -e "s/-/_/"` _dns=`curl -s 169.254.169.254/latest/meta-data/public-hostname/` mysql -u root ${_id} <<_EOT_ update wp_options set option_value = 'http://${_dns}' where option_name = 'siteurl'; update wp_options set option_value = 'http://${_dns}' where option_name = 'home'; _EOT_
武田さん、ありがとうございます。
でも、このままだと不完全で、実は WordPress では wp_options の他にも wp_posts 内に画像ファイルのURLとかを投稿した時の home_url を元に絶対パスで保存してたりするんです。
wp-cli ってツールを使うと、そんな値を一括で置換してくれる search-replace という便利なコマンドが有ります。
網元AMIには、最初から wp-cli 入っているので、wp-cli をインストールしたりする必要は無いです。
てことで、こんな感じで wp_update_siteurl.sh を作ればおっけー
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
WP_ROOT="/path/to/wordpress" | |
WP_CLI="/usr/local/bin/wp –path=${WP_ROOT}" | |
_old_url=`${WP_CLI} eval "echo home_url();"` | |
_dns=`curl -s 169.254.169.254/latest/meta-data/public-hostname/` | |
$WP_CLI search-replace ${_old_url} http://${_dns} |
2行目の “/path/to/wordpress” の所は、適宜自分の環境に合わせて変更してください。
コメントを残す