backup script log maintenance

This commit is contained in:
Eric Lay 2023-06-24 17:33:20 -05:00
parent b17fcb4249
commit 90fa5eaf19
2 changed files with 44 additions and 16 deletions

39
backup
View File

@ -2,7 +2,9 @@
# Setting this, so the repo does not need to be given on the commandline: # Setting this, so the repo does not need to be given on the commandline:
machine="$( hostnamectl --static )" machine="$( hostnamectl --static )"
export BORG_REPO="ez@reposerver:/backup/$machine" remoteHost="reposerver"
remoteUser=""
export BORG_REPO="$remoteUser@$remoteHost:/backup/$machine"
# See the section "Passphrase notes" for more infos. # See the section "Passphrase notes" for more infos.
export BORG_PASSPHRASE='' export BORG_PASSPHRASE=''
@ -10,27 +12,36 @@ export BORG_PASSPHRASE=''
# Non-interactively accept relocation of a repository # Non-interactively accept relocation of a repository
export BORG_RELOCATED_REPO_ACCESS_IS_OK=yes export BORG_RELOCATED_REPO_ACCESS_IS_OK=yes
# some helpers and error handling: # Some helpers and error handling:
logFile="/home/ez/.cache/logs/borg/$( date +%Y-%m-%d-%H:%M:%S )" logPath="/home/ez/.cache/logs/borg"
logFile="$logPath/$( date +%Y-%m-%d-%H:%M:%S.%N )"
infoLine() { printf "%s\t%s\n" "$( date +%H:%M:%S )" "$*" | tee -a "$logFile"; } infoLine() { printf "%s\t%s\n" "$( date +%H:%M:%S )" "$*" | tee -a "$logFile"; }
seeLog() { printf "\n%s %s" "$*" "$logFile"; } seeLog() { printf "\n%s %s" "$*" "$logFile"; }
trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM
# Notify user of running backup # Notify user of running backup
notify() { notify() {
if [ "$(command -v sudo)" -a "$(command -v notify-send)" ]; then if [ "$(command -v sudo)" ] && [ "$(command -v notify-send)" ]; then
dialog_kind=$(echo $1|tr '[:upper:]' '[:lower:]') dialog_kind=$(echo "$1"|tr '[:upper:]' '[:lower:]')
if [ $dialog_kind == 'info' ]; then if [ "$dialog_kind" = 'info' ]; then
dialog_kind='information' dialog_kind='information'
fi fi
for u in $(users); do for u in $(users); do
sudo -u "$u" DISPLAY=:0 \ sudo -u "$u" DISPLAY=:0 \
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(sudo -u $u id -u)/bus \ DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/"$(sudo -u "$u" id -u)/bus" \
notify-send -a 'luky-borg-backup' "$1" "$2" --icon=dialog-$dialog_kind notify-send -a 'luky-borg-backup' "$1" "$2" --icon="dialog-$dialog_kind"
done done
fi fi
} }
# Remove logs of pruned backups
tidyLogs() {
mapfile -t removeList < <(grep -w "^Pruning archive" "$logFile" | awk '{ print $4 }' )
for ((i=0; i<${#removeList[@]}; i++)); do
rm "$logPath/${removeList[i]}"
done
}
# Backup the most important directories into an archive named after # Backup the most important directories into an archive named after
# the machine this script is currently running on: # the machine this script is currently running on:
notify info "Starting backup to $BORG_REPO" notify info "Starting backup to $BORG_REPO"
@ -58,7 +69,7 @@ borg create \
--exclude '/home/*/.local/share/Trash/' \ --exclude '/home/*/.local/share/Trash/' \
--exclude '/home/*/.thumbnails/' \ --exclude '/home/*/.thumbnails/' \
\ \
::'{hostname}-{now:%Y-%m-%dT%H:%M:%S.%f}' \ ::'{hostname}-{now:%Y-%m-%d-%H:%M:%S.%f}' \
/boot \ /boot \
/bin \ /bin \
/efi \ /efi \
@ -80,7 +91,7 @@ backup_exit=$?
# archives of THIS machine. The '{hostname}-*' matching is very important to # archives of THIS machine. The '{hostname}-*' matching is very important to
# limit prune's operation to this machine's archives and not apply to # limit prune's operation to this machine's archives and not apply to
# other machines' archives also: # other machines' archives also:
notify info "Pruning repository" notify info "Pruning repository: $BORG_REPO"
infoLine "Pruning repository" infoLine "Pruning repository"
borg prune \ borg prune \
@ -96,21 +107,23 @@ borg prune \
prune_exit=$? prune_exit=$?
# actually free repo disk space by compacting segments # actually free repo disk space by compacting segments
notify info "Compacting repository: $BORG_REPO"
notify info "Compacting repository"
infoLine "Compacting repository" infoLine "Compacting repository"
borg compact 2>> "$logFile" borg compact 2>> "$logFile"
compact_exit=$? compact_exit=$?
# Don't forget to tidy up the log files!
tidyLogs
# use highest exit code as global exit code # use highest exit code as global exit code
global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit )) global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))
global_exit=$(( compact_exit > global_exit ? compact_exit : global_exit )) global_exit=$(( compact_exit > global_exit ? compact_exit : global_exit ))
if [ ${global_exit} -eq 0 ]; then if [ ${global_exit} -eq 0 ]; then
notify info "Backup, Prune, and Compact finished successfully to $BORG_REPO" notify info "Backup, Prune, and Compact finished successfully to $BORG_REPO"
infoLine "Backup, Prune, and Compact finished successfully" infoLine "Backup, Prune, and Compact finished successfully to $BORG_REPO"
seeLog "Log available:" seeLog "Log available:"
elif [ ${global_exit} -eq 1 ]; then elif [ ${global_exit} -eq 1 ]; then
notify info "Backup, Prune, and/or Compact finished with warnings" notify info "Backup, Prune, and/or Compact finished with warnings"

View File

@ -2,6 +2,8 @@
# Setting this, so the repo does not need to be given on the commandline: # Setting this, so the repo does not need to be given on the commandline:
machine="$( hostnamectl --static )" machine="$( hostnamectl --static )"
#remoteHost=""
#remoteUser=""
export BORG_REPO="/backup/$machine" export BORG_REPO="/backup/$machine"
# See the section "Passphrase notes" for more infos. # See the section "Passphrase notes" for more infos.
@ -11,11 +13,20 @@ export BORG_PASSPHRASE=''
export BORG_RELOCATED_REPO_ACCESS_IS_OK=yes export BORG_RELOCATED_REPO_ACCESS_IS_OK=yes
# some helpers and error handling: # some helpers and error handling:
logFile="/home/ez/.cache/logs/borg/$( date +%Y-%m-%d-%H:%M:%S )" logPath="/home/ez/.cache/logs/borg"
logFile="$logPath/$( date +%Y-%m-%d-%H:%M:%S.%N )"
infoLine() { printf "%s\t%s\n" "$( date +%H:%M:%S )" "$*" | tee -a "$logFile"; } infoLine() { printf "%s\t%s\n" "$( date +%H:%M:%S )" "$*" | tee -a "$logFile"; }
seeLog() { printf "\n%s %s" "$*" "$logFile"; } seeLog() { printf "\n%s %s" "$*" "$logFile"; }
trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM
# Remove logs of pruned backups
tidyLogs() {
mapfile -t removeList < <(grep -w "^Pruning archive" "$logFile" | awk '{ print $4 }' )
for ((i=0; i<${#removeList[@]}; i++)); do
rm "$logPath/${removeList[i]}"
done
}
# Backup the most important directories into an archive named after # Backup the most important directories into an archive named after
# the machine this script is currently running on: # the machine this script is currently running on:
infoLine "Starting backup to $BORG_REPO" infoLine "Starting backup to $BORG_REPO"
@ -79,10 +90,14 @@ prune_exit=$?
# actually free repo disk space by compacting segments # actually free repo disk space by compacting segments
infoLine "Compacting repository" infoLine "Compacting repository"
borg compact 2>> "$logFile" borg compact 2>> "$logFile"
compact_exit=$? compact_exit=$?
# Don't forget to tidy up the log files!
tidyLogs
# use highest exit code as global exit code # use highest exit code as global exit code
global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit )) global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))
global_exit=$(( compact_exit > global_exit ? compact_exit : global_exit )) global_exit=$(( compact_exit > global_exit ? compact_exit : global_exit ))