Linux数据备份通用Shell脚本

该脚本实现了在Linux中备份并删除过期的备份文件,当备份次数超出限制时才删除超出日期限制的备份文件,不用担心长时间不备份会导致备份目录被清空。此外,备份前会检查磁盘空间是否足够,备份后会检查备份文件是否需要压缩。

#!/bin/bash
# 数据备份通用脚本
# 当备份次数超出限制时才删除超出日期限制的备份文件
# 执行方法 : sh backup.sh -s 'str' -b 'str' -t 'str' -e 'num' -d 'num'
# -s srcFile  待备份的文件或目录(必填),绝对路径或者脚本相对路径
# -b bakPath  备份目标地址(必填),末尾不带'/',绝对路径或者脚本相对路径
# -t bakType  备份目标类型,dir/zip/tgz/all(默认)
# -e expTimes 保留备份文件的限制次数(默认15次)
# -d expDates 保留备份文件的限制天数(默认30天)

backupLog="${PWD}/backup.log"
getTime (){
  nowTime=`date +'%Y%m%d%H%M%S'`
}

while getopts 's:b:t:e:d:' OPT
do
  case ${OPT} in
    s) srcFile="$OPTARG" ;;
    b) bakPath="$OPTARG" ;;
    t) bakType="$OPTARG" ;;
    e) expTimes="$OPTARG";;
    d) expDates="$OPTARG";;
  esac
done

echo ">>>>>>>>>>>>>>>>>>>>>>>>> 开始备份数据 <<<<<<<<<<<<<<<<<<<<<<<<<" >> ${backupLog}

# 检查待备份的文件或目录
getTime
if [ -z "${srcFile}" ] || [ -z "`ls -l ${srcFile}`" ]
then
  echo -e "${nowTime}\t没有输入待备份的文件或路径,或${srcFile}不存在\n" >> ${backupLog}
  exit 1
fi
# 检查备份目标地址
[ -z "${bakPath}" ] && echo -e "${nowTime}\t没有输入备份目标路径\n" >> ${backupLog} && exit 1
[ ! -d "${bakPath}" ] && mkdir -p ${bakPath} && echo -e "${nowTime}\t已创建备份目标文件夹" >> ${backupLog}
# 如果不输入文件类型-t参数则赋默认值all
[ -z "${bakType}" ] && bakType="all"
# 如果不输入保留备份次数-e参数则赋默认值15
[ -z "${expTimes}" ] && expTimes=15
[ -z "${expDates}" ] && expDates=30

# 检查备份目录中的备份次数
case "${bakType}" in
  dir)
    bakTimes=`ls -l -d ${bakPath} | wc -l`
    varTypes="-type d"
    ;;
  zip)
    typeName="zip"
    bakTimes=`ls -l ${bakPath} | grep *.${typeName} | wc -l`
    varTypes="-name '*.${typeName}'"
    ;;
  tgz)
    typeName="tar.gz"
    bakTimes=`ls -l ${bakPath} | grep *.${typeName} | wc -l`
    varTypes="-name '*.${typeName}'"
    ;;
  *)
    bakTimes=`ls -l ${bakPath} | grep -Ev "total|总用量" | wc -l`
    varTypes=""
    ;;
esac

# 删除过期的备份文件
getTime
if [ ${bakTimes} -gt ${expTimes} ]
then
  find ${bakPath} ${varTypes} -mtime +${expDates} -exec rm -rf {} \;
  echo -e "${nowTime}\t已删除过期的备份文件" >> ${backupLog}
fi

# 判断磁盘空间
getTime
discAvail=`df -h ${srcFile} | awk '{print $4}' | tail -n 1`
discNeeds=`du -sh ${srcFile} | awk '{print $1}'`
getResult (){
  availNum="echo ${discAvail} | sed 's/.$//'"
  needsNum="echo ${discNeeds} | sed 's/.$//'"
  result=`expr ${availNum} - ${needsNum}`
  [ ${result} -lt 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

# 按当前时间创建备份文件夹
getTime
bakPath="${bakPath}/${nowTime}"
mkdir -p ${bakPath}

# 备份数据
getTime
echo -e "${nowTime}\t开始复制${srcFile}至${bakPath}/" >> ${backupLog}
cp -rf ${srcFile} ${bakPath}/

# 检查备份文件是否需要压缩
cd ${bakPath}
getTime
case ${bakType} in
  zip)
    if [ "${srcFile##*.}" != "${typeName}" ]
    then
      [ -z "`rpm -qa zip`" ] && echo -e "${nowTime}\t当前系统未安装zip工具,无法压缩\n" >> ${backupLog} && exit 1
      zip -m -q -r ${srcFile##/}.${typeName} *
      getTime
      echo -e "${nowTime}\t已将备份文件压缩为zip格式" >> ${backupLog}
    fi
    ;;
  tgz)
    if [ "${srcFile##*.}" != "${typeName}" ]
    then
      tar -zcf ${srcFile##/}.${typeName} * --remove-files
      getTime
      echo -e "${nowTime}\t已将备份文件压缩为tar.gz格式" >> ${backupLog}
    fi
    ;;
esac

echo -e ">>>>>>>>>>>>>>>>>>>>>>>>> 完成数据备份 <<<<<<<<<<<<<<<<<<<<<<<<<\n" >> ${backupLog}

原创文章禁止转载:技术学堂 » Linux数据备份通用Shell脚本

赞 (0) 打赏

精彩评论

3+4=

感谢您的支持与鼓励

支付宝扫一扫打赏

微信扫一扫打赏