教程

分分钟学会使用 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:教程

教程

你最喜欢的Linux终端技巧是什么?

你最喜欢的提高生产力的终端技巧是什么呢?或许,只是一个简单的为一长串字符或者你常用的命令所取的别名( _alias_ )。或许,它是一些很短的脚本来自动处理那些你工作中无聊的东西的。或许,是一些终端复用的软件,比如 _screen_ 或者 _tmux_ 。再或者是你记忆的一些快捷键。最终,这上面的所有东西让你变得更像一个命令行的大佬。
教程

如何在 Linux 中有效地使用 history 命令

众所周知,终端命令在 Linux 中是重要的一部分。对于每天都要和各种命令打交道的 Linux 使用者们,如何才能减少使用终端过程中的冗余部分,让终端更加方便快捷呢?这里教你几招!