1. 程式人生 > 其它 >Linux 下 SVN 的安裝和配置

Linux 下 SVN 的安裝和配置

SVN 是一個自由開源的版本管理系統,它可以按照時間的順序去管理檔案、目錄以及對其進行的修改。於今,它被廣泛的用於網際網路公司的專案版本管理中

工作原理

它的工作原理如下圖所示

它是由一個SVN伺服器和許多的SVN客戶端組成

資料統一儲存在SVN伺服器上

客戶端 從伺服器檢出(checkout)指定路徑上的版本檔案到本地,修改了之後再提交(commit)到伺服器上,當其他的客戶端再次檢出或更新的時候,就能獲取得到之前客戶端提交的修改

這樣,多個客戶端就可以互不干擾的工作,實現了多人的協作

SVN已經是一個非常成熟且能快速實現專案版本管理的工具了,很多中小團隊中都在使用,下面介紹下SVN伺服器的安裝和配置

安裝

yum install -y subversion

安裝完成之後,執行 svn --version 檢視是否安裝成功,如果有類似下面的輸出則表示安裝成功

[root@cghost21 ~]# svn --version
svn, version 1.7.14 (r1542130)
   compiled Sep 30 2020, 17:44:04

Copyright (C) 2013 The Apache Software Foundation.
This software consists of contributions made by many people; see the NOTICE
file for more information.
Subversion is open source software, see http://subversion.apache.org/

The following repository access (RA) modules are available:

* ra_neon : Module for accessing a repository via WebDAV protocol using Neon.
  - handles 'http' scheme
  - handles 'https' scheme
* ra_svn : Module for accessing a repository using the svn network protocol.
  - with Cyrus SASL authentication
  - handles 'svn' scheme
* ra_local : Module for accessing a repository on local disk.
  - handles 'file' scheme

[root@cghost21 ~]# 

預設目錄

  • svnserve 安裝目錄

svnserve 預設安裝在 /usr/bin 目錄下,可通過 which svnserve 命令檢視

[root@ecs-centos-7 ~]# which svnserve
/usr/bin/svnserve
  • 倉庫地址

svnserve 預設的倉庫地址是 /var/svn , 通過 /etc/sysconfig/svnserve 配置檔案可以修改

[root@ecs-centos-7 ~]# cat /etc/sysconfig/svnserve 
# OPTIONS is used to pass command-line arguments to svnserve.
# 
# Specify the repository location in -r parameter:
OPTIONS="-r /var/svn"
  • svnserve 埠

svnserve 啟動之後,預設使用 3690 埠

[root@ecs-centos-7 test_a]# netstat -anpt | grep svnserve
tcp    0    0 0.0.0.0:3690    0.0.0.0:*   LISTEN      28347/svnserve

配置

  • 建立倉庫

安裝完成之後,使用 svnadmin create 倉庫目錄 來建立一個新倉庫

我們在 /home/tt 目錄下建立一個名為svn的倉庫,以後所有的專案檔案都放在這個目錄下,建立成功之後在svn目錄下多了幾個子目錄

[root@ecs-centos-7 tt]# svnadmin create /home/tt/svn
[root@ecs-centos-7 tt]# ls svn/
conf  db  format  hooks  locks  README.txt

conf 目錄是存放配置檔案的

[root@ecs-centos-7 tt]# cd svn/conf/
[root@ecs-centos-7 conf]# ls
authz  passwd  svnserve.conf

authz 是使用者許可權控制檔案

passwd 是賬號密碼配置檔案

svnserve.conf 是svnserve服務配置檔案

  • 配置使用者名稱密碼

編輯 passwd 配置檔案,新增ta使用者名稱和123456密碼、 tb使用者名稱和12345密碼以及tc使用者名稱和123密碼

[root@ecs-centos-7 conf]# vim passwd 
### This file is an example password file for svnserve.
### Its format is similar to that of svnserve.conf. As shown in the
### example below it contains one section labelled [users].
### The name and password for each user follow, one account per line.

[users]
# harry = harryssecret
# sally = sallyssecret
ta = 123456
tb = 12345
tc = 123
  • 配置使用者許可權

為 SVN 使用者 ta 配置讀寫許可權,為 SVN使用者 tb 配置只讀許可權,為 SVN使用者 tc 配置只讀許可權

上圖中是配置倉庫的根目錄訪問許可權,如果想配置倉庫中指定目錄的訪問許可權,可以在末尾增加一個倉庫目錄,目錄下面再配置使用者的訪問許可權即可

比如:倉庫根目錄下有一個myproject的目錄,使用者ta有讀寫許可權,使用者tb無訪問許可權,使用者tc有隻讀許可權,則 myproject 目錄的許可權配置如下

[/myproject]
ta=rw
tc=r
*=
  • 配置 svnserve 服務

如上圖所示,開啟紅框中的選項前的註釋,注意:每個選項前都不能有空格

anon-access = none   # 匿名使用者無法訪問
auth-access = write   #授權使用者可寫
password-db = passwd  #使用者密碼驗證檔案
authz-db = authz      #使用者許可權驗證檔案
realm = /home/tt/svn  #svn版本庫目錄

啟動、停止、重啟

修改 /etc/sysconfig/svnserveOPTIONS 的值為我們新建立的倉庫地址

[root@ecs-centos-7 ~]# vim /etc/sysconfig/svnserve 
# OPTIONS is used to pass command-line arguments to svnserve.
#
# Specify the repository location in -r parameter:
OPTIONS="-r /home/tt/svn"

svn安裝完成之後,預設已經新增到systemctl中了,使用以下命令啟動、停止、重啟

[root@ecs-centos-7 ~]# systemctl start svnserve
[root@ecs-centos-7 ~]# systemctl stop svnserve
[root@ecs-centos-7 ~]# systemctl restart svnserve

在實際的部署中,為了重啟機器時,不影響客戶端的使用,svnserve通常會設定成開機啟動

[root@ecs-centos-7 ~]# systemctl enable svnserve
Created symlink from /etc/systemd/system/multi-user.target.wants/svnserve.service to /usr/lib/systemd/system/svnserve.service.

客戶端檢出

配置好使用者密碼以及訪問許可權之後,重新啟動 svnserve

新建test_a 目錄,進入該目錄,執行 svn co svn://192.168.0.9 --username ta --password 123456 命令檢出svn倉庫中的檔案以及目錄

[root@ecs-centos-7 tt]# mkdir test_a
[root@ecs-centos-7 tt]# cd test_a/
[root@ecs-centos-7 test_a]# svn co svn://192.168.0.9 --username ta --password 123456
[root@ecs-centos-7 test_a]# svn info
路徑: .
工作副本根目錄: /home/tt/test_a
URL: svn://192.168.0.9
版本庫根: svn://192.168.0.9
版本庫 UUID: c9730d20-968c-41c0-a224-4c6a967f8330
版本: 1
節點種類: 目錄
排程: 正常
最後修改的作者: ta
最後修改的版本: 1
最後修改的時間: 2020-12-04 23:48:11 +0800 (五, 202-12-04)

適用的場景

每個工具都有其適用的場景,SVN也不例外

當你需要記錄檔案每一次的修改時間,檢視隨著時間變化的日誌,追溯和回退到任意一次修改時間點時的版本,多人協作一起開發並追蹤是誰進行了修改,那麼是很適合使用SVN

對於一些比較大的或者後續不會有任何修改的檔案、把SVN當做檔案傳輸的中轉站,這些都不適合用SVN,比如:從一個SVN客戶端上傳一部電影到SVN伺服器上,然後在另一個SVN客戶端更新這個電影檔案,這是誤用SVN這個工具了,上述檔案的同步有專門的工具(rsync)來處理

小結

本章介紹了SVN伺服器的工作原理、安裝、配置、使用場景等,關於更多SVN相關的知識可以參考官方文件

對於中小創業團隊,在事情多,人員少,時間緊的情況下,具有安裝簡單、使用方便、易上手(特別是對於非技術人員,稍微講解下就能上手使用)等特點的SVN是非常好的選擇