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.

 hostname=`echo `hostname``
 git_check=`git --version`
 if [ $git_status -eq 0 ]
 echo "Git is installed"
 cd $dir
 git config --global `whoami`
 git config --global "$hostname"
 origin_check=`git remote -v | grep push | wc -l`
 if [ $origin_check -eq 1 ]
 echo "Origin exist $origin_check"
 git remote add origin [email protected]:"$repo"
 git init
 gitignore_check=`ls -ltr $dir/.gitignore | wc -l`
 if [ $gitignore_check -eq 1 ]
 echo "Gitignore is present"
 echo "*log*" > $dir/.gitignore
 echo "Added gitignore"
 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`
 echo "Git is not installed, git is required for this script to work"


Post a Comment