Use Git to take Backup of Configuration files on Linux

Sometimes while working on Linux servers we need to take backup of configuration files, traditional way of taking backup is to copy the same file with different name or insert some characters at end that file.

But using Git we can easily manage the backup of configuration files. In this post we will demonstrate how this can be achieved using Git. I have done below testings on CentOS 7 & RHEL 7.

Step:1 Install Git, if it is not installed.

[[email protected] ~]# yum install git

Check the Git version

[[email protected] ~]# git --version
git version
[[email protected] ~]#

Set the below Parameters to avoid commit errors.

[[email protected] network-scripts]# git config --global "Pradeep"
[[email protected] network-scripts]# git config --global "[email protected]"

You can replace the user name as per your environment.

Step:2 Now initialize the git database.

As i am going to take backup of network configuration file, for that i have to initialize the Git database in network configuration directory only.

[[email protected] ~]# cd /etc/sysconfig/network-scripts
[[email protected] network-scripts]# git init
Initialized empty Git repository in /etc/sysconfig/network-scripts/.git/
[[email protected] network-scripts]#

As we can see that “.git” directory is created in the folder.

Step:3 Take the backup using below commands

[[email protected] network-scripts]# git add ifcfg-enp0s3
[[email protected] network-scripts]#
[[email protected] 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
[[email protected] network-scripts]#

When we execute the second command it will ask you to put some comments like “Changes on 26 Oct 2015” and save the file.

Use below command to view the git logs.

[[email protected] network-scripts]# git log
commit 1269758e5f5b2fa3e0ad1fe507abaf73b646a33d
Author: Pradeep <[email protected]>
Date: Mon Oct 26 00:03:08 2015 -0400
Changes on 26 Oct 2015
[[email protected] network-scripts]#

Note: Try inserting some junk characters inside the file “ifcfg-enp0s3


Step:4 Now Restore network config file from git database

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

Use the same git id as shown in above. Git id will different for your setup.

Verify whether the file is correctly restore or not from git database.


Also Read9 ‘diff’ Command Examples in Linux

3 thoughts on “Use Git to take Backup of Configuration files on Linux”

  1. Why not install etckeeper? — it manages the whole of /etc for you, automatically updates on installation/removal of packages, supports git, hg, svn, etc


Leave a Comment