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)\nif [ "$LOGMSG" -lt 10 ]; then\necho -e "\n 提交檔案時必須新增註釋,提交中止."1>&2\nexit 1\nfi' $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