脚本实现将Subversion(SVN)仓库中指定的项目自动迁移至另一个仓库,同时以此为基线创建新分支,具备完善的判断机制和日志记录功能,可放心使用。
#!/bin/bash # 迁移代码库自动化脚本 # 迁移记录文件:copyReposList.txt # 迁移记录格式: # 因脚本中已有svn或git仓库前缀,故在迁移记录文件中无需重复填写 # 已完成迁移的记录会自动在行首用“#”标记,再次同步需删除“#”符号 # srcRepoBra(源始) desRepoBra(目标) # 101010_ABC/TRUNK/202210011001 202020_DEF/UAT/20230202002 # 脚本执行示例: # ./copyRepos.sh # 工作目录 mainOpsDir="${PWD}/COPY" srcRepoDir="${mainOpsDir}/srcRepo" desRepoDir="${mainOpsDir}/desRepo" # 源始仓库 srcRepoURL="http://10.10.100.1/svn/" srcRepoUsr="demo1" srcRepoPwd="Password1" # 目标仓库 desRepoURL="svn://10.10.200.2/" desRepoUsr="demo2" desRepoPwd="Password2" # 迁移列表 commitList="copyReposList.txt" # 迁移日志 commitLogs="${mainOpsDir}/copyReposLogs.log" # 日志等级 commitLogM=("【信息】" "【警告】" "【失败】" "【成功】" "设置代码仓库基线版本") # 获取时间函数 GET_TIME () { nowTime=`date +"%Y-%m-%d %H:%M:%S"` } GET_STAT () { [ $? -ne 0 ] && GET_TIME && echo -e "${nownowTime}${commitLogM[2]}上一次命令执行失败\n" >> ${commitLogs} && continue } # 检查基本环境 [ -z "checkCommand=`svn --version | grep -E "version|版本"`" ] && echo "${commitLogM[2]}本机没有安装subversion,终止代码迁移" && exit 1 [ ! -e "${commitList}" ] && echo "${commitLogM[2]}没有找到迁移列表文件,终止代码迁移" && exit 1 [ ! -d "${srcRepoDir}" ] && mkdir -p ${srcRepoDir} && echo "${commitLogM[0]}已创建源始工作目录" [ ! -d "${desRepoDir}" ] && mkdir -p ${desRepoDir} && echo "${commitLogM[0]}已创建目标工作目录" # 开始迁移仓库 cat ${commitList} | grep -v "^#" | while read srcRepoBra desRepoBra do [ -z "${srcRepoBra}" ] && continue echo -e "----- ${srcRepoURL}${srcRepoName} -> ${desRepoURL}${desRepoName} -----" >> ${commitLogs} lineNumber=`grep -n "${srcRepoBra}" ${commitList} | grep -v "^#" | head -n 1 | awk -F: '{print $1}'` GET_TIME # 检查列表数据 [ -z "${desRepoBra}" ] && echo -e "${nowTime}\t${commitLogM[2]}迁移信息表第${lineNumber}行项目信息不全\n" >> ${commitLogs} && continue # 检查历史记录 commitHistory=`grep "^#" ${commitList} | grep -Ew "${srcRepoBra}" | grep -Ew "${desRepoBra}"` [ -n "${commitHistory}" ] && echo -e "${nowTime}\t${commitLogM[1]}该项目之前已成功同步代码,重新同步请取消第${lineNumber}行首的#号\n" >> ${commitLogs} && continue # 删除本地仓库目录(源始) cd ${srcRepoDir} [ -d "${srcRepoBra}" ] && rm -rf ${srcRepoBra} && echo -e "${nowTime}\t${commitLogM[0]}已经删除之前下载的源始代码" >> ${commitLogs} GET_TIME # 导出源始仓库代码 echo -e "${nowTime}\t${commitLogM[0]}导出源始仓库代码${srcRepoURL}${srcRepoBra}" >> ${commitLogs} svn export ${srcRepoURL}${srcRepoBra} ${srcRepoBra} --username ${srcRepoUsr} --password ${srcRepoPwd} GET_STAT GET_TIME # 检查分支是否有效 [ -z "`ls ${srcRepoBra}`"] && echo -e "${nowTime}\t${commitLogM[1]}源代码库分支中没有需要迁移的文件\n" >> ${commitLogs} && continue sleep 1 # 删除本地仓库目录(目标) cd ${desRepoDir} [ -d "${desRepoBra}" ] && GET_TIME && rm -rf ${desRepoBra} && echo -e "${nowTime}\t${commitLogM[0]}已经删除之前下载的目标代码" >> ${commitLogs} GET_TIME # 创建目标代码目录 svn mkdir --parents ${desRepoURL}${desRepoBra} --username ${desRepoUsr} --password ${desRepoPwd} [ $? -ne 0 ] && str="${commitLogM[0]}创建代码目录" || str="${commitLogM[1]}无法创建目录" && echo -e "${nowTime}\t${str}${desRepoURL}${desRepoBra}" >> ${commitLogs} GET_TIME # 检出目标仓库代码 echo -e "${nowTime}\t${commitLogM[0]}检出目标仓库代码${desRepoURL}${desRepoBra}" >> ${commitLogs} svn co ${desRepoURL}${desRepoBra} ${desRepoBra} --username ${srcRepoUsr} --password ${srcRepoPwd} GET_STAT sleep 1 GET_TIME # 检查目标仓库是否空 if [ -n "`ls ${desRepoBra}`" ] then while true do read -p "目标仓库已有文件,输入'Y'更新目标仓库,输入'N'跳过本条记录..." toNEXT [ "${toNEXT^^}" = "Y" ] || [ "${toNEXT}" = "N" ] && break done [ "${toNEXT}" = "N" ] && continue fi cd ${desRepoBra} # 复制代码至目标分支 echo -e "${nowTime}\t${commitLogM[0]}复制${srcRepoBra}中的代码至${desRepoBra}" >> ${commitLogs} cp -rf ${srcRepoDir}/${srcRepoBra}/* ./ # 向代码库中添加代码 GET_TIME echo -e "${nowTime}\t${commitLogM[0]}开始向代码库添加代码" >> ${commitLogs} svn add . --force --no-ignore --username ${desRepoUsr} --password ${desRepoPwd} # 向代码库中提交代码 GET_TIME echo -e "${nowTime}\t${commitLogM[0]}开始向代码库提交代码" >> ${commitLogs} svn commit -m "${commitLogM[4]}" . --username ${desRepoUsr} --password ${desRepoPwd} GET_STAT GET_TIME echo -e "${nowTime}\t${commitLogM[3]}代码已迁移至${desRepoURL}${desRepoBra}\n" >> ${commitLogs} # 标记迁移成功的项目 eval sed -i '"${lineNumber}s/^/#&/"' ${commitList} sleep 3 done
原创文章禁止转载:技术学堂 » Subversion迁移仓库自动化脚本