温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何使用Git备份Linux配置文件

发布时间:2021-10-31 13:26:57 来源:亿速云 阅读:177 作者:小新 栏目:系统运维

这篇文章给大家分享的是有关如何使用Git备份Linux配置文件的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

我们使用 Linux 服务器时,有时候需要备份配置文件。传统的备份方法是复制文件,改名,并在文件结尾插入一些字符。

如何使用Git备份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” 文件中插入一些无用字符。

如何使用Git备份Linux配置文件

四、从 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 数据库恢复的文件是否正确。

如何使用Git备份Linux配置文件

感谢各位的阅读!关于“如何使用Git备份Linux配置文件”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI