使用svn加rsync進行遠程web的更新

  1. 實驗架構圖如下:

wKioL1VUarGzpse-AAFzxS2dvG8791.jpg

  2.實驗環境需求(3臺主機即可,1臺服務器安裝svn加rsync,1臺web服務器,1臺客戶端機器)

     node2   192.168.21.233    centos 6.6  x64    svn(subversion) + rsync

     node5   192.168.21.222    centos 6.6 x64     LAMP(Linux+Apache+MySQL+php)

各軟件的作用:svn用來做版本控制的,因爲在公司內部上線一個項目時,在多人開發的情況下,都會經過多人修改,此時項目源碼的管理就變得非常重要了,在RHEL系列下有這樣一款軟件svn(subversion)

Subversion is a concurrent version control system which enables one or more users to collaborate in developing and maintaining a hierarchy of files and directories while keeping a history of all changes.  Subversion only stores the differences between versions,instead of every complete file.  Subversion is intended to be a compelling replacement for CVS.

Subversion是一個併發版本控制系統,使一個或多個用戶協作開發和維護一個文件和目錄的層次結構,同時保持所有更改的歷史。Subversion存儲版本之間的差異,而不是每一個完整的文件。Subversion確定爲強迫替換CVS。

百度百科對CVS的解釋:CVS(Concurrent Versions System)版本控制系統是一種GNU軟件包,主要用於在多人開發環境下源碼的維護。

rsync是用來將編寫好的源碼同步到遠程的web服務器上。


軟件包都通過yum方式來安裝   yum install package_name

[root@node5 ~]# rpm -q httpd
httpd-2.2.15-39.el6.centos.x86_64
[root@node5 ~]# rpm -q mysql-server
mysql-server-5.1.73-3.el6_5.x86_64
[root@node5 ~]# rpm -q php
php-5.3.3-40.el6_6.x86_64

[root@node2 ~]# rpm -q subversion
subversion-1.6.11-12.el6_6.x86_64
[root@node2 ~]# rpm -q rsync
rsync-3.0.6-12.el6.x86_64


  3.配置環境

[root@node2 ~]# rpm -ql subversion
...
/etc/rc.d/init.d/svnserve  服務啓動腳本
...
/usr/bin/svn   會常用到的命令
/usr/bin/svnadmin
...

/usr/share/man/man5/svnserve.conf.5.gz  配置文件手冊

....

客戶端訪問服務器時有三種方式:svnserve訪問、svnserve和ssh訪問、Apache間接訪問,這裏以svnserve方式來講解。


svnadmin - Subversion repository administration too  subversion的版本管理工具l    
SYNOPSIS    命令格式
       svnadmin command /path/to/repos [options] [args]
。。。。。。
       Run ‘svnadmin help’ to access the built-in tool documentation.  通過svnadmin查看工具的使用
       
NAME
       svn - Subversion command line client tool     subversion的命令行客戶端工具
SYNOPSIS   命令格式
       svn command [options] [args]       
。。。。。
             Run ‘svn help’ to access the built-in tool documentation.   查看命令的文檔


[root@node2 ~]# svnadmin help
general usage: svnadmin SUBCOMMAND REPOS_PATH  [ARGS & OPTIONS ...]
Type 'svnadmin help <subcommand>' for help on a specific subcommand.
Type 'svnadmin --version' to see the program version and FS modules.

Available subcommands:     svnadmin help  subcommand查看子命令的使用,比如:svnadmin help create
   crashtest
   create   創建一個新的版本庫
   deltify
   dump
   help (?, h)
   。。。

[root@node2 ~]# mkdir /svn/project
[root@node2 ~]# svnadmin create /svn/project   創建一個新的版本庫
[root@node2 ~]# ls -l /svn/project
total 24
drwxr-xr-x 2 root root 4096 May 15 08:39 conf
drwxr-sr-x 6 root root 4096 May 15 08:39 db
-r--r--r-- 1 root root    2 May 15 08:39 format
drwxr-xr-x 2 root root 4096 May 15 08:39 hooks
drwxr-xr-x 2 root root 4096 May 15 08:39 locks
-rw-r--r-- 1 root root  229 May 15 08:39 README.txt
[root@node2 ~]# ls /usr/local/nginx-1.4.7/auto/
cc          feature       headers  install  modules  os       summary
define      have          include  lib      nohave   sources  types
endianness  have_headers  init     make     options  stubs    unix
[root@node2 ~]# du -sh /usr/local/nginx-1.4.7/auto/
436K    /usr/local/nginx-1.4.7/auto/
導入源碼到版本庫中,這裏使用nginx的部分源碼來舉例,使用import子命令
-m [--message] ARG       : specify log message ARG    相當於說明性的子串
[root@node2 ~]# svn import /usr/local/nginx-1.4.7/auto/ file:///svn/project/ -m "Initialization of a project"
Adding         /usr/local/nginx-1.4.7/auto/nohave
Adding         /usr/local/nginx-1.4.7/auto/os
Adding         /usr/local/nginx-1.4.7/auto/os/linux
Adding         /usr/local/nginx-1.4.7/auto/os/win32
.......
Adding         /usr/local/nginx-1.4.7/auto/make
Adding         /usr/local/nginx-1.4.7/auto/have_headers

Committed revision 1.
[root@node2 ~]# svn list file:///svn/project/  列出版本庫中有哪些文件
cc/
define
endianness
feature
have
have_headers
headers
include
init
install
lib/
make
modules
nohave
options
os/
sources
stubs
summary
types/
unix
[root@node2 ~]# ls /svn/project/conf/
authz  passwd  svnserve.conf
客戶端在訪問版本庫服務器時,服務器會根據版本庫目錄下conf/svnserve.conf文件中的定義對用戶進行訪問控制
[root@node2 ~]# vim /svn/project/conf/svnserve.conf 查看man svnserve.conf有配置文件的詳細說明
。。。。  做如下修改
[general]
### These options control access to the repository for unauthenticated
### and authenticated users.  Valid values are "write", "read",
### and "none".  The sample settings below are the defaults.
anon-access = none #設置匿名賬號訪問權限,這裏爲none無權限
auth-access = write #經過認證的賬戶可寫
### The password-db option controls the location of the password
### database file.  Unless you specify a path starting with a /,
### the file's location is relative to the directory containing
### this configuration file.
### If SASL is enabled (see below), this file will NOT be used.
### Uncomment the line below to use the default password file.
password-db = passwd #賬號名稱與密碼的存放文件名
### The authz-db option controls the location of the authorization
### rules for path-based access control.  Unless you specify a path
### starting with a /, the file's location is relative to the the
### directory containing this file.  If you don't specify an
### authz-db, no path-based access control is done.
### Uncomment the line below to use the default authorization file.
authz-db = authz #基於路徑訪問的控制文件名,可以對文件或目錄設置權限
### This option specifies the authentication realm of the repository.
### If two repositories have the same authentication realm, they should
### have the same password database, and vice versa.  The default realm
### is repository's uuid.
realm = My First Repository  #設置版本庫域,如果兩個版本庫的認證域相同,他們將使用相同的密碼數據
[root@node2 ~]# vim /svn/project/conf/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
keepalived = keepalived  #用戶名 = 密碼
haproxy = haproxy
[root@node2 ~]# vim /svn/project/conf/authz
。。。   添加如下內容
[groups]
# harry_and_sally = harry,sally
# harry_sally_and_joe = harry,sally,&joe
operator = keepalived,haproxy   #指定一個operator組包括了剛纔的兩個用戶

[/]                  #設置版本庫的根路徑
@operator = rw       #指定組對版本庫的訪問權限爲讀寫
* = r                 #其他用戶的權限,權限可以設置爲 只讀 'r'  讀寫'rw'  無''
。。。
[root@node2 ~]# ls /svn/project/
conf  db  format  hooks  locks  README.txt
[root@node2 ~]# ls /svn/project/hooks/
post-commit.tmpl          post-unlock.tmpl  pre-revprop-change.tmpl
post-lock.tmpl            pre-commit.tmpl   pre-unlock.tmpl
post-revprop-change.tmpl  pre-lock.tmpl     start-commit.tmpl
在hooks目錄下,可以提供一個post-commit的可執行的shell腳本,每當結束對版本庫的編輯時,執行commit一下
,即會調用對應版本庫中hooks目錄下commit腳本執行,這裏因爲結合rsync來做同步,剛好可以使用到,等會後
面提供腳本的使用內容。

啓動svnserve服務
可以用svnserve命令來啓動,使用-d選項說明以守護進程方式運行svnserve,-r PATH 爲版本庫指定一個虛擬
路徑,默認svn會將服務器上所有的版本庫都共享給用戶,使用-r僅發佈個別版本給用戶,svnserve -d -r /svn/project也可以使用service start svnserve啓動svnserve,svnserve默認是監聽在tcp的3690端口



客戶端訪問

svn命令

[root@node2 ~]# svn help
usage: svn <subcommand> [options] [args]
。。。
Available subcommands:   這裏()括號中的可以說是簡寫
   add             #添加文件
   blame (praise, annotate, ann)
   cat             #查看文件的內容
   changelist (cl)   #查看改變的列表
   checkout (co)     #從服務器版本庫中複製一份副本到本地
   cleanup
   commit (ci)    #將本地工作副本修改後的內容發佈到版本中
   copy (cp)
   delete (del, remove, rm)    #刪除文件、目錄
   diff (di)             #對比兩個版本之間的差別
   export
   help (?, h)
   import    #導入一個源碼庫到版本庫中
   info
   list (ls)    #列出版本庫中的文件
   lock
   log
   merge
   mergeinfo
   mkdir

其他的子命令使用到都可以查看幫助手冊的,svn help  subcommand

[root@node2 ~]# svn checkout file:///svn/project nginx_source
A    nginx_source/nohave
A    nginx_source/os
A    nginx_source/os/linux
A    nginx_source/os/win32
A    nginx_source/os/darwin
A    nginx_source/os/conf
A    nginx_source/os/freebsd
A    nginx_source/os/solaris
A    nginx_source/unix
。。。
[root@node2 ~]# vim /svn/project/hooks/post-commit    這裏通過post-commit腳本

#!/bin/bash
REPOS="$1"    #版本庫的絕對路徑
REV="$2"      #對應更新到的版本號

DIR="/root/nginx_source"
export LC_ALL=en_US.UTF-8
export LANG=zh_CN.UTF-8
CURDATE=`/bin/date`
#用來記錄日誌
echo "Code Deployed by at $CURDATE,$REPOS,$REV" >> /root/nginx_change.log
#當版本庫發生改變時,把更新寫入到副本當中
/usr/bin/svn update $DIR --username keepalived --password keepalived --no-auth-cache 2>1 /root/nginx_update.log
#使用rsync同步項目到web服務器上
/usr/bin/rsync -r -e ssh --stats --progress $DIR [email protected]:/var/www/html


這裏rsync同步時需要輸入遠程主機的密碼,所以我們可以通過密鑰來進行認證,

[root@node2 ~]#ssh-keygen -t rsa -P ''

[root@node2 ~]#ssh-copy-id -i .ssh/id_rsa.pub [email protected]


在windows上面操作時,需要安裝一個軟件TortoiseSVN,這裏提供一個下載路徑是英文版的http://pan.baidu.com/s/1ntoU1jN  ,下載後安裝,安裝好後,在桌面上右擊時選擇(SVN檢出或者SVN Checkout)

wKioL1VVXovBl-c5AAFoaEB76jM196.jpg

wKiom1VVXRHTvALvAAC38yoaBu8351.jpg

wKioL1VVXovBQuxRAACCgMZ19GA326.jpg

wKioL1VVXozDOZnTAAHepHgLs8g211.jpg

wKioL1VVXraiuVk6AAGubACLj6M835.jpg

接下來就可以打開文件進行操作了,我們修改cc目錄下的gcc文件,在第5行添加了(hello    Eventyone)和第33行添加了(Hello world!),在cc目錄下的owc文件中第10行到第15行的內容

# maximize speed
CFLAGS="$CFLAGS -ot"
# reorder instructions for best pipeline usage
CFLAGS="$CFLAGS -op"
# inline intrinsic functions
CFLAGS="$CFLAGS -oi"     都刪除了

而後在project文件夾上右擊,選擇SVN Commit。。。進行commit操作

wKiom1VVYQPhuBU4AAJZ1D-OHqw683.jpg

wKiom1VVYQPCOPkqAAHAS4a97x8933.jpg

wKioL1VVYn6yQQ-ZAAGO8P4lnk8924.jpg

[root@node2 ~]# svn cat file:///svn/project/cc/gcc | head -5   發現已經更新了

# Copyright (C) Igor Sysoev
# Copyright (C) Nginx, Inc.

hello    Eventyone
[root@node2 ~]# svn cat file:///svn/project/cc/gcc | head -35 | tail -5    兩個文件都更新成功
. auto/feature

Hello world£?
CC_TEST_FLAGS=


[root@node5 ~]# ls /var/www/html/   這裏遠程也已經把文件同步過來了
nginx_source
[root@node5 ~]# ls /var/www/html/nginx_source/
cc          feature       headers  install  modules  os       summary
define      have          include  lib      nohave   sources  types
endianness  have_headers  init     make     options  stubs    unix


下面通過新增幾個文件,查看一下效果,在project目錄下新建了兩個.txt的文件zabbixtxt、openstack.txt,並在openstack.txt文件添加了如下內容:

Migrating to the cloud






Hello,Openstack


ok,現在進行commit操作

wKiom1VVZEHRbMhFAAI1E8HU2PM427.jpg

wKioL1VVZbvyVAKBAANPgF0vHGU084.jpg

wKiom1VVZEGg8-tXAAgLzc1Y6PY219.jpg

wKioL1VVZdSzi0owAAI7lDa76q4482.jpg

wKiom1VVZFqApZ4AAAG3vvzhuAM611.jpg

wKioL1VVZdTgBt9pAAHZFp5EG3M789.jpg

[root@node2 ~]# svn list file:///svn/project
cc/
define
endianness
feature
have
have_headers
headers
include
init
install
lib/
make
modules
nohave
openstatck.txt.txt
options
os/
sources
stubs
summary
types/
unix
zabbix.txt.txt
[root@node2 ~]# svn cat file:///svn/project/openstatck.txt.txt
Migrating to the cloud






Hello£?Openstack[root@node2 ~]# 


[root@node5 ~]# ls /var/www/html/nginx_source/
cc          feature       headers  install  modules             options  stubs    unix
define      have          include  lib      nohave              os       summary  zabbix.txt.txt
endianness  have_headers  init     make     openstatck.txt.txt  sources  types
[root@node5 ~]# cat /var/www/html/nginx_source/openstatck.txt.txt 
Migrating to the cloud






Hello£?Openstack[root@node5 ~]#    發現一切都ok,就介紹到此

有興趣可以自己好好研究研究,有什麼錯誤系統大家可以指正,謝謝閱讀

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章