svn+rsync+inotify realizes automatic code synchronization under inux

1. Introduction to the environment

Operating system: CentOS release 5.7 (Final)

Apache version: Apache/2.2.21 (Unix)

Subversion version: svn, version 1.7.2

Rsync版本:rsync  version 3.0.6  protocol version 30

Inotify版本:inotifywait 3.14

SVN Server server: hereinafter collectively referred to as A

Synchronous test server: hereinafter collectively referred to as B

 

2. Introduction to requirements and processes

Due to the collaborative development of programmers, the development habits of each other are different, and people often delete and modify other people's code by mistake, which has a great impact on the stability of the online production environment. Therefore, SVN is specially invited to be responsible for code version control to solve the above problems.

However, simple code control will increase a lot of unnecessary labor costs, such as: manual submission, checkout, release, testing, etc. Therefore, as an operation and maintenance personnel, it is natural to think of a way to make them perform all the above operations automatically. The following requirements are inherent.

1. After the code is committed to the A server, it is automatically updated to the working copy of the code

2. After the update, it needs to be directly synchronized to the B server for testing.

3. Regularly back up the svn server code base file to the backup server (share one with B here)

4. After the test is completed, the working copy file is automatically released to the production environment through program control (to be developed)

The first three steps above are all completed by system-level operations. The fourth step requires the program to write an interactive system to complete the process of testing and publishing. This document mainly implements the first 3 steps.

 

3. Prepare

For SVN related downloads, see:

http://www.ttlsa.com/html/723.html

 

rsync download and install

[codesyntax lang="php"]

rpm -qa | grep rsync

(If there is no output, execute the following command to install, if there is, skip it)

yum install -y rsync

[/ codesyntax]

inotify download

[codesyntax lang="php"]

wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

[/ codesyntax]

4. Install and configure SVN

See: http://www.ttlsa.com/html/723.html

Note: Since the Svn client needs to be installed on both the A server and the B server

So, for the sake of simplicity, it is recommended to fully install SVN on both servers

 

5. Install and configure the rsync+inotify server

The main reference for the installation process: http://kerry.blog.51cto.com/172631/734087

Here, according to your own situation, slightly modified, the specific content is as follows

5.1, B server (rsync daemon)

Since the repository code is synchronized to the B server, the task of installing the rsync server falls on the B server

[codesyntax lang="php"]

[/ codesyntax]

[codesyntax lang="php"]

# vi /etc/rsyncd/test_server.pwd

test:test

# chmod 600 /etc/rsyncd.conf /etc/rsyncd/test_server.pwd

# echo "Welcome to use the rsync services\!" >> /var/rsyncd.motd

# /usr/bin/rsync --daemon

[/ codesyntax]

ok, so the rsync server side is done, the next step is the client side script

5.2, A server version library

Since the A server is the source server, inotify needs to be installed on the source server

 

[codesyntax lang="php"]

# cd /usr/local/src/tarbag

# tar xvf inotify-tools-3.14.tar.gz -C ../software

# cd ../software/inotify-tools-3.14

# ./configure && make && make install

[/ codesyntax]

 

6. Implement automatic update through svn's own hooks (the content of this section is prepared for svnsync, the content of the file can be left blank, if you do not use the svnsync function, you can skip this section)

6.1, A server, modify the post-commit file

[codesyntax lang="php"]

# cd  /data/svn/test/hooks/

# cp  post-commit.tmpl  post-commit

# vi post-commit

Clear all information and replace with the following information

#!/bin/sh

export LANG=en_US.UTF-8
REPOS="$1"
REV="$2"

AUTHOR=/usr/local/bin/svnlook author -r $REV $REPOS
DATE=date '+%F'
TIME=date '+%F %T'
DIR=/data/svn/test/hooks
PATH=/usr/local/bin
LOCAL_WORKING_COPY=/data/svn_data/test/
SVN_SRC=/data/svn_data/test/
WEB_SRC=/data/www/test/
PASSWD=/etc/rsyncd/test.pwd
DES=test
USER=test
SVN=$PATH/svn
SVNSYNC=$PATH/svnsync
RSYNC=/usr/bin/rsync
RSYNC_LOGFILE=/var/log/rsyncd/${DATE}-rsync.log
SVN_LOGFILE=/var/log/svn/${DATE}-svn.log
SVN_LOGIN_INFO="--username tonyty163 --password ty1224"

#SVN UPDATE
echo "----------------------------------BEGIN------------------------------------" >> ${SVN_LOGFILE}
echo "The following changes were made to the code:" >> ${SVN_LOGFILE}
echo "[$TIME]--[$AUTHOR] svn commit file:" >> ${SVN_LOGFILE}
echo "REV is $REV,REPOS is $REPOS" >> ${SVN_LOGFILE}
$SVN update $SVN_LOGIN_INFO $LOCAL_WORKING_COPY >> ${SVN_LOGFILE}
if [ "$?" = "0" ];
then
echo "update successed" >> ${SVN_LOGFILE};
else
echo "update failed" >> ${SVN_LOGFILE};
fi
$SVN log $LOCAL_WORKING_COPY -v -r "$REV" $SVN_LOGIN_INFO >>${SVN_LOGFILE}
$SVN diff $LOCAL_WORKING_COPY -c "$REV" --no-diff-deleted $SVN_LOGIN_INFO >> ${SVN_LOGFILE}
echo "------------------------------------END------------------------------------" >> ${SVN_LOGFILE}

#Rsync TO WEB_SRC
${RSYNC} -aH --delete --progress --exclude=".svn/" ${SVN_SRC} ${WEB_SRC} &&  echo "local rsync succesed" >> ${RSYNC_LOGFILE} 2>&1
echo "------------------------------------END------------------------------------" >> ${RSYNC_LOGFILE} 2>&1;
[/codesyntax]

Briefly describe the function of the above script

First of all, the main function of the post-commit script is to wait for someone in the repository to commit the actions that will be performed later.

The script first generates a log with the date as the file name in the $DIR/logs directory

Then record the time, author, and svn updata and svn log, svn diff information respectively

Note: The log is for the convenience of query and troubleshooting.

 

 

6.2. Server B, modify start-commit and pre-revprop-change

[codesyntax lang="php"]

# cd /data/svn/test/hooks/

# vi start-commit

 

#!/bin/sh

 

USER="$2"

 

if [ "$USER" = "user" ]; then exit 0; fi

 

echo "Only the user user may change revision properties as this is a read-only, mirror repository." >&2

# All checks passed, so allow the commit.

exit 1

[/ codesyntax]

Modify the content of the file to the above content

[codesyntax lang="php"]

# vi pre-revprop-change

 

#!/bin/sh

 

REPOS="$1"

REV="$2"

USER="$3"

PROPNAME="$4"

ACTION="$5"

 

if [ "$ACTION" = "M" -a "$PROPNAME" = "svn:log" ]; then exit 0; fi

 

echo "Changing revision properties other than svn:log is prohibited" >&2

exit 0

 

if [ "$USER" = "user" ]; then exit; fi

 

echo "Only the user user may change revision properties as this is a read-only, mirror repository." >&2

exit 1

[/ codesyntax]

Modify the content of the file to the above content

 

 

7. Synchronize server A and server B through rsync and inotify

After each component is installed, you need to write an rsync background monitoring script yourself, the content is as follows

[codesyntax lang="php"]

# vi /usr/local/src/scripts/rsync.sh

#!/bin/bash
HOST=107.6.15.235
SVN_SRC=/data/svn_data/test/
WEB_SRC=/data/www/test/
PASSWD=/etc/rsyncd/test.pwd
DES=test
USER=test
DATE=date '+%F'
RSYNC_LOGFILE=/var/log/rsyncd/${DATE}-rsync.log

/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format  '%T %w%f' -e modify,delete,create,attrib ${WEB_SRC} | while read  file
do
rsync -aH --delete --progress ${WEB_SRC} ${USER}@${HOST}::${DES} --password-file=${PASSWD} &&  echo "${file} rsync succesed" >> ${RSYNC_LOGFILE} 2>&1
if [ $? -ne 0 ];
then
echo "remote rsync failed" >> ${RSYNC_LOGFILE};
fi
echo "-----------------------------------END-------------------------------------" >> ${RSYNC_LOGFILE} 2>&1;
done

[/ codesyntax]

Briefly explain the function of the script, mainly two

a. Changes in the ${src} directory are monitored by inotifywait

b. Store the changed file in ${file}, pass it to rsync for synchronization (ie incremental synchronization), and write the updated file to the log

 

8. Regularly back up SVN through crontab

[codesyntax lang="php"]

# crontab -e

* * * * * /usr/local/src/scripts/svnsync.sh

 

# vi /usr/local/src/scripts/svnsync.sh

#!/bin/sh

DATE=date '+%F'

TIME=date '+%F %T'

DIR=/data/svn/test/hooks

PATH=/usr/local/bin

SVN=$PATH/svn

SVNSYNC=$PATH/svnsync

MIRROR_SVN=http://192.168.1.204/svn/test/

SVN_LOGIN_INFO="--username user --password passwd"

$SVNSYNC sync $MIRROR_SVN $SVN_LOGIN_INFO >> $DIR/logs/$DATE-log.txt

if [ "$?" = "0" ];

then

echo "svnsync successed" >> $DIR/logs/$DATE-log.txt;

else

echo "svnsync failed" >> $DIR/logs/$DATE-log.txt;

be

[/ codesyntax]

Introduction to the main functions of the above script:

First crontab executes the svnsync.sh script every minute

The script is to synchronize the local repository remotely (note: non-working copy, that is, the db file of svn)

After the synchronization is completed, the script execution result is recorded in the log

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325764728&siteId=291194637