手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

分分钟学会使用 Git 备份 Linux 配置文件

[日期:2015-11-08] 来源:linuxstory.org  作者:ZERO [字体:大 中 小]

我们使用 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 数据库恢复网络配置文件

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

GitHub 教程系列文章: 

GitHub 使用教程图文详解  http://www.linuxidc.com/Linux/2014-09/106230.htm 

Git 标签管理详解 http://www.linuxidc.com/Linux/2014-09/106231.htm 

Git 分支管理详解 http://www.linuxidc.com/Linux/2014-09/106232.htm 

Git 远程仓库详解 http://www.linuxidc.com/Linux/2014-09/106233.htm 

Git 本地仓库(Repository)详解 http://www.linuxidc.com/Linux/2014-09/106234.htm 

Git 服务器搭建与客户端安装  http://www.linuxidc.com/Linux/2014-05/101830.htm 

Git 概述 http://www.linuxidc.com/Linux/2014-05/101829.htm 

分享实用的GitHub 使用教程 http://www.linuxidc.com/Linux/2014-04/100556.htm 

Ubuntu下Git服务器的搭建与使用指南  http://www.linuxidc.com/Linux/2015-07/120617.htm

Git 的详细介绍:请点这里
Git 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-11/124946.htm

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款