JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Linux server svn remote code synchronization

Author:JIYIK Last Updated:2025/04/08 Views:

In the article "Building SVN Service Project and Synchronizing Code to Project Directory" , we briefly introduced how to use SVN to synchronize submitted code to the working directory. But there is a problem here, that is, the SVN service and our working directory are on the same server. If our application is deployed in a distributed manner, then the SVN service must be deployed on each application server, and the same code needs to be submitted multiple times.

In this chapter, we will introduce two ways to remotely synchronize code

Use scp command in hooks for remote synchronization

SVN server: 192.168.5.201
Application server: 192.168.5.200

Here we need to use the scp remote file copy command. We also need to create a new /STest directory on the 5.201 host and check out a copy of the code in this directory.

# mkdir /STest
# cd /STest
# svn checkout svn://192.168.5.201 –username svnuser –password svnuser123

After each update is submitted, we first use the method of synchronizing the code on a server to update it to the /STest directory, then use the svnlook changed command to view the updated files (in fact, here we are looking for updated files in the /STest directory), and finally use the scp command to copy the updated files in the /STest directory to the corresponding directory on 5.200.

Create a new post-commit file under the hooks directory and add the following shell code

#!/bin/bash
export LANG=zh_CN.UTF-8
 
REPOS="$1"
REV="$2"
SVN=/usr/bin/svn
SVNLOOK=/usr/bin/svnlook
WEB=/App
LOG=/data/home/auto_svn.log
 
#Update the latest version to the local /STest
$SVN update $WEB --username svnuser --password svnuser123
 
#View the updated files
upfile=`$SVNLOOK changed -r $REV $REPOS`
 
#Copy the updated files to the target host in sequence
for file in $upfile
do
 
  if [ -d /STest/${file} ]; then
#Judge whether there is a new folder. If it is a folder, use ssh to log in to the target host first.
#Create a folder on the target host
        ssh root@192.168.18.239 "mkdir /www/App/${file}; exit"
  elif [ -f /STest/${file} ]; then
                   #If the file is updated, directly use the scp command to remotely copy the file to the corresponding target file
        scp /STest/${file} root@192.168.18.239:/www/App/${file}
 
  fi
 
done

This method requires storing a copy of the code on 5.201, which can be considered as the code temporarily borrowed by the scp copy file. Although it is a bit troublesome, there will be no .svn directory in the code on the target host, which can be said to improve the security of the application. Of course, the presence of .svn in the application code can be filtered out by the rewriting function of nginx or apache.

Use ssh to log in to the target host and update the file

This method is different from the above method in that there is no need to store a temporary code locally.

The first thing you need to do is to create a new application project /www/App on the target host 5.200, then enter the application directory and check out a copy of the code

#mkdir /www/App
#cd /www/App
#svn checkout svn://192.168.5.201 –username svnuser –password svnuser123

Then, every time an updated code is submitted, use ssh to log in to the target host and use svn update to update the application code.

You also need to create a new post-commit file in the hooks directory and write the shell code as follows

#!/bin/bash
export LANG=zh_CN.UTF-8
 
REPOS="$1"
REV="$2"
SVN=/usr/bin/svn
SVNLOOK=/usr/bin/svnlook
MASTERDIR=/www
TARGETDIR=/App
LOG=/data/home/auto_svn.log
#View the updated folder
updirs=`$SVNLOOK dirs-changed -r $REV $REPOS`
 
CHANGEDIR=$(echo "$updirs" | head -1) # The top directory in updirs
 
#Use ssh to log in to the target host and perform the update operation
ssh root@192.168.5.200 “export LANG=zh_CN.UTF-8; svn update --username svnuser --password svnuser123 ${MASTERDIR}${TARGETDIR}”

The disadvantage of the above method is that there will be a .svn directory in the code, which poses a security problem. However, this can be filtered out by the nginx rewrite function.

The above two methods have their own advantages and disadvantages. I personally prefer the first method. I hope the introduction in this chapter can help you.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Restart PostgreSQL in Ubuntu 18.04

Publish Date:2025/04/09 Views:72 Category:PostgreSQL

This short article shows how to restart PostgreSQL in Ubuntu. Restart PostgreSQL Server in Ubuntu You can restart Postgres server in Ubuntu using the following command. Order: sudo service postgres restart Sometimes the above command does n

Issues to note when installing Apache on Linux

Publish Date:2025/04/08 Views:78 Category:OPERATING SYSTEM

As the most commonly used web server, Apache can be used in most computer operating systems. As a free and open source Unix-like operating system, Linux and Apache are a golden pair. This article will introduce the installation and use of A

How to decompress x.tar.xz format files under Linux

Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM

A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr

Summary of vim common commands

Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM

In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme

Detailed explanation of command return value $? in Linux

Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM

? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re

Common judgment formulas for Linux script shell

Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM

In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial