Using Git/GitHub for Linux configuration management

Create a public repository in GitHub

https://help.github.com/articles/create-a-repo/

  

 
root@web01:/home/sree/conf_git# git clone https://github.com/stackerboy/firstrepo.git
Cloning into ‘firstrepo’…remote: Counting objects: 15, done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 15 (delta 0), reused 9 (delta 0), pack-reused 0
Unpacking objects: 100% (15/15), done.
Checking connectivity… done..


This command will clone your repository created on GitHub to your local directory

my local directory

root@web01:/home/sree/conf_git#

For illustration purpose what we are going to do here is to use git to save the configuration files of Nginx proxy server.
The file needs to be in git directory for the git to track the changes .What we do here is to move the reverse-proxy.conf from /etc/nginx/sites-available/reverse-proxy.conf to /home/sree/conf_git/firstrepo and create a softlink in /etc/nginx/sites-available/reverse-proxy.conf so that we can find the file in the right configuration directory ,this serves two purpose.


root@web01:/etc/nginx/sites-available# ll
total 16
drwxr-xr-x 2 root root 4096 Feb 19 05:46 ./
drwxr-xr-x 6 root root 4096 Jan 15 09:19 ../
-rw-r--r-- 1 root root 2074 Apr 26 2016 default
-rw-r--r-- 1 root root 2074 Jan 15 05:51 default_bkp
lrwxrwxrwx 1 root root 48 Feb 19 05:45 reverse-proxy.conf -> /home/sree/conf_git/firstrepo/reverse-proxy.conf


Status check
1) Created Git repo
2) Clone git repo to local server
3) Created a soft link and moved file to git directory

Our config file is in

root@web01:/home/sree/conf_git/firstrepo# cat reverse-proxy.conf
http {
upstream backend {
server 192.168.1.20;
server 192.168.1.21;
server 192.0.0.1 backup;
}
server {
location / {
proxy_pass http://backend;
}
}
}

Now we can start playing with git

Will open reverse-proxy.conf and edit file.

root@web01:/home/sree/conf_git/firstrepo# cat reverse-proxy.conf
http {
upstream backend {
server 192.168.1.20;
server 192.168.1.21;
server 192.0.0.1 backup;
}
server {
location / {
proxy_pass http://backend;
}
}
}
##
i have added the last line here to test
 
[simterm]
root@web01:/home/sree/conf_git/firstrepo# git status
HEAD detached at 4866d61
Changes not staged for commit:
(use “git add …” to update what will be committed)
(use “git checkout --- …” to discard changes in working directory)
modified: reverse-proxy.conf
no changes added to commit (use “git add” and/or “git commit -a”)
[/simterm
Shows file is modified
next step is to add,commit and push to git public repo.
[simterm]
root@web01:/home/sree/conf_git/firstrepo# git add reverse-proxy.conf
root@web01:/home/sree/conf_git/firstrepo# git status
HEAD detached at 4866d61
Changes to be committed:
(use “git reset HEAD …” to unstage)
modified: reverse-proxy.conf

shows there are changes which needs to be commited to local git.

root@web01:/home/sree/conf_git/firstrepo# git commit -m “changed reverse-proxy.conf to test git”
[detached HEAD db3fffc] changed reverse-proxy.conf to test git
1 file changed, 1 insertion(+), 4 deletions(-)

Pushing changes to remote repo

root@web01:/home/sree/conf_git/firstrepo# git push origin master
Username for ‘https://github.com’: stackerboy
Password for ‘https://stackerboy@github.com’:
Counting objects: 3, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 327 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To https://github.com/stackerboy/firstrepo.git
4866d61..985f6be master -> master

Any changes can be pushed and pull back to the local repository.

Accidental deletion of configuration files or going back to last known good configuration

find the latest best commit id


root@web01:/home/sree/conf_git/firstrepo# rm reverse-proxy.conf
root@web01:/home/sree/conf_git/firstrepo# ll
total 28
drwxr-xr-x 3 root root 4096 Feb 19 09:54 ./
drwxr-xr-x 4 root root 4096 Feb 19 09:48 ../
-rw-r–r– 1 root root 32 Feb 19 09:48 1
drwxr-xr-x 8 root root 4096 Feb 19 09:50 .git/
-rw-r–r– 1 root root 32 Feb 19 09:48 README
-rw-r–r– 1 root root 48 Feb 19 09:48 README.md
-rw-r–r– 1 root root 220 Feb 19 09:48 site_avail_rev_git
lrwxrwxrwx 1 root root 45 Feb 19 09:48 site_avail_rev_git_symlink -> /etc/nginx/sites-available/reverse-proxy.conf


Deleted reverse-proxy.conf file

To get it back attach head to last know configuration.

root@web01:/home/sree/conf_git/firstrepo# git checkout — 985f6be1a1da2a4b9ef82a93fda98b92aba4ad79
error: pathspec ‘985f6be1a1da2a4b9ef82a93fda98b92aba4ad79’ did not match any file(s) known to git.
root@web01:/home/sree/conf_git/firstrepo# git checkout 985f6be1a1da2a4b9ef82a93fda98b92aba4ad79
D reverse-proxy.conf
Note: checking out ‘985f6be1a1da2a4b9ef82a93fda98b92aba4ad79’.

 

You are in ‘detached HEAD’ state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

git checkout -b

HEAD is now at 985f6be… changes reverse-proxy.conf to test git
root@web01:/home/sree/conf_git/firstrepo# ll
total 28
drwxr-xr-x 3 root root 4096 Feb 19 09:54 ./
drwxr-xr-x 4 root root 4096 Feb 19 09:48 ../
-rw-r–r– 1 root root 32 Feb 19 09:48 1
drwxr-xr-x 8 root root 4096 Feb 19 09:56 .git/
-rw-r–r– 1 root root 32 Feb 19 09:48 README
-rw-r–r– 1 root root 48 Feb 19 09:48 README.md
-rw-r–r– 1 root root 220 Feb 19 09:48 site_avail_rev_git
lrwxrwxrwx 1 root root 45 Feb 19 09:48 site_avail_rev_git_symlink -> /etc/nginx/sites-available/reverse-proxy.conf

Checkout the file from the repo.


root@web01:/home/sree/conf_git/firstrepo# git checkout — reverse-proxy.conf
root@web01:/home/sree/conf_git/firstrepo# ll
total 32
drwxr-xr-x 3 root root 4096 Feb 19 09:58 ./
drwxr-xr-x 4 root root 4096 Feb 19 09:48 ../
-rw-r–r– 1 root root 32 Feb 19 09:48 1
drwxr-xr-x 8 root root 4096 Feb 19 09:58 .git/
-rw-r–r– 1 root root 32 Feb 19 09:48 README
-rw-r–r– 1 root root 48 Feb 19 09:48 README.md
-rw-r–r– 1 root root 258 Feb 19 09:58 reverse-proxy.conf
-rw-r–r– 1 root root 220 Feb 19 09:48 site_avail_rev_git
lrwxrwxrwx 1 root root 45 Feb 19 09:48 site_avail_rev_git_symlink -> /etc/nginx/sites-available/reverse-proxy.conf

Recovered the file .


root@web01:/home/sree/conf_git/firstrepo# cat reverse-proxy.conf
#http {
upstream backend {
server 192.168.1.20;
server 192.168.1.21;
# server 192.0.0.1 backup;
}
server {
location / {
proxy_pass http://backend;
}
}
#}
########### edit to test git ########

Posted in Cloud, Uncategorized.

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *