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.
[root@localhost ~]# yum install git
Check the Git version
[root@localhost ~]# git --version git version 1.8.3.1 [root@localhost ~]#
Set the below Parameters to avoid commit errors.
[root@localhost network-scripts]# git config --global user.name "Pradeep" [root@localhost network-scripts]# git config --global user.email "[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.
[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]#
As we can see that “.git” directory is created in the folder.
Step:3 Take the backup using below commands
[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]#
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.
[root@localhost network-scripts]# git log commit 1269758e5f5b2fa3e0ad1fe507abaf73b646a33d Author: Pradeep <[email protected]> Date: Mon Oct 26 00:03:08 2015 -0400 Changes on 26 Oct 2015 [root@localhost network-scripts]#
Note: Try inserting some junk characters inside the file “ifcfg-enp0s3”
Step:4 Now Restore network config file from git database
[root@localhost network-scripts]# git reset --hard 1269758e5f5b2fa3e0ad1fe507abaf73b646a33d HEAD is now at 1269758 Changes on 26 Oct 2015 [root@localhost 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 Read : 9 ‘diff’ Command Examples in Linux
Why not install etckeeper? — it manages the whole of /etc for you, automatically updates on installation/removal of packages, supports git, hg, svn, etc
Indeed, I was going to suggest the same.
‘http://etckeeper.branchable.com/’
Thanks, very useful tutorial.