Subversion仓库Dump自动化备份脚本

Subversion代码库自动化备份脚本,通过dump命令进生全量或增量备份,只需要把待备份的仓库名称记录在指定的文本中即可,可设置定时任务,也可手动执行。

#!/bin/bash
# 最后更新:2023年7月22日 15:45
# 脚本功能:Subversion仓库备份脚本,即可手动也可定时
#           周一至周六进行增量备份、周日进行全量备份
#           删除过期的备份文件:全量留3天/增量留15天
# 特殊说明:必须挂载网络磁盘作为仓库备份盘
# 定时任务:0 0 * * * /home/zhilin/scripts/svnadminDump.sh
# 仓库明细:仓库名称  最后一次备份的版本号
# 参数说明:
# -t 单独执行脚本时可指定备份模式
#    ful:全量备份
#    inc:增量备份

# 源仓库
repPath="/svn/repositories"
# 备份库
bakPath="/bak/repositories"
# 备份盘
bakDisk="10.10.100.100/svnBackup"
# 库账号
svnUser="admin"
svnPass="admin@123"
# 库明细
repList="${PWD}/svnRepositoriesList.txt"
# 库日志
bakLogs="${PWD}/svnRepositoriesDump.log"

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

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

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

# 检查仓库明细
[ ! -e ${repList} ] && echo -e "${nowTime}\t仓库明细文件不存在" >> ${bakLogs}

# 统一列表格式
sed -i '/^$/d' ${repList}
sed -i 's/^\ \+//g' ${repList}
sed -i 's/\ \+$//g' ${repList}
sed -i 's/[\t ]\+/\t/g' ${repList}

# 检查备份磁盘
[ -z "`df -Th | grep ${bakDisk}`" ] && echo -e "${nowTime}\t仓库备份磁盘未挂载" >> ${bakLogs} && exit 1
discAvail=`df -h ${bakPath} | awk '{print $4}' | tail -n 1`
discNeeds=`du -sh ${repPath} | awk '{print $1}'`
getResult (){
  availNum="`echo ${discAvail} | sed 's/.$//'`"
  needsNum="`echo ${discNeeds} | sed 's/.$//'`"
  result=`echo "scale=3; ${availNum} - ${needsNum}" | bc`
  [[ ${result} < 0 ]] && echo -e "${nowTime}\t磁盘空间不足导致无法备份\n" >> ${backupLog} && exit 1
}
case ${discAvail: -1} in
  G) [ "${discNeeds: -1}" = "G" ] && getResult;;
  M) [ "${discNeeds: -1}" = "G" ] && echo -e "${nowTime}\t磁盘空间不足导致无法备份\n" >> ${backupLog} && exit 1
     [ "${discNeeds: -1}" = "M" ] && getResult;;
  K) echo -e "${nowTime}\t磁盘空间不足导致无法备份\n" >> ${backupLog} && exit 1;;
esac

# 重置备份类型
[ "${bakType^^}" = "FUL" ] && echo "备份类型参数为FUL(全量)或INC(增量)" && exit 1
[ -z "${bakType}" ] && [ "`date | awk '{print $1}'`" != "Sun" ] && bakType="inc"

# 开始备份仓库
cat ${repList} | while read repName revLast
do
  getTime
  # 获取当前版本号
  [ ! -d ${repPath}/${repName} ] && echo -e "${nowTime}\t项目${repName}无效或未创建仓库" >> ${bakLogs} && continue || revRect=`svnlook youngest ${repPath}/${repName}`
  [ $? -ne 0 ] && echo -e "${nowTime}\t项目${repName}获取当前版本号异常" >> ${bakLogs} && continue
  # 获取备份版本号
  [ ! -d ${bakPath}/${repName} ] && mkdir -p ${bakPath}/${repName} && revLast=""
  [ -z "${revLast}" ] && revLast=1 && eval sed -i '"/^${repName}/s/$/&\t${revLast}/g"' ${repList}
  [ $? -ne 0 ] && echo -e "${nowTime}\t项目${repName}版本号初始化异常" >> ${bakLogs} && continue
  # 判断是否要备份
  [ ${revRect} -le ${revLast} ] && echo -e "${nowTime}\t项目${repName}备份仓库无需更新" >> ${bakLogs} && continue
  # 开始备份代码库
  getTime
  echo -e "${nowTime}\t项目${repName}开始` [ ${bakType^^} = "FUL" ] && echo 全量 || echo 增量`备份" >> ${bakLogs}
  svnadmin dump ` [ ${bakType^^} = "INC" ] && echo "--incremental --revision ${revLast}:${revRect}"` ${repPath}/${repName} > ${bakPath}/${repName}/${repName}_${bakType}_${nowTime}
  getTime
  echo -e "${nowTime}\t项目${repName}完成` [ ${bakType^^} = "FUL" ] && echo 全量 || echo 增量`备份" >> ${bakLogs}
  # 更新备份版本号
  getTime
  echo -e "${nowTime}\t更新${repName}仓库的备份版本号" >> ${bakLogs}
  eval sed -i '"/^${repName}/s/${revLast}$/${revRect}$/g"' ${repList}
  # 删除过期的备份
  getTime
  echo -e "${nowTime}\t删除${repName}项目中的过期备份" >> ${bakLogs}
  flag=1
  [ "${bakType^^}" = "FUL" ] && nums=3 || nums=15
  for fileName in `ls -lt ${bakPath}/${repName} | grep ${bakType} | awk '{print $9}'`
  do
    [ ${flag} -gt ${nums} ] && rm -f ${bakPath}/${repName}/${fileName} && echo -e "\t\t\t删除${bakPath}/${repName}/${fileName}" >> ${bakLogs}
    flag=`expr ${flag} + 1`
  done
  [ ${flag} -lt ${nums} ] && echo -e "${nowTime}\t项目${repName}无过期的备份文件" >> ${bakLogs}
done

getTime
echo ">>>>>>>>>>>>>>> ${nowTime} <<<<<<<<<<<<<<<" >> ${bakLogs}

原创文章禁止转载:技术学堂 » Subversion仓库Dump自动化备份脚本

精彩评论

3+3=

感谢您的支持与鼓励

支付宝扫一扫打赏

微信扫一扫打赏