Bash rsync 脚本无法完成大型备份

Bash rsync 脚本无法完成大型备份

在过去一年左右的时间里,我一直在尝试学习在 Ubuntu 上编写 bash 脚本(一个 20.04 LTS 机器和一个 21.04 机器)。我觉得我的技能进步很大,但我对自己编写的一个特定备份脚本束手无策。

长话短说,如果备份脚本只是尝试备份我的 /home 目录,它会从头到尾顺利运行,但如果它尝试备份我的 / 目录,它会一直运行直到 rsync 过程完成(包括创建日志文件),然后脚本似乎就停止了,尽管我有一些后续的管理代码来提醒我。我甚至添加了一个退出陷阱,但它没有被触发。

我不知道我缺少什么,但如果有人能帮助我,我渴望尝试学习。

我不知道这是否有帮助,但这是我的脚本的一个稍微简化的版本:

#!/bin/bash
#
# backup
# unified back up, home and/or root folders to raid on Betty
# runs daily from sudo crontab (time varies by device); day of month determines which backup is performed
#
# related files
# generates backup.log in home folder of main user, archives existing backup.log to logs folder as yyyy-mm-dd-backup.log
# requires two files in $homefold: home-backup.exclude and boot-backup.exclude, to provide backup exclusions for rsync
#
# exit trap
function badquit {
   if [[ "$1" = "test" ]] ; then
      exit 0
   elif [[ "$goodflag" = "0" ]] ; then
      echo "$(date +%r) : $shname exited with goodflag=0" > $logfold/$shname.quit
   fi
}
trap badquit EXIT
#
# variables
host="$(hostname)"
hostlc="${host,,}"
shname="$(basename $0)"
hostuser="$(getent passwd "1000" | cut -d: -f1)"
homefold="/home/$hostuser"
logfold="$homefold/logs"
log="$homefold/$shname.log"
date="$(date +%Y-%m-%d)"
dom="$(date +%d)" # day of month at runtime determines which backup is performed (daily home, weekly home, quarterly boot)
soonwarn="7" # days in advance to create .soon report
case $host in # $bumount location varies by device: Betty mounts it differently than all others
   Betty)
      bumount="/mnt/raid"
      ;;
   *)
      bumount="/media/betty-raid"
      ;;
esac
bufold="$bumount/$hostlc"
goodflag="0" # default status of goodflag to trigger unexpected exit message
#
# pre-check
# confirm existence of $bufold, attempt to mount if not found, set fail message if unsuccessful
if [[ ! -e $bufold ]] ; then
   mount 192.168.x.x:/mnt/raid $bumount
   sleep 5
   if [[ ! -e $bufold ]] ; then
      echo "$(date +%r) : $shname exited because $bufold could not be mounted" > $logfold/$shname.quit
      goodflag="1"
      exit 1
   fi
fi
# 
# log management
if [[ -e $log ]] ; then
   mv $log $logfold/"$(date -r $log +"%Y-%m-%d")"-$shname.log
fi
#
# primary tasks
# set targdom per old boot backup schedule (to allow time for .soon)
case $host in
   Betty)
      targdom="8"
      ;;
   Veronica)
      targdom="12"
      ;;
   *)
      echo "$(date +%r) : $shname cannot identify $host" > $logfold/$shname.quit
      goodflag="1"
      exit 1
      ;;
esac   
# determine which backup to run, set final variables
if [[ "$dom" = "$targdom" ]] ; then # targdom triggers non-daily backups
   if [[ $(date +%m) -eq "01" ]] || [[ $(date +%m) -eq "04" ]] || [[ $(date +%m) -eq "07" ]] || [[ $(date +%m) -eq "10" ]] ; then # quarterly boot backup Jan,Apr,Jul,Oct
      ropts="-avuHkbi --delete --exclude-from=$homefold/boot-backup.exclude --backup-dir=$bufold/Backup.$date --log-file=$log" 
      rsource="/"
   else # monthly home backup
      ropts="-axvuHkbi --delete --exclude-from=$homefold/home-backup.exclude --backup-dir=$bufold/Backup.$date --log-file=$log"
      rsource="/home"
   fi
else # not targdom, daily home backup
   ropts="-axvuHkbi --exclude-from=$homefold/home-backup.exclude --backup-dir=$bufold/Backup.$date --log-file=$log" #daily home backup
   rsource="/home"
fi
# if this is a test, set variables appropriately
if [[ "$1" = "test" ]] ; then
   ropts="-navuHkbi --exclude-from=$homefold/boot-backup.exclude --backup-dir=$bufold/Backup.$date --log-file=$log" # dry-run ON
   rsource="/"
fi
# run the backup, finally
rsync $ropts --backup-dir=$bufold/Backup.$date --log-file=$log $rsource $bufold
#
# set exitcond
if [[ "$?" = "0" ]] ; then
# successful, log saved
   exitcond="successfully, log saved"
else
# unsuccessful, exit cond
   exitcond="unsuccessfully, exit code $?"
fi
#
echo "$(date +%r) : $shname completed $exitcond" >> $logfold/$shname.done
goodflag="1"

相关内容