今日はサーバ管理者向けの軽めの記事です。
この記事は読みましたか?
etckeeper の説明に関しては、この記事で十分です。
しかし、etckeeperで気になる点が1つあります。
- /etc以下のファイルを 全て 自動で管理する
これを修正し、 自分の管理したい設定ファイルだけ をetckeeperで管理する方法を説明します。
ついでに、自動で中央リポジトリと同期する方法も説明します。
便利に.gitignoreを使う
便利な.gitignoreの設定方法を紹介します:
/* /.* !/nginx !/postfix !/dovecot !/php5 !/etckeeper
この設定がなかなか便利で、応用が効きます。
- 始めの2行で、全てのファイルをgitの管理下から排除します
- その後に、 ! プレフィックスを使い、gitで管理したいファイルを指定します
このように設定すると、.gitignoreがバージョニングするファイル一覧に変わります。
このテクニックは、以下の記事で詳しく紹介されています。
- Revision Your Home Directory With Git Using Gibak
- この記事では ホームディレクトリ をgitで管理しています。
ホームディレクトリ以下には、バージョニング不要なファイルが多く存在するため、.gitignoreを活用し、これを制御しています。
このアイディアを念頭に、etckeeperをセッティングします。
ステップバイステップでetckeeperを設定する
このチュートリアルの対象はUbuntu 10.04 TLSです。
しかし、始めのインストール手順だけ変えれば、どのディストリビューションでも対応出来ます。
全て管理者権限で実行してください。
etckeeperをインストールします:
apt-get install etckeeper
etckeeperで利用するバージョン管理ソフトウェアを指定します:
vi /etc/etckeeper/etckeeper.conf
etckeeper.confの中身を以下に変更します:
# The VCS to use. # VCS="hg" VCS="git" # VCS="bzr" # VCS="darcs"
リポジトリを初期化します:
etckeeper init # リポジトリを作成 cd /etc && git rm --cached -r -f ./ # ステージされたファイルを一旦削除
.gitignoreを編集します:
vi /etc/.gitignore
.gitginoreを以下に変更します。 begin section から end section の行は、あなたの元のファイルと同じに設定してください:
/* /.* # begin section managed by etckeeper (do not edit this section by hand) # new and old versions of conffiles, stored by dpkg *.dpkg-* # mount(8) records system state here, no need to store these blkid.tab blkid.tab.old # some other files in /etc that typically do not need to be tracked nologin ld.so.cache mtab .pwd.lock network/run adjtime lvm/cache # editor temp files *~ .*.sw? .sw? #*# DEADJOE # end section managed by etckeeper
リポジトリの状況を確認します。コミットするファイルがなければOKです:
git status # On branch master # # Initial commit # nothing to commit (create/copy files and use "git add" to track)
管理すべき設定ファイルを確認します。ここでは起動しているプロセスから、設定ファイルを割り出します:
ps aux
再度、.gitignoreを編集します:
vi /etc/.gitignore
先ほど紹介したテクニックを使います:
/* /.* # begin section managed by etckeeper (do not edit this section by hand) # new and old versions of conffiles, stored by dpkg *.dpkg-* # mount(8) records system state here, no need to store these blkid.tab blkid.tab.old # some other files in /etc that typically do not need to be tracked nologin ld.so.cache mtab .pwd.lock network/run adjtime lvm/cache # editor temp files *~ .*.sw? .sw? #*# DEADJOE # end section managed by etckeeper !/zabbix !/nginx !/postfix !/dovecot
etckeeperに必要なファイルを追加します。.gitignoreに含めたくないファイルは、 -fオプション を使います:
cd /etc
git add -f .etckeeper
git add -f .gitignore
git add -A
コミットします:
etckeeper commit "Init repo."
これで、Zabbix/Nginx/Postfix/Dovecotの4つの設定ファイルだけがetckeeperの管理下に置かれます。
中央リポジトリと自動で同期する
最後におまけとして、 中央リポジトリと自動で同期する方法 を紹介します。
etckeeperがリポジトリを管理するタイミングで、自動で実行してくれるスクリプト群が /etc/etckeeper 以下にあります。
commit.dディレクトリがコミット時に実行されるスクリプトです:
vi /etc/etckeeper/commit.d/60git-push
この 60git-push を以下の内容に設定します:
#!/bin/sh set -e if [ "$VCS" = git ] && [ -d .git ]; then cd /etc && git push fi
/etc/.git/config にmasterブランチのトラック先を設定しておきます:
[remote "origin"] fetch = +refs/heads/*:refs/remotes/origin/* url = git@github.com:sunomaru/etc_server.git [branch "master"] remote = origin merge = refs/heads/master
これで、etckeeperがコミットするタイミングで、中央リポジトリとも自動で内容が同期します。
この例では git push するだけなので、主な用途はバックアップです。
単なるスクリプトなので、もっと複雑なことも出来ます。
/etc/etckeeper/commit.d を使って、様々なことを自動化出来そうです。
この設定のミソはやはり、 .gitignore ですね。
.gitignoreを活用し、バージョニングしたい設定ファイルを指定します。
gitで管理するファイルをしっかりと絞っておくと、サーバの中も頭の中もすっきりします。
アプリケーション別に設定ファイルをバージョン管理するのは面倒ですしね。
他に設定ファイルを管理する良い方法があれば、コメント欄まで。

