git+rsync+邮件通告

来源:互联网 发布:淘宝网男徒步凉鞋 编辑:程序博客网 时间:2024/05/21 11:33

一、GIT

Git是一款免费、开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目。

Git是一个开源的分布式版本控制系统,可以有效、高速的处理从很小到非常大的项目版本管理。

Git
分布式相比于集中式的最大区别在于开发者可以提交到本地,每个开发者通过克隆(git clone),

在本地机器上拷贝一个完整的Git仓库。


Git的强大就不详细讲解。博主对Git的学习还待提高。

二、rsync

rsync是类unix系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,支持本

地复制,或者与其他SSH、rsync主机同步。


它的特性如下:
可以镜像保存整个目录树和文件系统。
可以很容易做到保持原来文件的权限、时间、软硬链接等等。
无须特殊权限即可安装。
快速:第一次同步时 rsync 会复制全部内容,但在下一次只传输修改过的文件。rsync 在传输数据的过程

中可以实行压缩及解压缩操作,因此可以使用更少的带宽。
安全:可以使用scp、ssh等方式来传输文件,当然也可以通过直接的socket连接。
支持匿名传输,以方便进行网站镜象。

三、项目需求

本博主从事的是Linux运维,平时为了提高效率,喜欢编写脚本,减轻工作量。将开发人员的代码实现快

速发布。

在出现问题时能快速回滚版本,快速处理代码带来的故障,实现业务正常运行。

四、shell脚本编写

本次利用的是远端的平台是GitHub。就不在本地搭建了,也方便测试。

使用GitHub账户,在上面发布项目,作为Git仓,从其拉取代码。

!/bin/bash#author:aizhen#mail:997822785@qq.com #博主信息,如有问题,请联系user=$(whoami)send_user=997822785@qq.comdate=$(date "+%F %H:%M:%S")work_dir=/deploydeploy="abc"WEB="node1.aizhen.com node2.aizhen.com"#部署版本后发送邮件send_mail(){echo -e "$1\noperator:$user" | mail -s "$2" $send_user}#回滚版本发送邮件send_mail_roll(){echo -e "$1\noperatoe:$user" | mail -s "$2" $send_user}#检查网站是否正常check_web(){for w in $WEB;dostatus_code=$(curl -o /dev/null -s -w %{http_code} http://$w)if [ $status_code -eq 200 -o $status_code -eq 301 -o $status_code -eq 302 -o $status_code -eq 304 ];thenecho "$w is ok"elseecho "$w is terrible\nplease check your index.html"fidone}#从远端拉取版本信息并向管理员发邮件告知git_pull(){if [ ! -d $work_dir ];thenmkdir $work_dircd $work_direlsecd $work_dirfiif [ ! -d $deploy ];thengit clone git@github.com:likehuan/abc.gitelsecd $deploygit pullfisend_mail "clone or git pull $date" "git pull"}#给版本添加tag,并向管理员发邮件告知tag信息git_add_tag(){cd $work_dir/$deploygit pulltag_count=$(($(git tag | wc -l)+1))tag_version="v$tag_count.0"git tag -a $tag_version -m "create $tag_version"git pushgit push --taggit_deploysend_mail "add tag $tag_version && deploy $tag_version" "add && deploy"}#查看目前所有版本信息git_list_tag(){cd $work_dir/$deploygit pulltag_num=$(git tag | wc -l)if [ $tag_num -eq 0 ];thenecho "please create your git tag"elsegit tagfi}#删除版本信息git_del_tag(){cd $work_dir/$deploygit pull        tag_count1=$(git tag | wc -l)        if [ $tag_count1 -eq 0 ];then                echo "you did not create git tag"        else        del_tag=$(git tag)        echo "please select you want to delete your version"                select d in $del_tag;do                        git tag -d $d                        git push origin :refs/tags/$d                        git commit -m "delete tag $d"                        git push -f                        git_deploy                        send_mail "delete tag $d $user && deploy $d" "delete tag && deploy"                        break                done        fi}#部署版本同步至服务器git_deploy(){rsync -vzrtopg --progress $work_dir/$deploy/* 192.168.40.129:/lkh/cd $work_dir/$deploy        ssh 192.168.40.129 ln -s /lkh/ /var/www/html}#回滚版本(快速回滚到上个版本和选择回滚),并且发邮件告知管理管理员git_reset(){cd $work_dir/$deploylast_version=$(git tag | tail -n 2 | head -n 1)echo "please you want to roll back version"select reset in "last_version" "other_version";docase $reset inlast_version)git reset --hard HEAD^git commit -m "roll back $last_version"git push -fgit_deploysend_mail_roll "roll back last_version $last_version && deploy $other" "roll back && deploy"break;;other_version)other_ver=$(git tag)select other in $other_ver;doecho "please select version which you want to roll back version"git reset --hard $othergit commit -m "roll back $other"git push -fgit_deploysend_mail "roll back other_version $other && deploy $other" "roll back && deploy"breakdone;;esacbreakdone}main(){echo "bash $0 git_pull|git_add_tag|git_list_tag|git_del_tag|git_deploy|git_reset"}$1

五、总结

博主对Git的了解属于学习状态,如若存在问题,请赐教。

本文为博主原创博文,如需要转载本文,请添加原文出处。谢谢!