Logging format updates - hurray!

This commit is contained in:
2022-10-16 22:11:19 +03:00
parent 4fedc39d1a
commit 42782597cd
3 changed files with 84 additions and 48 deletions

View File

@@ -1,7 +1,6 @@
#!/bin/bash
##Simple script to run Restic backups
source /root/.bash_profile
source /home/shmick/Scripts/Okiru
source /etc/environment
export HOME=/root/
arg0=$(basename "$0")
@@ -26,6 +25,7 @@ help()
echo " {-c|--clean} -- Force prune of the remote repositories"
echo " {-r|--repository} repository -- Only backup the specified repository."
echo " {-l|--limit} #[Kbps] -- Limit upload & download speed"
echo " {-v|--verbose} -- Print debug messages"
echo " {-h|--help} -- Print this help message and exit"
echo "Available repositories:"
echo "Gerbil-TK Photos (path: /var/Red-Vol/Media/Pictures)"
@@ -44,18 +44,19 @@ flags()
do
case "$1" in
(-c|--clean)
debug "Cleaning will take place per request."
export CLEAN="1"
shift;;
(-r|--repository)
shift
export REPOSITORY="$1"
debug "Only repository $1 will be processed per request."
shift;;
(-l|--limit)
shift
export BWLIMIT="$1"
debug "Bandwidth will be limited to $BWLIMIT Kbps per request."
shift;;
(-v|--verbose)
shift
export VERBOSE="$1"
shift;;
(-h|--help)
help;;
@@ -64,23 +65,32 @@ flags()
done
}
flags "$@"
source /home/shmick/Scripts/Okiru "$VERBOSE"
logging Kumonoboru
#Defaults
if [[ -z $BWLIMIT ]]; then
export BWLIMIT="0"
else
debug "Bandwidth will be limited to" "$BWLIMIT Kbps"
fi
if [[ -n $CLEAN ]]; then
debug "Cleaning will take place per request."
fi
if [[ -n $REPOSITORY ]]; then
debug "Will only process repository" "$1"
fi
#Safety function; accepts repository to check
safety(){
REPOSITORY="$1"
info "Checking if repository $REPOSITORY is in use..."
info "Checking if repository is in use - " "$REPOSITORY"
#Check no other Restic process is using this repository; Free unnecessary locks, if present
if [[ -n $(ps aux | grep restic | grep "$REPOSITORY") ]]; then
warn "Repository $REPOSITORY is in use - ignoring"
warn "Repository is in use - ignoring"
return 1
# ^ If there's a restic process holding the repository, leave it alone.
else
info "Repository $REPOSITORY is not in use - unlocking"
restic -r b2:$REPOSITORY unlock
info "Repository is not in use - unlocking"
restic -q -r b2:$REPOSITORY unlock
# ^ If a lock exists but no process, the repository is safe and should be unlocked.
fi
}
@@ -90,46 +100,46 @@ backup(){
REPOSITORY_PATH="$2"
if safety "$REPOSITORY"; then
#Run the backup
info "Backing up repository $REPOSITORY"
info "Backing up repository" "$REPOSITORY"
if restic --cache-dir="$RESTIC_CACHE_DIR" -r b2:"$REPOSITORY" backup "$REPOSITORY_PATH" --limit-upload="$BWLIMIT" --limit-download="$BWLIMIT" | tee -a $LOG; then
ok "Path $REPOSITORY_PATH completed upload to $REPOSITORY."
ok "$REPOSITORY_PATH" "completed upload to $REPOSITORY."
check "$REPOSITORY"
else
error "Repository $REPOSTIORY failed to upload path $REPOSITORY_PATH!"
error "$REPOSITORY failed to upload path" "$REPOSITORY_PATH"
fi
fi
}
check(){
REPOSITORY="$1"
PRUNE="$2"
debug "Working on Repostory $1 with prune option $2"
debug "Checking integrity (prune: $PRUNE) of repository" "$REPOSITORY"
## ^ This variable will have value if repo is already clean, indicating
#+ This is a post backup check.
if [[ -n $PRUNE ]]; then
warn "This repository has been cleaned already; will not clean again."
fi
if safety "$REPOSITORY"; then
info "Checking repository $REPOSITORY"
info "Checking repository health - " "$REPOSITORY"
if restic -r b2:"$REPOSITORY" check --limit-upload="$BWLIMIT" --limit-download="$BWLIMIT" | tee -a $LOG; then
ok "Repository $REPOSITORY passed integrity check!"
ok "Repository passed integrity check - " "$REPOSITORY"
info "Current snapshots:"
restic -r b2:"$REPOSITORY" snapshots | tee -a $LOG
else
error "Repository $REPOSITORY failed integrity check!"
error "Repository failed integrity check - " "$REPOSITORY"
fi
fi
}
clean(){
REPOSITORY="$1"
if safety "$REPOSITORY"; then
info "Cleaning repository $REPOSITORY"
info "Cleaning repository" "$REPOSITORY"
if restic -r b2:$REPOSITORY forget --keep-daily 7 --keep-weekly 4 --keep-monthly 12 --prune --limit-upload="$BWLIMIT" --limit-download="$BWLIMIT" | tee -a $LOG; then
ok "Repository $REPOSITORY is trim!"
ok "Repository is trim - " "$REPOSITORY"
debug "Running post clean check..."
check "$REPOSITORY" "1"
# Marks repository as cleaned already ^ so it won't passed to this function again.
else
error "Repository $REPOSITORY failed to prune!"
error "Failed to prune repository" "$REPOSITORY"
fi
fi
}