Linuxサーバーで作業しているときに、構成ファイルのバックアップを取る必要がある場合があります。バックアップを取る従来の方法は、同じファイルを別の名前でコピーするか、そのファイルの最後にいくつかの文字を挿入することです。
しかし、Gitを使用すると、構成ファイルのバックアップを簡単に管理できます。この投稿では、Gitを使用してこれを実現する方法を示します。 CentOS7とRHEL7で以下のテストを行いました。
ステップ:1Gitがインストールされていない場合は、インストールします。
[[email protected] ~]# yum install git
Gitのバージョンを確認する
[[email protected] ~]# git --version git version 1.8.3.1 [[email protected] ~]#
コミットエラーを回避するには、以下のパラメータを設定してください。
[[email protected] network-scripts]# git config --global user.name "Pradeep" [[email protected] network-scripts]# git config --global user.email "[email protected]"
環境に応じてユーザー名を置き換えることができます。
ステップ:2ここでgitデータベースを初期化します。
ネットワーク構成ファイルのバックアップを取るので、そのためにネットワーク構成ディレクトリでのみGitデータベースを初期化する必要があります。
[[email protected] ~]# cd /etc/sysconfig/network-scripts [[email protected] network-scripts]# git init Initialized empty Git repository in /etc/sysconfig/network-scripts/.git/ [[email protected] network-scripts]#
ご覧のとおり、「 .git 」ディレクトリがフォルダに作成されます。
ステップ:3以下のコマンドを使用してバックアップを取ります
[[email protected] network-scripts]# git add ifcfg-enp0s3 [[email protected] network-scripts]# [[email protected] network-scripts]# git commit ifcfg-enp0s3 [master (root-commit) 1269758] Changes on 26 Oct 2015 1 file changed, 16 insertions(+) create mode 100644 ifcfg-enp0s3 [[email protected] network-scripts]#
2番目のコマンドを実行すると、「2015年10月26日の変更」などのコメントを入力して、ファイルを保存するように求められます。
以下のコマンドを使用して、gitログを表示します。
[[email protected] network-scripts]# git log commit 1269758e5f5b2fa3e0ad1fe507abaf73b646a33d Author: Pradeep <[email protected]> Date: Mon Oct 26 00:03:08 2015 -0400 Changes on 26 Oct 2015 [[email protected] network-scripts]#
注:ファイル「 ifcfg-enp0s3」内にジャンク文字を挿入してみてください 」
ステップ:4ここで、gitデータベースからネットワーク構成ファイルを復元します
[[email protected] network-scripts]# git reset --hard 1269758e5f5b2fa3e0ad1fe507abaf73b646a33d HEAD is now at 1269758 Changes on 26 Oct 2015 [[email protected] network-scripts]#
上記と同じgitidを使用します。 GitIDはセットアップによって異なります。
ファイルがgitデータベースから正しく復元されているかどうかを確認します。
また読む :Linuxでの9つの「diff」コマンド例