Skip to content

Instantly share code, notes, and snippets.

@oppara
Last active October 21, 2022 09:19
Show Gist options
  • Save oppara/311d52c4631e9423ef822950536bb8b4 to your computer and use it in GitHub Desktop.
Save oppara/311d52c4631e9423ef822950536bb8b4 to your computer and use it in GitHub Desktop.
WordPress の同期

ステージング環境から本番環境への同期テスト

WP-CLI で WordPress を日本語パッケージと同じ構成にする - Qiita

MySQLの設定追加

[mysqld]
sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

日付周りで以下のエラーが出るため

SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'updated_at'

hostsの設定

192.168.10.19 wp-sync.local
192.168.10.19 stg.wp-sync.local

wp-cliのインストール

$ curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
$ sudo mv wp-cli.phar /usr/bin/wp
$ sudo chmod +x /usr/bin/wp
$ wp --info

本番環境のWordPressのインストール

$ rm /srv/httpd/htdocs/index.php
$ TARGET=/srv/httpd/htdocs
$ wp core download --locale=ja --version=5.2.4 --path=$TARGET
$ wp config create --dbname=wp --dbuser=vagrant --dbpass=vagrant --dbhost=localhost --dbcharset=utf8mb4 --dbprefix=wp_  --path=$TARGET
$ wp db create --path=$TARGET
$ wp core install --url=wp-sync.local --title=Example --admin_user=admin --admin_password=admin --admin_email=info@example.com --skip-email --path=$TARGET

ステージング環境のWordPressのインストール

<VirtualHost *:80>
    ServerName stg.wp-sync.local
    DocumentRoot "/srv/httpd/htdocs/stg"

    ErrorLog logs/stg-vagrant-error_log
    CustomLog logs/stg-vagrant-access_log combined

    RewriteEngine on
    # RewriteLog /tmp/rewrite.log
    # RewriteLogLevel 6

    <Directory "/srv/httpd/htdocs/stg">
        Options FollowSymlinks Includes
        AllowOverride All
        Require all granted
        SetEnv CAKE_ENV "staging"
        SetEnv APP_ENV "staging"
    </Directory>
</VirtualHost>
$ wp core download --locale=ja --version=5.2.4 --path=/srv/httpd/htdocs/stg
$ wp config create --dbname=wp_stg --dbuser=vagrant --dbpass=vagrant --dbhost=localhost --dbprefix=stg_  --path=/srv/httpd/htdocs/stg
$ wp db create --path=/srv/httpd/htdocs/stg
$ wp core install --path=/srv/httpd/htdocs/stg --url=stg.wp-sync.local --title=Staging --admin_user=admin --admin_password=admin --admin_email=info@example.com --skip-email

ステージングのwpのdbのデータを、本番環境用のドメイン部分を置換してダンプファイルを作成

どのテーブル、カラムをダンプするか精査が必要

$ wp search-replace 'http://stg.wp-sync.local' 'http://wp-sync.local' stg_* \
 --path=/srv/httpd/htdocs/stg \
 --precise --recurse-objects \
 --skip-tables=*comment* \
 --export=dump.sql \
 --report-changed-only
$ sed -i -e 's/`stg_/`wp_/g' dump.sql

本番環境に反映

$ wp db --path=/srv/httpd/htdocs import dump.sql
$ wp rewrite flush --path=/srv/httpd/htdocs --hard

$ rsync -av /srv/httpd/htdocs/stg/wp-content/ /srv/httpd/htdocs/wp-content/ --dry-run
@oppara
Copy link
Author

oppara commented Apr 4, 2022

curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
php wp-cli.phar --info
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment