etckeeperで/etc配下をgit管理してみよう
インフラエンジニアの伊藤です。
Linuxサーバの運用をする上で、設定ファイルの書き換えは日常で行われます。
設定を変更する際はファイルの最後に日付名をつけたりして、バックアップすることが多いかと思います。
こんな感じに。
1 2 3 4 | # ls -al |grep httpd.conf -rw-r--r-- 1 root root 16730 Jan 25 22:59 httpd.conf -rw-r--r-- 1 root root 16730 Jan 25 22:59 httpd.conf_20151225 -rw-r--r-- 1 root root 16730 Jan 25 22:59 httpd.conf_20160120 |
ただし、設定変更するごとにバックアップファイルを取っていたら、ファイルが散乱してしまいます。
ファイルが多いってことはそれだけミスが起こる可能性が増えてきます。これってかなり精神衛生上よくないですよね。
そんなときは、etckeeperという VCS(バージョン管理システム)を使って管理するのがオススメです!
etckeeperは、もちろん手動でコミットしてくれますが、以下のタイミングでも自動でコミットしてくれます。
・yumコマンド実行時
・日付が変わったタイミング
とりあえずインストールする
etckeeperをインストールしてみます。
先ほど軽く触れたように、VCSが必要になります。今回はgitをインストールします。
1 | #yum install git |
epelリポジトリからetckeeperをインストールします。
1 | #yum install --enablerepo=epel etckeeper |
無事インストールされました。
1 2 | # rpm -qa |grep etckeeper etckeeper-0.64-1.el5.rf |
使ってみる
まずはリポジトリを作成します。
1 2 | # etckeeper init Initialized empty Git repository in /etc/ .git/ |
コミットしてみます。
1 2 3 4 5 6 | # etckeeper commit "First commit" [master (root-commit) 970f0b3] First commit Author: vagrant <vagrant@cli> 1174 files changed, 122593 insertions(+), 0 deletions(-) create mode 100755 .etckeeper create mode 100644 yum /version-groups .conf |
コミットのログを確認します。
etckeeper vcs~にて、gitのコマンドが使用可能です。
1 2 3 4 5 6 | # etckeeper vcs log commit 970f0b335acdf586e099d57f1bc95d442bff853f Author: vagrant <vagrant@cli> Date: Fri Jan 29 21:02:33 2016 +0900 First commit |
試しにApacheをインストールしてみます。
途中でetckeeperがコミットしてくれます。
1 2 3 4 5 6 7 8 9 10 11 12 13 | #yum install httpd (中略) etckeeper: pre transaction commit Updating : httpd-tools-2.2.15-47.el6.centos.1.x86_64 1 /4 Updating : httpd-2.2.15-47.el6.centos.1.x86_64 2 /4 Cleanup : httpd-2.2.15-47.el6.centos.x86_64 3 /4 Cleanup : httpd-tools-2.2.15-47.el6.centos.x86_64 4 /4 etckeeper: post transaction commit Verifying : httpd-tools-2.2.15-47.el6.centos.1.x86_64 1 /4 Verifying : httpd-2.2.15-47.el6.centos.1.x86_64 2 /4 Verifying : httpd-tools-2.2.15-47.el6.centos.x86_64 3 /4 Verifying : httpd-2.2.15-47.el6.centos.x86_64 4 /4 (中略) |
試しにhttpd.confを編集してコミットしたときのログを確認してみます。
1 2 3 4 5 6 7 8 9 10 11 12 | # etckeeper vcs log commit a6cee87ff14bcf90587e98017d8a737777bcc5c0 Author: vagrant <vagrant@cli> Date: Fri Jan 29 22:43:53 2016 +0900 edit httpd.conf commit 970f0b335acdf586e099d57f1bc95d442bff853f Author: vagrant <vagrant@cli> Date: Fri Jan 29 21:02:33 2016 +0900 First commit |
どの部分を編集したか、diffの確認をしてみます。
1 2 3 4 5 6 7 8 9 10 | # etckeeper vcs diff 970f0b335acdf586e099d57f1bc95d442bff853f 66aefb8e71a7526988c0b3d3863fd6e7e3ec0b54 diff --git a /httpd/conf/httpd .conf b /httpd/conf/httpd .conf index 579d194..5870596 100644 --- a /httpd/conf/httpd .conf +++ b /httpd/conf/httpd .conf @@ -1,3 +1,4 @@ + #test # # This is the main Apache server configuration file. It contains the # configuration directives that give the server its instructions. |
ある地点に戻してみます。
1 2 3 4 | # etckeeper vcs revert a6cee87ff14bcf90587e98017d8a737777bcc5c0 Finished one revert. # On branch master nothing to commit (working directory clean) |
使い方としてはざっとこんな感じですかね。
/etc/配下の設定ファイルが大量にあることが避けられていい感じですね!
ということで、etckeeperの使い方でした。
etckeeperを使って、/etc配下を美しく管理しましょう!