構成管理ツールfairy support runで行うサーバー管理入門
サンプル紹介(データベース関連)

準備

こちらからサンプルをダウンロードしてください
全て説明不要な簡単なものしかありませんが
いくつかを紹介します
ダウンロードしてきたサンプル内のvagrantフォルダに移動し
vagrant up
と入力して仮想環境を起動してください

SQLの実行

ユーザー追加

MySQLに対してnewuserというユーザーを新たに作成してみます
sqlフォルダを作成します
sqlフォルダ配下にinputフォルダを作成します
inputフォルダ配下にuserフォルダを作成します
userフォルダ配下に00001.sqlを作成します

00001.sql
CREATE USER 'newuser'@'localhost' IDENTIFIED WITH mysql_native_password BY 'newuser_password';
GRANT ALL ON *.* TO 'newuser'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
ファイル構成
     |-- fairysupport_run
     |   |-- common
     |   |   |-- common.sh
     |   |   `-- db_user.sh
     |   |-- sql
     |   |   `-- input
     |   |       `-- user
     |   |           `-- 00001.sql
     |   |-- sql_exe
     |   |   |-- get.txt
     |   |   |-- include.txt
     |   |   `-- main.sh
     |   |-- com_fairysupport_run.jar
     |   |-- env.txt
     |   `-- server.properties.local
     |-- vagrant
     |   `-- Vagrantfile

sql_exeフォルダ配下のinclude.txtとget.txtは下記となっています

include.txt
../common
../sql/input/${2}
get.txt
output ../sql/output/${2}/${FILE}_${SERVER}/${DATE}_${HH}${MM}${SS}

実行してみましょう
sql_exeを実行します
引数のmysqlはSQLを実行するMySQL上のデータベース名
引数のuserはsql>inputフォルダ配下のフォルダ名です

fairysupport_run.bat sql_exe mysql user
./fairysupport_run.sh sql_exe mysql user

outputフォルダが出来ています
output>user>server_server1>YYYYMMDD_HHMMSS、output>user>server_server2>YYYYMMDD_HHMMSSフォルダ配下の00001.sqlを見てみましょう

ファイル構成
     |-- fairysupport_run
     |   |-- common
     |   |   |-- common.sh
     |   |   `-- db_user.sh
     |   |-- sql
     |   |   |-- input
     |   |   |   `-- user
     |   |   |       `-- 00001.sql
     |   |   `-- output
     |   |       `-- user
     |   |           |-- server_server1
     |   |           |   `-- YYYYMMDD_HHMMSS
     |   |           |       `-- 00001.sql
     |   |           `-- server_server2
     |   |               `-- YYYYMMDD_HHMMSS
     |   |                   `-- 00001.sql
     |   |-- sql_exe
     |   |   |-- get.txt
     |   |   |-- include.txt
     |   |   `-- main.sh
     |   |-- com_fairysupport_run.jar
     |   |-- env.txt
     |   `-- server.properties.local
     |-- vagrant
     |   `-- Vagrantfile

newuserが作成されたことがわかります


00001.sqlを少し改良してみましょう

00001.sql
CREATE USER '${_ARG3}'@'localhost' IDENTIFIED WITH mysql_native_password BY '${_ARG4}';
GRANT ALL ON *.* TO '${_ARG3}'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;

実行してみましょう
引数を2つ増やします

fairysupport_run.bat sql_exe mysql user newuser2 newuser2_password
./fairysupport_run.sh sql_exe mysql user newuser2 newuser2_password

newuser2が作成されます

データベースの作成

MySQL上に新しいデータベースを作成してみます
sql>inputフォルダ配下にdbフォルダを作成します
dbフォルダ配下に00001.sqlを作成します

00001.sql
CREATE DATABASE NEW_DB;
ファイル構成
     |-- fairysupport_run
     |   |-- common
     |   |   |-- common.sh
     |   |   `-- db_user.sh
     |   |-- sql
     |   |   `-- input
     |   |       `-- db
     |   |           `-- 00001.sql
     |   |-- sql_exe
     |   |   |-- get.txt
     |   |   |-- include.txt
     |   |   `-- main.sh
     |   |-- com_fairysupport_run.jar
     |   |-- env.txt
     |   `-- server.properties.local
     |-- vagrant
     |   `-- Vagrantfile

実行してみましょう

fairysupport_run.bat sql_exe mysql db
./fairysupport_run.sh sql_exe mysql db

output>dbフォルダが出来ています

ファイル構成
     |-- fairysupport_run
     |   |-- common
     |   |   |-- common.sh
     |   |   `-- db_user.sh
     |   |-- sql
     |   |   |-- input
     |   |   |   `-- db
     |   |   |       `-- 00001.sql
     |   |   `-- output
     |   |       `-- db
     |   |           |-- server_server1
     |   |           |   `-- YYYYMMDD_HHMMSS
     |   |           |       `-- 00001.sql
     |   |           `-- server_server2
     |   |               `-- YYYYMMDD_HHMMSS
     |   |                   `-- 00001.sql
     |   |-- sql_exe
     |   |   |-- get.txt
     |   |   |-- include.txt
     |   |   `-- main.sh
     |   |-- com_fairysupport_run.jar
     |   |-- env.txt
     |   `-- server.properties.local
     |-- vagrant
     |   `-- Vagrantfile

NEW_DBが作成されたことがわかります

テーブルの作成

初回テーブルの作成

MySQL上に初めてテーブルを作成してみます
sql_versionフォルダを作成します
sql_versionフォルダ配下にinputフォルダを作成します
sql_version>inputフォルダ配下にtableフォルダを作成します
sql_version>input>tableフォルダ配下に000000001フォルダを作成します
000000001フォルダ配下に00001_SAMPLE_1.sqlを作成します
000000001フォルダ配下に00002_SAMPLE_2.sqlを作成します

00001_SAMPLE_1.sql
CREATE TABLE SAMPLE_1 (
  ID int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  NAME varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
00002_SAMPLE_2.sql
CREATE TABLE SAMPLE_2 (
  ID int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  NAME varchar(255) NOT NULL,
  SAMPLE_1_ID int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
ファイル構成
     |-- fairysupport_run
     |   |-- common
     |   |   |-- common.sh
     |   |   `-- db_user.sh
     |   |-- sql_version
     |   |   `-- input
     |   |       `-- table
     |   |           `-- 000000001
     |   |               |-- 00001_SAMPLE_1.sql
     |   |               `-- 00002_SAMPLE_2.sql
     |   |-- sql_version_exe
     |   |   |-- get.txt
     |   |   |-- include.txt
     |   |   `-- main.sh
     |   |-- com_fairysupport_run.jar
     |   |-- env.txt
     |   `-- server.properties.local
     |-- vagrant
     |   `-- Vagrantfile

sql_version_exeフォルダ配下のinclude.txtとget.txtは下記となっています

include.txt
../common
../sql_version/input/${2}
get.txt
output ../sql_version/output/${2}/${FILE}_${SERVER}/${DATE}_${HH}${MM}${SS}

実行してみましょう
sql_version_exeを実行します
引数のNEW_DBはSQLを実行するMySQL上のデータベース名です。今回は先ほど作成したNEW_DBに対して実行します
引数のtableはsql_version>inputフォルダ配下のフォルダ名です
引数に1を2つ渡しています。sql_version>input>tableフォルダ配下の000000001フォルダのことです
sql_version_exeは000000001から000000003フォルダまでに入っているファイルを実行というように範囲指定をして実行できます
今回は000000001から000000001フォルダまでに入っているファイルを実行するということになります

fairysupport_run.bat sql_version_exe NEW_DB table 1 1
./fairysupport_run.sh sql_version_exe NEW_DB table 1 1

sql_version>outputフォルダが出来ています。見てみましょう

ファイル構成
     |-- fairysupport_run
     |   |-- common
     |   |   |-- common.sh
     |   |   `-- db_user.sh
     |   |-- sql_version
     |   |   |-- input
     |   |   |   `-- table
     |   |   |       `-- 000000001
     |   |   |           |-- 00001_SAMPLE_1.sql
     |   |   |           `-- 00002_SAMPLE_2.sql
     |   |   `-- output
     |   |       `-- table
     |   |           |-- server_server1
     |   |           |   `-- YYYYMMDD_HHMMSS
     |   |           |       `-- 000000001
     |   |           |           |-- 00001_SAMPLE_1.sql
     |   |           |           `-- 00002_SAMPLE_2.sql
     |   |           `-- server_server2
     |   |               `-- YYYYMMDD_HHMMSS
     |   |                   `-- 000000001
     |   |                       |-- 00001_SAMPLE_1.sql
     |   |                       `-- 00002_SAMPLE_2.sql
     |   |-- sql_version_exe
     |   |   |-- get.txt
     |   |   |-- include.txt
     |   |   `-- main.sh
     |   |-- com_fairysupport_run.jar
     |   |-- env.txt
     |   `-- server.properties.local
     |-- vagrant
     |   `-- Vagrantfile

テーブルが作成されたことがわかります

2回目以降のテーブルの作成と変更

初回リリース後、さらに開発が進んで新規テーブルができたとしましょう
sql_version>input>tableフォルダ配下に000000002フォルダを作成します
000000002フォルダ配下に00001_SAMPLE_3.sqlを作成します

00001_SAMPLE_3.sql
CREATE TABLE SAMPLE_3 (
  ID int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  NAME varchar(255) NOT NULL,
  SAMPLE_1_ID int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8

新規テーブルがリリースされ、さらに開発が進んでテーブルにカラム追加が必要になったとしましょう
sql_version>input>tableフォルダ配下に000000003フォルダを作成します
000000003フォルダ配下に00001_SAMPLE_1.sqlを作成します

00001_SAMPLE_1.sql
ALTER TABLE SAMPLE_1 ADD NEW_COL varchar(10);
ファイル構成
     |-- fairysupport_run
     |   |-- common
     |   |   |-- common.sh
     |   |   `-- db_user.sh
     |   |-- sql_version
     |   |   `-- input
     |   |       `-- table
     |   |           |-- 000000001
     |   |           |   |-- 00001_SAMPLE_1.sql
     |   |           |   `-- 00002_SAMPLE_2.sql
     |   |           |-- 000000002
     |   |           |   `-- 00001_SAMPLE_3.sql
     |   |           `-- 000000003
     |   |               `-- 00001_SAMPLE_1.sql
     |   |-- sql_version_exe
     |   |   |-- get.txt
     |   |   |-- include.txt
     |   |   `-- main.sh
     |   |-- com_fairysupport_run.jar
     |   |-- env.txt
     |   `-- server.properties.local
     |-- vagrant
     |   `-- Vagrantfile

初回リリース状態になっている環境をversion3状態にします
sql_version>outputフォルダ配下を見ればどのversionまで実行したかわかります
実行してみましょう
引数の2 3は000000002と000000003フォルダ配下のSQLを実行するということではなく、000000002から000000003フォルダ配下のSQLを実行するということです
000000004フォルダまで作成してある場合、引数に2 4と渡せば000000002、000000003、000000004フォルダ配下のSQLが実行されます

fairysupport_run.bat sql_version_exe NEW_DB table 2 3
./fairysupport_run.sh sql_version_exe NEW_DB table 2 3

000000002、000000003フォルダ配下のSQLが実行されました

INSERTの実行

テーブルを作成したので、先ほどのsql_exeを使ってデータを投入してみましょう
sql>inputフォルダ配下にinsertフォルダを作成します
insertフォルダ配下に00001.sqlを作成します
insertフォルダ配下に00002.sqlを作成します

00001.sql
INSERT INTO SAMPLE_1 (NAME, NEW_COL) VALUES ('sample1_name1', 'val1')
00002.sql
INSERT INTO SAMPLE_2 (NAME, SAMPLE_1_ID) SELECT 'sample2_name1', ID FROM SAMPLE_1
ファイル構成
     |-- fairysupport_run
     |   |-- common
     |   |   |-- common.sh
     |   |   `-- db_user.sh
     |   |-- sql
     |   |   `-- input
     |   |       `-- insert
     |   |           |-- 00001.sql
     |   |           `-- 00002.sql
     |   |-- sql_exe
     |   |   |-- get.txt
     |   |   |-- include.txt
     |   |   `-- main.sh
     |   |-- com_fairysupport_run.jar
     |   |-- env.txt
     |   `-- server.properties.local
     |-- vagrant
     |   `-- Vagrantfile

実行してみましょう

fairysupport_run.bat sql_exe NEW_DB insert
./fairysupport_run.sh sql_exe NEW_DB insert

output>insertフォルダが出来ています

ファイル構成
     |-- fairysupport_run
     |   |-- common
     |   |   |-- common.sh
     |   |   `-- db_user.sh
     |   |-- sql
     |   |   |-- input
     |   |   |   `-- insert
     |   |   |       |-- 00001.sql
     |   |   |       `-- 00002.sql
     |   |   `-- output
     |   |       `-- insert
     |   |           |-- server_server1
     |   |           |   `-- YYYYMMDD_HHMMSS
     |   |           |       |-- 00001.sql
     |   |           |       `-- 00002.sql
     |   |           `-- server_server2
     |   |               `-- YYYYMMDD_HHMMSS
     |   |                   |-- 00001.sql
     |   |                   `-- 00002.sql
     |   |-- sql_exe
     |   |   |-- get.txt
     |   |   |-- include.txt
     |   |   `-- main.sh
     |   |-- com_fairysupport_run.jar
     |   |-- env.txt
     |   `-- server.properties.local
     |-- vagrant
     |   `-- Vagrantfile

INSERT文が実行されたことがわかります

データの取得

投入したデータを取得してみましょう

ファイル構成
     |-- fairysupport_run
     |   |-- common
     |   |   |-- common.sh
     |   |   `-- db_user.sh
     |   |-- dump_db_get
     |   |   |-- get.txt
     |   |   |-- include.txt
     |   |   `-- main.sh
     |   |-- com_fairysupport_run.jar
     |   |-- env.txt
     |   `-- server.properties.local
     |-- vagrant
     |   `-- Vagrantfile

実行してみましょう

fairysupport_run.bat dump_db_get NEW_DB
./fairysupport_run.sh dump_db_get NEW_DB

dump_dbフォルダが出来ています

ファイル構成
     |-- fairysupport_run
     |   |-- common
     |   |   |-- common.sh
     |   |   `-- db_user.sh
     |   |-- dump_db
     |   |   |-- server_server1
     |   |   |   `-- NEW_DB
     |   |   |       `-- YYYYMMDD_HHMMSS
     |   |   |           `-- mysqldump.sql
     |   |   `-- server_server2
     |   |       `-- NEW_DB
     |   |           `-- YYYYMMDD_HHMMSS
     |   |               `-- mysqldump.sql
     |   |-- dump_db_get
     |   |   |-- get.txt
     |   |   |-- include.txt
     |   |   `-- main.sh
     |   |-- com_fairysupport_run.jar
     |   |-- env.txt
     |   `-- server.properties.local
     |-- vagrant
     |   `-- Vagrantfile

mysqldump.sqlファイルを見ると投入データが取得できています

データの投入

取得したデータを投入してみましょう

ファイル構成
     |-- fairysupport_run
     |   |-- common
     |   |   |-- common.sh
     |   |   `-- db_user.sh
     |   |-- dump_db
     |   |   |-- server_server1
     |   |   |   `-- NEW_DB
     |   |   |       `-- YYYYMMDD_HHMMSS
     |   |   |           `-- mysqldump.sql
     |   |   `-- server_server2
     |   |       `-- NEW_DB
     |   |           `-- YYYYMMDD_HHMMSS
     |   |               `-- mysqldump.sql
     |   |-- dump_db_set
     |   |   |-- include.txt
     |   |   `-- main.sh
     |   |-- com_fairysupport_run.jar
     |   |-- env.txt
     |   `-- server.properties.local
     |-- vagrant
     |   `-- Vagrantfile

実行してみましょう
引数のYYYYMMDD_HHMMSSの部分は実際のdump_db>server_server1>NEW_DB配下のフォルダ名に変えてください

fairysupport_run.bat dump_db_set NEW_DB YYYYMMDD_HHMMSS
./fairysupport_run.sh dump_db_set NEW_DB YYYYMMDD_HHMMSS

mysqldumpが実行されました


サンプルの紹介は以上です


目次