1 #!/bin/bash 2 # 3 # 在Linux(CentOS 6.6)服务器上安装并配置基于Apache的SVN服务器: 4 # 5 # 1、安装服务 6 # 2、创建svn版本库 7 # 3、创建svn用户 8 # 4、配置svn权限 9 # 5、配置http访问 10 # 11 12 # 13 # 1、安装服务 14 # 15 16 # 1.1、安装程序包:subversion httpd mod_dav_svn 17 18 yum install subversion httpd mod_dav_svn -y >> $0.log 19 if [[ $? != 0 ]] 20 then 21 echo "安装服务失败,停止svn部署" 22 exit 1 23 fi 24 25 # 26 # 2、创建svn版本库 27 # 28 29 # 2.1、定义svn版本库存放目录 30 svn_path=/svn 31 # 2.2、创建svn版本库存放目录 32 mkdir -p $svn_path 33 # 2.3、定义要创建版本库的名称 34 repo_name=mysvn5 35 # 2.4、创建新的版本库 36 if [[ -d $svn_path/$repo_name ]] 37 then 38 echo "版本库 $repo_name 已经存在" 39 exit 1 40 fi 41 svnadmin create $svn_path/$repo_name 42 # 2.5、配置svn版本库的属主属组为http服务的启动用户 43 chown apache.apache -R $svn_path 44 # 2.6、确认新创建的svn版本库已经生成,且文件属主属组正确 45 ls -l $svn_path/$repo_name >> $0.log 46 47 # 48 # 3、创建svn用户 49 # 50 51 # 3.1、创建svn管理员账号 52 admin_passwd=$(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c9) 53 htpasswd -c -b -m $svn_path/$repo_name/conf/passwd admin $admin_passwd 54 echo "$repo_name 版本库的管理员账号为 admin ,密码是 $admin_passwd " |tee -a $0.log 55 56 # 3.2、创建svn具有读写权限的账号 57 rw_user_passwd=$(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c9) 58 htpasswd -b -m $svn_path/$repo_name/conf/passwd rw_user $rw_user_passwd 59 echo "$repo_name 版本库的读写账号为 rw_user ,密码是 $rw_user_passwd " |tee -a $0.log 60 61 # 3.3、创建svn具有只读权限的账号 62 ro_user_passwd=$(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c9) 63 htpasswd -b -m $svn_path/$repo_name/conf/passwd ro_user $ro_user_passwd 64 echo "$repo_name 版本库的只读账号为 ro_user ,密码是 $ro_user_passwd " |tee -a $0.log 65 66 # 67 # 4、配置svn权限 68 # 69 70 # 4.1、设置权限节点 71 echo "[$repo_name:/]" >>$svn_path/$repo_name/conf/authz 72 # 4.2、配置管理员账号有“读写”权限 73 echo "admin = rw" >>$svn_path/$repo_name/conf/authz 74 # 4.3、配置读写账号有“读写”权限 75 echo "rw_user = rw" >>$svn_path/$repo_name/conf/authz 76 # 4.4、配置只读账号有“只读”权限 77 echo "ro_user = r" >>$svn_path/$repo_name/conf/authz 78 79 # 80 # 5、配置http访问 81 # 82 83 echo '<Location /svn/'$repo_name'>' >> /etc/httpd/conf.d/subversion.conf 84 # 定义svn类型的http服务,会调用dav_svn_module 85 echo ' DAV svn' >> /etc/httpd/conf.d/subversion.conf 86 # svn版本库目录 87 echo ' SVNPath '$svn_path'/'$repo_name'' >> /etc/httpd/conf.d/subversion.conf 88 # svn访问权限的配置文件 89 echo ' AuthzSVNAccessFile '$svn_path'/'$repo_name'/conf/authz' >> /etc/httpd/conf.d/subversion.conf 90 # svn使用http基本验证方式验证账号身份 91 echo ' AuthType Basic' >> /etc/httpd/conf.d/subversion.conf 92 # http基本验证的提示信息 93 echo ' AuthName "Authorization repot SVN"' >> /etc/httpd/conf.d/subversion.conf 94 # svn账号配置文件,包含加密后的密码信息 95 echo ' AuthUserFile '$svn_path'/'$repo_name'/conf/passwd' >> /etc/httpd/conf.d/subversion.conf 96 # 仅允许通过验证的用户访问svn 97 echo ' Require valid-user' >> /etc/httpd/conf.d/subversion.conf 98 echo '</Location>' >> /etc/httpd/conf.d/subversion.conf 99 100 # 重启http服务 101 /etc/init.d/httpd restart 102 # 设置http服务开机启动 103 chkconfig httpd on 104 # 在防火墙上开放http服务 105 iptables -I INPUT -p tcp --dport 80 -m state --state NEW -j ACCEPT 106 /etc/init.d/iptables save 107 # 关闭selinux 108 setenforce 0 109 110 # 111 # 6、提交代码时必须写注释,且不能少于10个字符;允许修改注释 112 # 113 114 cp $svn_path/$repo_name/hooks/pre-commit.tmpl $svn_path/$repo_name/hooks/pre-commit 115 116 sed -i '75a LOGMSG=$($SVNLOOK log -t "$TXN" "$REPOS" | grep "[a-zA-Z0-9]" | wc -c) if [ "$LOGMSG" -lt 10 ]; then echo -e " 提交文件时必须添加注释,提交中止."1>&2 exit 1 fi' $svn_path/$repo_name/hooks/pre-commit 117 118 sed -i 's/^commit-access-control.pl/#commit-access-control.pl/' $svn_path/$repo_name/hooks/pre-commit 119 120 chmod +x $svn_path/$repo_name/hooks/pre-commit 121 122 cp $svn_path/$repo_name/hooks/pre-revprop-change.tmpl $svn_path/$repo_name/hooks/pre-revprop-change 123 124 chmod +x $svn_path/$repo_name/hooks/pre-revprop-change