Subversion仓库svnsync自动化热同步脚本

Subversion代码库自动化热同步脚本,通过svnsync命令实现,设置定时任务之后即可自动将主库中更新的内容同步至从库。

#!/bin/bash
# 最后更新:2023/08/02 15:03
# 脚本开发:
# 脚本功能:Subversion仓库实时同步脚本
#           自动发现主库的新项目并同步至从库
# 特殊说明:从库Subversion部署尽量与主库一致
#           从仓库服务器设定时任务或执行脚本
#           从仓库服务器免密访问主仓库服务器
#           主仓库目录必须给${mastUsr}赋权限
# 定时任务: 30 * * * * /home/username/scripts/svnsyncBackup.sh
# 参数说明:
# -a 指定特同步的项目(留空则同步明细表中的所有项目)

# 主仓库
mastSvn="10.10.100.100"
mastUsr="myname"
syncCmd="/usr/local/subversion/bin/svnsync"
# 从仓库
slavSvn="10.10.100.101"
# 库路径
repoDir="/svn/repositories"
# 库账号
syncUsr="svnsync"
syncPwd="svnsync@123456"
# 库模板
repTmpl="${PWD}/svnTemplates"
# 库明细
repList="${PWD}/svnSlaverList.txt"
tmpList="${PWD}/svnMasterList.txt"
# 排除库
excList="${PWD}/excludeList.txt"
# 库日志
syncLog="${PWD}/svnsyncLogs.txt"

while getopts 'a:' OPT
do
  case ${OPT} in
    a) repList="$OPTARG";;
  esac
done

# 获取当前时间
getTime (){
  nowTime=`date +'%Y%m%d%H%M%S'`
}

getTime
echo ">>>>>>>>>>>>>>> 备份仓库-${nowTime:0:8} <<<<<<<<<<<<<<<" >> ${bakLogs}

# 获取从库明细
[ ! -e ${repList} ] && ls -l ${repoDir} | grep ^d | awk '{print $9}' > ${repList}

# 获取主库明细
ssh ${mastUsr}@${mastSvn} "ls -l ${repoDir} | grep ^d" | awk '{print $9}' > ${tmpList}

# 开始同步仓库
cat ${tmpList} | while read repName
do
  [ -n "`grep -w ${repName} ${repList}`" ] && echo "项目${repName}已在实时同步列表中" && continue
  getTime
  echo -e "${nowTime}\t在主库中发现新项目${repName}" >> ${syncLog}
  cd ${repoDir}
  svnadmin create ${repName}
  sleep 1
  # 配置模板
  if [ ! -e ${repTmpl}/pre-revprop-change ]
  then
    cp ${repoDir}/${repName}/hooks/pre-revprop-change.tmpl ${repTmpl}/pre-revprop-change
    sed -i 's/^[A-Zie]/#&/g' ${repTmpl}/pre-revprop-change
    sed -i 's/^#exit/exit/g' ${repTmpl}/pre-revprop-change
  fi
  sleep 1
  cp -f ${repoDir}/${repName}/hooks/post-commit.tmpl ${repTmpl}/post-commit
  sed -i 's/^[A-Zm]/#&/g' ${repTmpl}/post-commit
  echo "${syncCmd} synchronize --non-interactive http://${slavSvn}/svn/${repName} --username ${syncUsr} --password ${syncPwd}" >> ${repTmpl}/post-commit
  sleep 1
  # 配置从库
  getTime
  echo -e "${nowTime}\t配置从库中的新项目${repName}" >> ${syncLog}
  cd ${repName}/hooks
  cp -f ${repTmpl}/pre-revprop-change ./
  sleep 1
  svnsync init file://${repoDir}/${repName} http://${mastSvn}/svn/${repName} --username ${syncUsr} --password ${syncPwd} --username ${syncUsr} --password ${syncPwd} --no-auth-cache
  svnsync sync file://${repoDir}/${repName}
  sleep 1
  [ "${mastUsr}" != "root" ] && setfacl -d -R -m u:${mastUsr}:rwx ${repoDir}/${repName}
  # 配置主库
  getTime
  echo -e "${nowTime}\t在主库配置同步勾子${repName}" >> ${syncLog}
  scp ${repTmpl}/post-commit ${mastUsr}@${mastSvn}:${repoDir}/${repName}/hooks/
  sleep 3
done

rm -f ${tmpList}

# 同步权限文件
getTime
echo -e "${nowTime}\t将主库中的权限文件同步到从库" >> ${syncLog}
scp -r ${mastUsr}@${mastSvn}:${repoDir}/conf/* ${repoDir}/conf/
sleep 1

getTime
echo -e ">>>>>>>>>>>>>>> ${nowTime} <<<<<<<<<<<<<<<" >> ${syncLog}

原创文章禁止转载:技术学堂 » Subversion仓库svnsync自动化热同步脚本

精彩评论

7+2=

感谢您的支持与鼓励

支付宝扫一扫打赏

微信扫一扫打赏