Created
December 18, 2012 07:04
-
-
Save JerryBian/4325722 to your computer and use it in GitHub Desktop.
shell script for blog hosting on VPS, powered by Jekyll and GitHub. This script can do the following:
1).keeps sync with github about my blog source and data.
2).uses jekyll to generate the HTML of my blog.
3).sends a email if Jekyll exists with an error.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#function: send a email when error occurs | |
send_email_and_exit(){ | |
recipient=$1 | |
msg=$2 | |
#send email | |
/bin/mail -s "blog generation failure" "${recipient}" << EOF | |
${msg} | |
EOF | |
#print info | |
echo "sending email and exiting due to error" | |
} | |
#function: check if git dir is empty or not | |
git_dir_empty(){ | |
gitpath=$1 | |
gitbaseurl=$2 | |
githubrepo=$3 | |
if [ ! -d ${gitpath} ]; then | |
echo "checking out repo for the first time" | |
mkdir -p ${gitpath} | |
cd ${gitbaseurl} | |
git clone ${githubrepo} | |
else | |
cd ${gitpath} | |
git remote rm origin | |
git remote add origin ${githubrepo} | |
git pull | |
fi | |
} | |
#begin | |
echo "running at " `date` | |
#website basedir | |
basedir=/www/blog | |
#http://laobian.me | |
techdir=${basedir}/tech | |
gittechdir=${techdir}/archive | |
ghtechsourcerepo=https://github.com/JerryBian/blog_tech_source.git | |
ghtechdatarepo=https://github.com/JerryBian/blog_tech_data.git | |
#http://blog.laobian.me | |
lifedir=${basedir}/life | |
gitlifedir=${lifedir}/archive | |
ghlifesourcerepo=https://github.com/JerryBian/blog_life_source.git | |
ghlifedatarepo=https://github.com/JerryBian/blog_life_data.git | |
#set master email | |
emailto=ProjectBian@gmail.com | |
#tech source | |
git_dir_empty "${techdir}" "${basedir}" "${ghtechsourcerepo}" | |
#tech data | |
git_dir_empty "${gittechdir}" "${techdir}" "${ghtechdatarepo}" | |
#life source | |
git_dir_empty "${lifedir}" "${basedir}" "${ghlifesourcerepo}" | |
#life data | |
git_dir_empty "${gitlifedir}" "${lifedir}" "${ghlifedatarepo}" | |
#jekyll | |
jekyll ${techdir}/ | |
jekyll ${lifedir}/ | |
#error: generating html | |
exitCode=$? | |
if [ ${exitCode} != "0" ]; then | |
send_email_and_exit "${mailto}" "Jekyll failed with exit code ${exitCode}" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment