Files
kumonoboru/kumonoboru.sh
shmick 5753b425d6
Some checks failed
Configure Kumonoboru / deploy (push) Has been cancelled
Sanitized for CV
2023-11-21 23:01:20 +02:00

182 lines
5.0 KiB
Bash
Executable File

#!/bin/bash
## Script to run Restic backups to a B2 backend.
## It makes sure each of the preconfigured repositories can be safely backed up.
show_help()
{
echo "Kumonoboru - Back up important location to the B2 cloud using Restic."
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|--show_help} -- Print this show_help message and exit"
echo "Available repositories:"
echo "REPO-1 stuff (path: /stuff/stuff1)"
echo "REPO-2 other stuff (path: /stuff/stuff2)"
echo "REPO-3 more stuff (path: /stuff/stuff3)"
exit 0
}
#Pass arguments to the script
flags()
{
#This is utterly useless
if [[ $# == "0" ]]; then
:
fi
while test $# -gt 0
do
case "$1" in
(-c|--clean)
export CLEAN="1"
shift;;
(-r|--repository)
shift
export REPOSITORY="$1"
shift;;
(-l|--limit)
shift
export BWLIMIT="$1"
shift;;
(-h|--show_help)
show_help;;
(*) show_help;;
esac
done
}
flags "$@"
## Defaults
if [[ -z $BWLIMIT ]]; then
export BWLIMIT="0"
else
echo -e "Bandwidth will be limited to" "$BWLIMIT Kbps"
fi
if [[ -n $CLEAN ]]; then
echo -e "Cleaning will take place per request."
fi
if [[ -n $REPOSITORY ]]; then
echo -e "Will only process repository" "$1"
fi
#Safety function; accepts repository to check
safety(){
REPOSITORY="$1"
echo -e "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
echo -e "Repository is in use - ignoring"
return 1
# ^ If there's a restic process holding the repository, leave it alone.
else
echo -e "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
}
#Backup function; accepts repository and path to backup
backup(){
REPOSITORY="$1"
REPOSITORY_PATH="$2"
if safety "$REPOSITORY"; then
#Run the backup
echo -e "Backing up repository" "$REPOSITORY"
if restic --cache-dir="$RESTIC_CACHE_DIR" -r b2:"$REPOSITORY" backup "$REPOSITORY_PATH" --limit-upload="$BWLIMIT" --limit-download="$BWLIMIT"; then
echo -e "$REPOSITORY_PATH" "completed upload to $REPOSITORY."
else
echo -e "$REPOSITORY failed to upload path" "$REPOSITORY_PATH"
fi
fi
}
check(){
REPOSITORY="$1"
PRUNE="$2"
echo -e "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
echo -e "This repository has been cleaned already; will not clean again."
fi
if safety "$REPOSITORY"; then
echo -e "Checking repository health - " "$REPOSITORY"
if restic -r b2:"$REPOSITORY" check --limit-upload="$BWLIMIT" --limit-download="$BWLIMIT"; then
echo -e "Repository passed integrity check - " "$REPOSITORY"
echo -e "Current snapshots:"
restic -r b2:"$REPOSITORY" snapshots | tee -a $LOG
else
echo -e "Repository failed integrity check - " "$REPOSITORY"
fi
fi
}
clean(){
REPOSITORY="$1"
if safety "$REPOSITORY"; then
echo -e "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"; then
echo -e "Repository is trim - " "$REPOSITORY"
echo -e "Running post clean check..."
check "$REPOSITORY" "1"
# Marks repository as cleaned already ^ so it won't passed to this function again.
else
echo -e "Failed to prune repository" "$REPOSITORY"
fi
fi
}
#If cleaning was forced, or if it's the first of this month - clean.
if [[ -n $CLEAN ]] || [[ $(date +%d) == "1" ]]; then
check REPO-1
clean REPO-1
check REPO-2
clean REPO-2
check REPO-3
clean REPO-3
#If a specific repository was requested, back it up; otherwise, back them all up.
elif [[ -n $REPOSITORY ]] && [[ -z $CLEAN ]]; then
case "$REPOSITORY" in
(REPO-1)
backup REPO-1 /stuff/stuff1/
;;
(REPO-2)
backup REPO-2 /stuff/stuff2/
;;
(REPO-3)
backup REPO-3 /stuff/stuff3/
;;
(*)
show_help;;
esac
#If cleaning was not forced, backup the repositories
elif [[ -z $CLEAN ]]; then
backup REPO-1 /stuff/stuff1/
backup REPO-2 /stuff/stuff2/
backup REPO-3 /stuff/stuff3/
#If a specific repository was requested to be cleaned, clean it
elif [[ -n $REPOSITORY ]] && [[ -n $CLEAN ]]; then
case "$REPOSITORY" in
(REPO-1)
check REPO-1
clean REPO-1
;;
(REPO-2)
check REPO-2
clean REPO-2
;;
(REPO-3)
check REPO-3
clean REPO-3
;;
(*)
show_help;;
esac
fi
#Wrap up this run's log and report nicely
echo "All done; have a nice day!"