你需要学会使用Git备份Linux配置文件

系统 Linux 系统运维
我们使用Linux服务器时,有时候需要备份配置文件。如果我们使用Git,可以很轻松的管理配置文件的备份。在这篇文章中,我将会演示如何使用Git来实现备份以及管理备份。

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

[[155190]]

  但是,如果我们使用 Git ,我们可以很轻松的管理配置文件的备份。在这篇文章中,我将会演示如何使用 Git 来实现备份以及管理备份。我测试所用的环境是 CentOS 7 和 RHEL 7。

  一、安装 Git  

  1. [root@localhost ~]# yum install git 

  检查Git版本

 

  1. [root@localhost ~]# git --version  
  2. git version 1.8.3.1  
  3. [root@localhost ~]# 

  设置初始参数

  将如下命令中的用户名,邮件替换成你自己的。

 

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

  二、现在初始化 Git 数据库

  因为我准备备份网络配置文件,所以我只需要在网络配置文件的目录初始化Git数据库。

 

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

  命令行输入 ls -a , 那么我们可以看到,“.git” 文件夹被创建了。

  三、使用下面的命令进行备份

 

  1. [root@localhost network-scripts]# git add ifcfg-enp0s3  
  2. [root@localhost network-scripts]#  
  3. [root@localhost network-scripts]# git commit ifcfg-enp0s3  
  4. [master (root-commit) 1269758] Changes on 26 Oct 2015  
  5. 1 file changed, 16 insertions(+)  
  6. create mode 100644 ifcfg-enp0s3  
  7. [root@localhost network-scripts]# 

  当我们执行第二个命令的时候,它会要求你输入像 “Changes on 26 Oct 2015” 这样的备注,然后保存文件。

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

 

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

  注意:尝试在 “ifcfg-enp0s3” 文件中插入一些无用字符。  

  四、从 Git 数据库恢复网络配置文件

 

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

  使用与上边相同的 git id,你安装的不同,git 的 id 也不同。

  验证从 git 数据库恢复的文件是否正确。

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

责任编辑:火凤凰 来源: Linux Story
相关推荐

2009-11-05 15:25:36

WCF服务端配置

2021-05-12 07:50:05

Linux运维Linux系统

2022-09-13 12:10:04

unlink删除文件

2013-12-19 09:20:59

2011-01-13 16:27:26

Linux配置文件

2020-04-09 10:49:19

VMware主机配置

2021-07-28 08:53:53

GoGDB调试

2021-10-09 10:50:30

JavaScript编程开发

2024-03-14 08:05:30

2024-03-19 00:10:00

aiofilesPython开发

2023-10-30 07:05:31

2021-05-30 07:56:51

QSettingsLog4Qt变量

2022-06-12 16:05:22

vimLinux

2010-06-17 21:34:05

2010-02-02 18:19:52

Linux mplay

2010-12-27 14:59:31

Outlook 配置文

2021-04-01 10:23:45

SpringBootbootstrapapplication

2010-08-03 11:29:53

路由器配置

2021-07-05 12:09:58

Python编程语言

2009-08-18 10:56:40

Linux网卡配置Linux网卡配置
点赞
收藏

51CTO技术栈公众号