公司機房環境不穩定,領導要求做SVN的實時異地備份,以保證數據安全。備份服務器非公司網絡,可遠程訪問。
前提條件:
1. 備份服務器需要安裝和主服務器相同的環境。例如,主服務器的環境爲 Apache2.4.15 + SVN1.8.17,那麼從服務器也需要設置相同的環境。
2. 主服務器能夠通過HTTP遠程訪問備份服務器的配置庫
首先搭建備份服務器環境,以下步驟在備份服務器(192.168.3.2)上執行
1. 安裝和配置Apache2.4.15,請參看http://blog.csdn.net/w171066/article/details/51093319 和 http://blog.csdn.net/w171066/article/details/51130324
2. 安裝SVN1.8.17,請參看http://blog.csdn.net/w171066/article/details/75019663
3. 添加同步用戶,此用戶用於讀寫備份後的配置庫
[root@localhost ~]# htpasswd -b /opt/svndata syncuser syncpass
Adding password for user syncuser
[root@localhost ~]# vi /opt/svndata/accessfile
[tools:/]
syncuser = rw
4.創建備份庫,創建並編輯pre-revprop-change 和start-commit hook
[svn@localhost svndata]$ svnadmin create tools
[svn@localhost svndata]$ cd tools/hooks
[svn@localhost svndata]$ vi pre-revprop-change
#!/bin/sh
USER="$3"
# 限制只有syncuser用戶才能提交版本屬性修改到此版本庫
if [ "$USER" = "syncuser" ]; then exit 0; fi
echo "Only the syncuser user may change revision properties" >&2
exit 1
[svn@localhost svndata]$ vi start-commit
#!/bin/sh
USER="$2" # 限制只用syncuser用戶才能提交版本修改到版本庫 if [ "$USER" = "syncuser" ]; then exit 0; fi echo "Only the syncuser user may commit new revisions" >&2 exit 1[svn@localhost svndata]$chmod 775 start_commit pre-revprop-change
回到主服務器,以下步驟在主服務器上進行
在主服務器上必須能夠通過http訪問備份庫.(可能需要關閉備份服務器上的防火牆或者開放相應端口)
1.初始化備份庫
[svn@localhost ~]$ svnsync init http://192.168.3.2:8000/opt/svndata/tools file:///opt/svndata/tools --username syncuser --password syncpass
Copied properties for revision 0.
2.同步備份庫
[root@localhost ~]# svnsync sync http://192.168.3.2:8000/opt/svndata/tools --username syncuser --password syncpass
這個步驟用時很長。執行完這步,配置庫就同步過去了。
3.修改post-commit hook, 以便每次執行完commit動作,都把修改同步到備份庫
[svn@localhost ~]$ vi tools/hook/post-commit
#!/bin/sh
/bin/svnsync synchronize --non-interactive http://192.168.3.2:8000/opt/svndata/tools --username syncuser --password syncpass