設定ファイルのバックアップはもう設定ファイル.日付じゃなくてgit使いたいという話

gitの操作方法覚えたいならオフィシャルのこのページがおすすめです。
日本語で書いてあって、全体的に網羅してて申し分ない。

Git - Book

従来プログラム書いたりする人でないと、なかなかgitを使う機会がないと思うのですが
ミドルウェアの設定ファイルの管理とかもgitでやった方がいいなと思うことがあったので
とてもカジュアルなgitの操作方法をメモっておきます。

サーバエンジニアとか、インフラエンジニアとか呼ばれてる人で、
日々サーバ上の設定ファイルを、hogehoge.conf.日付でバックアップしてる人へ
gitを使ってカジュアルに運用したらいいと思う。という内容の戯言でございます。

etckeeperは?という方はetckeeper使ってみたらよいかと思います。
gitのコマンド絶対覚える気がなくて、設定ファイルは全て /etc/ 配下で
自分でコミットとかしないし。って人向けだと思います。

なのでリモートリポジトリに置く前提じゃないので
.gitignoreとか、pullとfetchの違いとか、checkoutのあれとか
branchのあれとかは一切書かないです。すごい雑なまとめでです。

例えばnginxの設定ファイルをgitで管理する場合です。

従来の日付バックアップ方法だと


[root@homhom nginx]#
[root@homhom nginx]# DATE=`date +%Y%m%d`
[root@homhom nginx]#
[root@homhom nginx]# echo $DATE
20120603
[root@homhom nginx]#
[root@homhom nginx]# ls
conf.d fastcgi.conf.default fastcgi_params.default koi-win mime.types.default nginx.conf.default scgi_params.default uwsgi_params.default
fastcgi.conf fastcgi_params koi-utf mime.types nginx.conf scgi_params uwsgi_params win-utf
[root@homhom nginx]#
[root@homhom nginx]# cp -ip nginx.conf nginx.conf.$DATE
[root@homhom nginx]#
[root@homhom nginx]# ls
conf.d fastcgi_params koi-win nginx.conf scgi_params uwsgi_params.default
fastcgi.conf fastcgi_params.default mime.types nginx.conf.20120603 scgi_params.default win-utf
fastcgi.conf.default koi-utf mime.types.default nginx.conf.default uwsgi_params
[root@homhom nginx]#
[root@homhom nginx]#
[root@homhom nginx]# vi nginx.conf
[root@homhom nginx]#
[root@homhom nginx]#
[root@homhom nginx]# diff -C0 nginx.conf.20120603 nginx.conf

nginx.conf.20120603 Mon May 14 18:22:53 2012
      • nginx.conf Sun Jun 3 16:34:55 2012

**************

74 ****
      • 75,76 ----
  1. ## 6/3 is my birthday by Frank

[root@homhom nginx]#

こんな感じになるかと思います。
これをgitにすると


[root@homhom nginx]#
[root@homhom nginx]#
[root@homhom nginx]# git init
Initialized empty Git repository in /etc/nginx/.git/
[root@homhom nginx]#
[root@homhom nginx]# git add .
[root@homhom nginx]#
[root@homhom nginx]# git commit -m "/etc/nginx/ first commit"
[master (root-commit) d437a05] /etc/nginx/ first commit
20 files changed, 1092 insertions(+), 0 deletions(-)
create mode 100644 fastcgi.conf
create mode 100644 fastcgi.conf.default
create mode 100644 fastcgi_params
create mode 100644 fastcgi_params.default
create mode 100644 koi-utf
create mode 100644 koi-win
create mode 100644 mime.types
create mode 100644 mime.types.default
create mode 100644 nginx.conf
create mode 100644 nginx.conf.20120603
create mode 100644 nginx.conf.default
create mode 100644 scgi_params
create mode 100644 scgi_params.default
create mode 100644 uwsgi_params
create mode 100644 uwsgi_params.default
create mode 100644 win-utf
[root@homhom nginx]#

という感じで、 /etc/nginx/ 配下のファイルを管理します。

そして、nginx.confのkeepalive_timeoutの値を変えたとします。


[root@homhom nginx]#
[root@homhom nginx]# vi nginx.conf
[root@homhom nginx]#
[root@homhom nginx]# git add nginx.conf
[root@homhom nginx]#
[root@homhom nginx]# git commit -m "keepalive_timeout 65 -> 5"
[root@homhom nginx]#
[root@homhom nginx]# git show HEAD
commit e828959addfaa3587b9637a9de951818a0a2a2d0
Author: root
Date: Sun Jun 3 17:09:28 2012 +0900

keepalive_timeout 65 -> 5

diff --git a/nginx.conf b/nginx.conf
index c63208f..367ab97 100644

      • a/nginx.conf
      1. b/nginx.conf

@@ -63,7 +63,7 @@ http {
#tcp_nopush on;

#keepalive_timeout 0;

  • keepalive_timeout 65;
  1. keepalive_timeout 5;


#gzip on;

[root@homhom nginx]#

こんな感じですね。

最新版とひとつ前のコミットの差分を確認する場合は


git show HEAD

ですね。そして、よくある話として
「いついつからレスポンスが悪くなった、確認してほしい。」
という状況になったとして、指定した日付から今までの
差分とかも


指定した日付以降
git log --until=2012-01-01
git log --before=2010-01-01

指定した日付以前
git log --after=2012-01-01
git log --since=2012-01-01

こんな感じで指定すればその分のコミットが表示されます。
"設定ファイル.日付"の状態でのファイル一覧目diffはなかなかしんどいですよね。

あとコミットログで検索する方法もあります。


[root@homhom nginx]#
[root@homhom nginx]# git log --grep='gzi'
commit 62790b8aad981fabea17dbb4f30cdc5c29d080b7
Author: root
Date: Sun Jun 3 17:23:19 2012 +0900

gzif off

commit 1c25d999f6c886a992729e06376f01f2eb38c96e
Author: root
Date: Sun Jun 3 17:17:02 2012 +0900

gzip on
[root@homhom nginx]#

正規表現的な感じでgrepできるので素敵です。
こういうtypoも見つけられますからね。

あとコミットログの一覧のフォーマットは色々あるので
自分で使いやすいの探してもらって、それを会社で
.gitconfigにalias組んでもらうか何かすれば捗ると思います。
個人的には


[root@homhom nginx]# git log --pretty="%ad --- %h - %s " --date=iso
2012-06-03 17:23:19 +0900 --- 62790b8 - gzif off
2012-06-03 17:17:02 +0900 --- 1c25d99 - gzip on
2012-06-03 17:09:28 +0900 --- e828959 - keepalive_timeout 65 -> 5
2012-06-03 16:36:27 +0900 --- d437a05 - /etc/nginx/ first commit

[root@homhom nginx]#

が好きです。

あとはコミット指定して差分を確認します


[root@homhom nginx]# git diff 62790b8 1c25d99
diff --git a/nginx.conf b/nginx.conf
index 367ab97..fe634a6 100644

      • a/nginx.conf
      1. b/nginx.conf

@@ -65,7 +65,7 @@ http {
#keepalive_timeout 0;
keepalive_timeout 5;

  1. gzip on;


# Load config files from the /etc/nginx/conf.d directory
# The default server is in conf.d/default.conf
[root@homhom nginx]#

こんな感じですね。

最新からn番目からの差分を表示する


[root@homhom nginx]#
[root@homhom nginx]# git log -p -2
commit 62790b8aad981fabea17dbb4f30cdc5c29d080b7
Author: root
Date: Sun Jun 3 17:23:19 2012 +0900

gzif off

diff --git a/nginx.conf b/nginx.conf
index fe634a6..367ab97 100644

      • a/nginx.conf
      1. b/nginx.conf

@@ -65,7 +65,7 @@ http {
#keepalive_timeout 0;
keepalive_timeout 5;

  1. #gzip on;


# Load config files from the /etc/nginx/conf.d directory
# The default server is in conf.d/default.conf

commit 1c25d999f6c886a992729e06376f01f2eb38c96e
Author: root
Date: Sun Jun 3 17:17:02 2012 +0900

gzip on

diff --git a/nginx.conf b/nginx.conf
index 367ab97..fe634a6 100644

      • a/nginx.conf
      1. b/nginx.conf

@@ -65,7 +65,7 @@ http {
#keepalive_timeout 0;
keepalive_timeout 5;

  1. gzip on;


# Load config files from the /etc/nginx/conf.d directory
# The default server is in conf.d/default.conf
[root@homhom nginx]#

フォーマットを指定して、表示される情報を少なくする


[root@homhom nginx]# git log -p -2 --pretty="%ad --- %h - %s " --date=iso
2012-06-03 17:23:19 +0900 --- 62790b8 - gzif off

diff --git a/nginx.conf b/nginx.conf
index fe634a6..367ab97 100644

      • a/nginx.conf
      1. b/nginx.conf

@@ -65,7 +65,7 @@ http {
#keepalive_timeout 0;
keepalive_timeout 5;

  1. #gzip on;


# Load config files from the /etc/nginx/conf.d directory
# The default server is in conf.d/default.conf
2012-06-03 17:17:02 +0900 --- 1c25d99 - gzip on

diff --git a/nginx.conf b/nginx.conf
index 367ab97..fe634a6 100644

      • a/nginx.conf
      1. b/nginx.conf

@@ -65,7 +65,7 @@ http {
#keepalive_timeout 0;
keepalive_timeout 5;

  1. gzip on;


# Load config files from the /etc/nginx/conf.d directory
# The default server is in conf.d/default.conf
[root@homhom nginx]#

みたいな感じが見やすくていいなと思います。

もちろん指定したファイルの差分だけをみたい。
ということも可能です。


[root@homhom nginx]#
[root@homhom nginx]# echo "hoge" > hoge
[root@homhom nginx]#
[root@homhom nginx]# git add hoge
[root@homhom nginx]#
[root@homhom nginx]# git commit -m "add hoge"
[master bacda4a] add hoge
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 hoge
[root@homhom nginx]#
[root@homhom nginx]# git show
commit bacda4ae40e9bbd792d889faba5edb4459822a17
Author: root
Date: Sun Jun 3 18:17:26 2012 +0900

add hoge

diff --git a/hoge b/hoge
new file mode 100644
index 0000000..2262de0

@@ -0,0 +1 @@

  1. hoge

[root@homhom nginx]#
[root@homhom nginx]#
[root@homhom nginx]# git log -p -2 --pretty="%ad --- %h - %s " --date=iso
2012-06-03 18:17:26 +0900 --- bacda4a - add hoge

diff --git a/hoge b/hoge
new file mode 100644
index 0000000..2262de0

@@ -0,0 +1 @@

  1. hoge

2012-06-03 17:23:19 +0900 --- 62790b8 - gzif off

diff --git a/nginx.conf b/nginx.conf
index fe634a6..367ab97 100644

      • a/nginx.conf
      1. b/nginx.conf

@@ -65,7 +65,7 @@ http {
#keepalive_timeout 0;
keepalive_timeout 5;

  1. #gzip on;


# Load config files from the /etc/nginx/conf.d directory
# The default server is in conf.d/default.conf
[root@homhom nginx]#
[root@homhom nginx]#
[root@homhom nginx]# git log -p -2 --pretty="%ad --- %h - %s " --date=iso nginx.conf
2012-06-03 17:23:19 +0900 --- 62790b8 - gzif off

diff --git a/nginx.conf b/nginx.conf
index fe634a6..367ab97 100644

      • a/nginx.conf
      1. b/nginx.conf

@@ -65,7 +65,7 @@ http {
#keepalive_timeout 0;
keepalive_timeout 5;

  1. #gzip on;


# Load config files from the /etc/nginx/conf.d directory
# The default server is in conf.d/default.conf
2012-06-03 17:17:02 +0900 --- 1c25d99 - gzip on

diff --git a/nginx.conf b/nginx.conf
index 367ab97..fe634a6 100644

      • a/nginx.conf
      1. b/nginx.conf

@@ -65,7 +65,7 @@ http {
#keepalive_timeout 0;
keepalive_timeout 5;

  1. gzip on;


# Load config files from the /etc/nginx/conf.d directory
# The default server is in conf.d/default.conf
[root@homhom nginx]#

のように確認できます。

自分の体験談ですが、基本的に過去の設定ファイルを確認する場合は


XX日前からレスポンスが悪くなった。何か設定がかわっていないか

git log --before でXX日以降のコミット一覧を表示

[root@homhom nginx]#
[root@homhom nginx]# git log --pretty="%ad --- %h - %s " --date=iso --before=2012-06
2012-06-03 18:17:26 +0900 --- bacda4a - add hoge
2012-06-03 17:23:19 +0900 --- 62790b8 - gzif off
2012-06-03 17:17:02 +0900 --- 1c25d99 - gzip on
2012-06-03 17:09:28 +0900 --- e828959 - keepalive_timeout 65 -> 5
2012-06-03 16:36:27 +0900 --- d437a05 - /etc/nginx/ first commit
[root@homhom nginx]#


その対象のコミットのハッシュからハッシュのdiffを表示する。
確実にその対象ファイルがわかっている場合は、対象ファイルを指定する。

[root@homhom nginx]#
[root@homhom nginx]# git show 1c25d99..62790b8 nginx.conf
commit 62790b8aad981fabea17dbb4f30cdc5c29d080b7
Author: root
Date: Sun Jun 3 17:23:19 2012 +0900

gzif off

diff --git a/nginx.conf b/nginx.conf
index fe634a6..367ab97 100644

      • a/nginx.conf
      1. b/nginx.conf

@@ -65,7 +65,7 @@ http {
#keepalive_timeout 0;
keepalive_timeout 5;

  1. #gzip on;


# Load config files from the /etc/nginx/conf.d directory
# The default server is in conf.d/default.conf
[root@homhom nginx]#

という流れが多いかと思います。
特にこの場合は "設定ファイル.日付" で管理している場合と比べて
問題の設定に辿り着く工数がダンチだと思います。
これで問題の設定箇所を変更して、add、commit、するという流れかと思います。
resetとかはやりませんよね。

.gitディレクトリ消しちゃったら終わりじゃん。という問題もありますが
その辺は他のバックアップも兼ねてって感じでしょうか。
まとめると


差分バックアップを取りたいファイルのあるディレクトリそれぞれで

$ git init でgitの管理下に置いて
$ git add . or git add filename.conf で管理対象ファイル追加して
$ vi filename.conf で変更が発生した場合は
$ git add filename.conf addして
$ git commit -m "comment" commitして状態を追加する
$ git show HEAD で最新の設定ファイルとひとつ前の設定の差分を確認して
$ git log --pretty="%ad --- %h - %s "\ で--before=を指定して、過去分の変更全てを確認したりする
--date=iso --before=2012-06

という作業を行えば、設定ファイルを常にgitで差分バックアップが取得できますね。
というお話でした。

以上、雑なまとめですが
お役に立てればと思い書きました。