backup/backup.sh - only create backup whilst services down; check after
This commit is contained in:
@@ -9,6 +9,6 @@ DC_DIR=/opt/docker-compose/
|
|||||||
|
|
||||||
cd $DC_DIR
|
cd $DC_DIR
|
||||||
docker-compose down
|
docker-compose down
|
||||||
docker-compose run --name borgmatic -T --rm borgmatic /backup.sh
|
docker-compose run --name borgmatic -T --rm borgmatic /backup.sh run
|
||||||
docker-compose up -d main-services
|
docker-compose up -d main-services
|
||||||
|
docker-compose run --name borgmatic -T --rm borgmatic /backup.sh check
|
||||||
|
|||||||
@@ -1,42 +1,49 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
# Run the backup and mail the logs
|
# Run the backup and mail the logs:
|
||||||
# Set TEST_SMTP=1 to run in SMTP testing mode, which will just send an email
|
# Depending on parameter 1:
|
||||||
|
# - test-smtp: just send a test email to $MAIL_TO
|
||||||
|
# - run: create the backup, no checks
|
||||||
|
# - check: prune, compact and check the backup
|
||||||
|
# Anything else is an error.
|
||||||
|
|
||||||
set -o pipefail
|
set -o pipefail
|
||||||
|
|
||||||
# Set up environment
|
# Set up environment
|
||||||
/bin/sh /scripts/msmtprc.sh
|
/bin/sh /scripts/msmtprc.sh
|
||||||
/bin/sh /scripts/env.sh
|
/bin/sh /scripts/env.sh
|
||||||
BACKUP_COMMAND="borgmatic --stats -v 2"
|
RUN_COMMAND="borgmatic --stats -v 2 create"
|
||||||
|
CHECK_COMMAND="borgmatic --stats -v 1 prune compact check"
|
||||||
LOGFILE="/tmp/backup_run_$(date +%s).log"
|
LOGFILE="/tmp/backup_run_$(date +%s).log"
|
||||||
SUCCESS_PREFIX="=?utf-8?Q? =E2=9C=85 SUCCESS?="
|
SUCCESS_PREFIX="=?utf-8?Q? =E2=9C=85 SUCCESS?="
|
||||||
FAILED_PREFIX="=?utf-8?Q? =E2=9D=8C FAILED?="
|
FAILED_PREFIX="=?utf-8?Q? =E2=9D=8C FAILED?="
|
||||||
|
|
||||||
|
PARAM="$1"
|
||||||
|
|
||||||
# Helper function to prepend a timestamp and the first parameter to every line of STDIN
|
# Helper function to prepend a timestamp and the first parameter to every line of STDIN
|
||||||
_indent() {
|
indent() {
|
||||||
while read -rs line; do
|
while read -rs line; do
|
||||||
printf "%s%s\n" "$(date +%s)" "$1" "$line"
|
printf "%s%s%s\n" "$(date -Iminutes)" "${1:- }" "$line"
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
# This function prepends timestamps to stderr and stdout of the
|
# This function prepends timestamps to stderr and stdout of the
|
||||||
# command supplied as parameters to this.
|
# command supplied as parameters to this.
|
||||||
log() {
|
log() {
|
||||||
# Adapted from https://stackoverflow.com/a/31151808
|
# Adapted from https://stackoverflow.com/a/31151808
|
||||||
{
|
{
|
||||||
"$@" 2>&1 1>&3 3>&- | _indent " ! "
|
"$@" 2>&1 1>&3 3>&- | indent " ! "
|
||||||
} 3>&1 1>&2 | _indent " | " >>"$LOGFILE"
|
} 3>&1 1>&2 | indent " | " | tee -a "$LOGFILE"
|
||||||
}
|
}
|
||||||
|
|
||||||
report() {
|
report() {
|
||||||
if [ -n "$SUCCESS" ]; then
|
if [ "$RESULT" = "0" ]; then
|
||||||
log echo SUCCESS
|
log echo "SUCCESS"
|
||||||
PREFIX="$SUCCESS_PREFIX"
|
PREFIX="$SUCCESS_PREFIX"
|
||||||
else
|
else
|
||||||
log echo FAILED
|
log echo "FAILED: $RESULT"
|
||||||
PREFIX="$FAILED_PREFIX"
|
PREFIX="$FAILED_PREFIX"
|
||||||
fi
|
fi
|
||||||
printf "Subject: $PREFIX: $MAIL_SUBJECT\n\n$(cat $LOGFILE)\n" |
|
printf "Subject: $PREFIX: '$PARAM'\n\n$(cat $LOGFILE)\n" |
|
||||||
sendmail -t "$MAIL_TO"
|
sendmail -t "$MAIL_TO"
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,16 +68,27 @@ cleanup() {
|
|||||||
trap failed INT QUIT KILL
|
trap failed INT QUIT KILL
|
||||||
trap cleanup EXIT
|
trap cleanup EXIT
|
||||||
|
|
||||||
if [ -n "$TEST_SMTP" ]; then
|
case "$PARAM" in
|
||||||
echo "Testing mail to $MAIL_TO"
|
test-smtp)
|
||||||
testmail
|
echo "Testing mail to $MAIL_TO"
|
||||||
echo "Done."
|
testmail
|
||||||
exit
|
echo "Done."
|
||||||
fi
|
;;
|
||||||
|
check)
|
||||||
|
log echo STARTED: $CHECK_COMMAND
|
||||||
|
log $CHECK_COMMAND
|
||||||
|
RESULT=$?
|
||||||
|
report
|
||||||
|
;;
|
||||||
|
run)
|
||||||
|
log echo STARTED: $RUN_COMMAND
|
||||||
|
log $RUN_COMMAND
|
||||||
|
RESULT=$?
|
||||||
|
report
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
log echo "UNKNOWN COMMAND: '$PARAM'"
|
||||||
|
report
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
|
||||||
echo "STARTED" >> "$LOGFILE"
|
|
||||||
if log $BACKUP_COMMAND; then
|
|
||||||
SUCCESS=1
|
|
||||||
report
|
|
||||||
fi
|
|
||||||
|
|||||||
Reference in New Issue
Block a user