|
|
|
|
@@ -1,6 +1,5 @@
|
|
|
|
|
#!/bin/bash
|
|
|
|
|
## Script to run Restic backups to a B2 backend.
|
|
|
|
|
## It makes sure each of the preconfigured repositories can be safely backed up.
|
|
|
|
|
##Simple script to run Restic backups
|
|
|
|
|
|
|
|
|
|
show_help()
|
|
|
|
|
{
|
|
|
|
|
@@ -9,11 +8,11 @@ show_help()
|
|
|
|
|
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 " {-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)"
|
|
|
|
|
echo "Gerbil-TK Photos (path: /var/Red-Vol/Media/Pictures)"
|
|
|
|
|
echo "Pukeko-XYZ-Containers Containers (path: /var/Red-Vol/Media/Containers)"
|
|
|
|
|
echo "Pukeko-XYZ-Cloud Data from all devices (path: /var/Red-Vol/Media/Cloud)"
|
|
|
|
|
exit 0
|
|
|
|
|
}
|
|
|
|
|
#Pass arguments to the script
|
|
|
|
|
@@ -45,7 +44,23 @@ flags()
|
|
|
|
|
}
|
|
|
|
|
flags "$@"
|
|
|
|
|
|
|
|
|
|
## Defaults
|
|
|
|
|
#File to write results to; picked up by Prometheus and yells about changes
|
|
|
|
|
PROM_FILE="$CONTAINER_DIR/prometheus/data/kumonoboru.prom"
|
|
|
|
|
|
|
|
|
|
#Remove log from last runs, if present
|
|
|
|
|
if [[ -f $PROM_FILE ]]; then
|
|
|
|
|
rm $PROM_FILE
|
|
|
|
|
fi
|
|
|
|
|
## Monitoring codes:
|
|
|
|
|
#+ -3 - failed pruning
|
|
|
|
|
#+ -2 - failed integrity check
|
|
|
|
|
#+ -1 - failed to unlock
|
|
|
|
|
#+ 0 - succesfully backed up
|
|
|
|
|
#+ 1 - failed backup
|
|
|
|
|
#+ 2 - passed integrity check
|
|
|
|
|
#+ 3- succesfully pruned
|
|
|
|
|
|
|
|
|
|
#Defaults
|
|
|
|
|
if [[ -z $BWLIMIT ]]; then
|
|
|
|
|
export BWLIMIT="0"
|
|
|
|
|
else
|
|
|
|
|
@@ -58,6 +73,10 @@ if [[ -n $REPOSITORY ]]; then
|
|
|
|
|
echo -e "Will only process repository" "$1"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
export B2_ACCOUNT_ID=8582a42a3b99 #Master Key
|
|
|
|
|
export B2_ACCOUNT_KEY=00041845e8dd29d7e3d091d77bb8a631ee71332be7 #Master Application ID
|
|
|
|
|
RESTIC_PASSWORD='f$774$#je4%U8vp8ov*UsZMHqL$m3Smh#fEbbt7hyULQxfnnWmSiS5MEndzVWT$$n^@s$P*o4vV*^rgv3jvvrv@y35VppU$$y*vnG5V@botU&4$39Y6t9HSb3Z548M!4'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#Safety function; accepts repository to check
|
|
|
|
|
safety(){
|
|
|
|
|
@@ -66,7 +85,8 @@ safety(){
|
|
|
|
|
#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
|
|
|
|
|
echo "system_backup{name=\"$REPOSITORY\"} -1" >> $PROM_FILE
|
|
|
|
|
return 1 # code for ^ failed to unlock
|
|
|
|
|
# ^ If there's a restic process holding the repository, leave it alone.
|
|
|
|
|
else
|
|
|
|
|
echo -e "Repository is not in use - unlocking"
|
|
|
|
|
@@ -84,8 +104,10 @@ 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."
|
|
|
|
|
echo "system_backup{name=\"$REPOSITORY\"} 0" >> $PROM_FILE
|
|
|
|
|
else
|
|
|
|
|
echo -e "$REPOSITORY failed to upload path" "$REPOSITORY_PATH"
|
|
|
|
|
echo "system_backup{name=\"$REPOSITORY\"} 1" >> $PROM_FILE
|
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
@@ -107,6 +129,9 @@ check(){
|
|
|
|
|
restic -r b2:"$REPOSITORY" snapshots | tee -a $LOG
|
|
|
|
|
else
|
|
|
|
|
echo -e "Repository failed integrity check - " "$REPOSITORY"
|
|
|
|
|
echo "system_backup{name=\"$REPOSITORY\"} -2" >> $PROM_FILE
|
|
|
|
|
# code for ^ failed integrity check
|
|
|
|
|
|
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
@@ -122,30 +147,33 @@ clean(){
|
|
|
|
|
# Marks repository as cleaned already ^ so it won't passed to this function again.
|
|
|
|
|
else
|
|
|
|
|
echo -e "Failed to prune repository" "$REPOSITORY"
|
|
|
|
|
echo "system_backup{name=\"$REPOSITORY\"} -3" >> $PROM_FILE
|
|
|
|
|
# code for ^ failed pruning
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
check Gerbil-TK
|
|
|
|
|
clean Gerbil-TK
|
|
|
|
|
check Pukeko-XYZ-Containers
|
|
|
|
|
clean Pukeko-XYZ-Containers
|
|
|
|
|
check Pukeko-XYZ-Cloud
|
|
|
|
|
clean Pukeko-XYZ-Cloud
|
|
|
|
|
|
|
|
|
|
#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/
|
|
|
|
|
(Gerbil-TK)
|
|
|
|
|
backup Gerbil-TK /var/Red-Vol/Media/Pictures/
|
|
|
|
|
;;
|
|
|
|
|
(REPO-2)
|
|
|
|
|
backup REPO-2 /stuff/stuff2/
|
|
|
|
|
(Pukeko-XYZ-Containers)
|
|
|
|
|
backup Pukeko-XYZ-Containers /var/Red-Vol/Media/Containers/
|
|
|
|
|
;;
|
|
|
|
|
(REPO-3)
|
|
|
|
|
backup REPO-3 /stuff/stuff3/
|
|
|
|
|
(Pukeko-XYZ-Cloud)
|
|
|
|
|
backup Pukeko-XYZ-Cloud /var/Red-Vol/Media/Cloud/
|
|
|
|
|
;;
|
|
|
|
|
(*)
|
|
|
|
|
show_help;;
|
|
|
|
|
@@ -153,24 +181,24 @@ elif [[ -n $REPOSITORY ]] && [[ -z $CLEAN ]]; then
|
|
|
|
|
|
|
|
|
|
#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/
|
|
|
|
|
backup Gerbil-TK /var/Red-Vol/Media/Pictures/
|
|
|
|
|
backup Pukeko-XYZ-Containers /var/Red-Vol/Media/Containers/
|
|
|
|
|
backup Pukeko-XYZ-Cloud /var/Red-Vol/Media/Cloud/
|
|
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
(Gerbil-TK)
|
|
|
|
|
check Gerbil-TK
|
|
|
|
|
clean Gerbil-TK
|
|
|
|
|
;;
|
|
|
|
|
(REPO-2)
|
|
|
|
|
check REPO-2
|
|
|
|
|
clean REPO-2
|
|
|
|
|
(Pukeko-XYZ-Containers)
|
|
|
|
|
check Pukeko-XYZ-Containers
|
|
|
|
|
clean Pukeko-XYZ-Containers
|
|
|
|
|
;;
|
|
|
|
|
(REPO-3)
|
|
|
|
|
check REPO-3
|
|
|
|
|
clean REPO-3
|
|
|
|
|
(Pukeko-XYZ-Cloud)
|
|
|
|
|
check Pukeko-XYZ-Cloud
|
|
|
|
|
clean Pukeko-XYZ-Cloud
|
|
|
|
|
;;
|
|
|
|
|
(*)
|
|
|
|
|
show_help;;
|
|
|
|
|
@@ -178,4 +206,4 @@ elif [[ -n $REPOSITORY ]] && [[ -n $CLEAN ]]; then
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
#Wrap up this run's log and report nicely
|
|
|
|
|
echo "All done; have a nice day!"
|
|
|
|
|
echo "All done; have a nice day!"
|