教程

分分鐘學會使用 Git 備份 Linux 配置文件

我們使用 Linux 伺服器時,有時候需要備份配置文件。傳統的備份方法是複製文件,改名,並在文件結尾插入一些字元。

但,如果我們使用 Git ,我們可以很輕鬆的管理配置文件的備份。在這篇文章中,我將會演示如何使用 Git 來實現備份以及管理備份。我測試所用的環境是 CentOS 7  和 RHEL 7。

一、安裝 Git

[root@localhost ~]# yum install git

檢查Git版本

[root@localhost ~]# git --version
git version 1.8.3.1
[root@localhost ~]#

設置初始參數

將如下命令中的用戶名,郵件替換成你自己的。

[root@localhost network-scripts]# git config --global user.name "your_user_name"
[root@localhost network-scripts]# git config --global user.email "your_email"

二、現在初始化 Git 資料庫

因為我準備備份網路配置文件,所以我只需要在網路配置文件的目錄初始化Git資料庫。

[root@localhost ~]# cd /etc/sysconfig/network-scripts
[root@localhost network-scripts]# git init
Initialized empty Git repository in /etc/sysconfig/network-scripts/.git/
[root@localhost network-scripts]#

命令行輸入 ls -a , 那麼我們可以看到,「.git」 文件夾被創建了。

三、使用下面的命令進行備份

[root@localhost network-scripts]# git add ifcfg-enp0s3
[root@localhost network-scripts]#
[root@localhost 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
[root@localhost network-scripts]#

當我們執行第二個命令的時候,它會要求你輸入像 「Changes on 26 Oct 2015」 這樣的備註,然後保存文件。

使用下面的命令查看 git 日誌

[root@localhost network-scripts]# git log
commit 1269758e5f5b2fa3e0ad1fe507abaf73b646a33d
Author: Pradeep <pradeep@linuxtechi.com> Date: Mon Oct 26 00:03:08 2015 -0400
Changes on 26 Oct 2015
[root@localhost network-scripts]#

註:嘗試在 「ifcfg-enp0s3」 文件中插入一些無用字元

wrong-network-config-file

四、從 Git 資料庫恢復網路配置文件

[root@localhost network-scripts]# git reset --hard 1269758e5f5b2fa3e0ad1fe507abaf73b646a33d
HEAD is now at 1269758 Changes on 26 Oct 2015
[root@localhost network-scripts]#

使用與上邊相同的 git id,你安裝的不同,git 的 id 也不同。

驗證從 git 資料庫恢復的文件是否正確。

correct-network-config-file

原文鏈接:http://www.linuxtechi.com/use-git-backup-configuration-files-on-linux/

譯文鏈接:http://www.linuxstory.org/use-git-backup-configuration-files-on-linux/

對這篇文章感覺如何?

太棒了
0
不錯
0
愛死了
0
不太好
0
感覺很糟
0

You may also like

Leave a reply

您的電子郵箱地址不會被公開。 必填項已用 * 標註

此站點使用Akismet來減少垃圾評論。了解我們如何處理您的評論數據

More in:教程

教程

在 Ubuntu 像22.04 LTS Linux 安裝 JUnit 5

JUnit 不僅簡單而且是一種有效的方法來編寫和執行 Java 應用程序的單元測試,因此它是開源類別中使用最廣泛的測試框架。 JUnit的最新版本5發布時帶來了許多改進。 所以,如果你使用Ubuntu […]
教程

同時運行多個 Linux 命令

了解如何在 Linux 中同時執行多個命令可以顯著提高您的效率和生產力。本文將指導您通過各種方式在單行中運行多個 Linux 命令,甚至如何自動化重複的任務。 理解基礎知識 在深入了解高級技巧之前,您 […]