Pages

Subscribe:

Sunday, July 31, 2016

Webserver/Appserver config backup with Git Script

In production environment there are always backup scheduled for the entire servers using the AMI or snapshots which take the system backup over a period of time and runs daily in the non peak hours.

But there are environments such as non-prod where many people have access to the config so that they can tune this according to there requirements. While this makes things to speed up but since no one is accountable for the changes this can simply go on breaking up the configuration due to multiple changes being performed.

Now you can always troubleshoot the changes but sometimes there are situation when the hardware or dns problems might arise which is outside your control. In those cases you can't keep your environment down since this would affect the testing in the lower environments.



To overcome this problem  you can configure the backup of the configuration file or files according to your requirement. Git tracks for the changes and manages the different versions of the file if its changed. So this holds an additional benefit that you will be able to track what changes where made and when. Also you can revert back the entire configuration  based on the versions or you can revert to just a day back or week back based on your requirement.

Backing up configurations using the Git is inexpensive and can be done on any directory. Even the entire code in the directory or other files can be backed using the git script below.

 #!/bin/bash  
 dir=$1
 repo=$2
 hostname=`echo `hostname``
 git_check=`git --version`
 $git_check
 git_status=$?
 if [ $git_status -eq 0 ]
 then
 echo "Git is installed"
 cd $dir
 git config --global user.name `whoami`
 git config --global user.email "$hostname"
 origin_check=`git remote -v | grep push | wc -l`
 if [ $origin_check -eq 1 ]
 then
 echo "Origin exist $origin_check"
 else
 git remote add origin [email protected]:"$repo"
 fi
 git init
 gitignore_check=`ls -ltr $dir/.gitignore | wc -l`
 if [ $gitignore_check -eq 1 ]
 then
 echo "Gitignore is present"
 else
 echo "*log*" > $dir/.gitignore
 echo "Added gitignore"
 fi
 git checkout -b `hostname` > /dev/null
 git add .
 echo "####Changes for `date`#####" >> changedfiles.txt
 git status >> changedfiles.txt
 git add .
 git commit -m "Automated commit on `date`"
 git push origin `hostname`
 else
 echo "Git is not installed, git is required for this script to work"
 fi

0 comments:

Post a Comment