feat: support email notification when backup succeeds and fails

This commit is contained in:
ttionya
2020-06-14 01:59:29 +08:00
parent 299fe93b9d
commit 89e45af55a
6 changed files with 141 additions and 3 deletions

View File

@@ -1,4 +1,4 @@
FROM rclone/rclone:1.52.0
FROM rclone/rclone:1.52.1
LABEL "repository"="https://github.com/ttionya/BitwardenRS-Backup" \
"homepage"="https://github.com/ttionya/BitwardenRS-Backup" \
@@ -7,6 +7,6 @@ LABEL "repository"="https://github.com/ttionya/BitwardenRS-Backup" \
COPY scripts/*.sh /app/
RUN chmod +x /app/*.sh \
&& apk add --no-cache sqlite zip
&& apk add --no-cache sqlite zip heirloom-mailx
ENTRYPOINT ["/app/entrypoint.sh"]

View File

@@ -112,6 +112,62 @@ Only keep last a few days backup files in the storage system. Set to `0` to keep
Default: `0`
#### MAIL_SMTP_ENABLE
The tool uses [heirloom-mailx](https://www.systutorials.com/docs/linux/man/1-heirloom-mailx/) to send mail.
Default: `FALSE`
#### MAIL_SMTP_VARIABLES
Because the configuration for sending emails is too complicated, we allow you to configure it yourself.
**We will set the subject according to the usage scenario, so you should not use the `-s` option.**
When testing, we will add the `-v` option to display detailed information.
```text
# My example:
# For Zoho
-S smtp-use-starttls \
-S smtp=smtp://smtp.zoho.com:587 \
-S smtp-auth=login \
-S smtp-auth-user=<my-email-address> \
-S smtp-auth-password=<my-email-password> \
-S from=<my-email-address>
```
See [here](https://www.systutorials.com/sending-email-from-mailx-command-in-linux-using-gmails-smtp/) for more information.
#### MAIL_TO
Who will receive the notification email.
#### MAIL_WHEN_SUCCESS
Send email when backup is successful.
Default: `TRUE`
#### MAIL_WHEN_FAILURE
Send email when backup fails.
Default: `TRUE`
## Mail Test
You can use the following command to test the mail sending. Remember to replace your smtp variables.
```shell
docker run --rm -it -e MAIL_SMTP_VARIABLES='<your smtp variables>' ttionya/bitwardenrs-backup:latest mail <mail send to>
# Or
docker run --rm -it -e MAIL_SMTP_VARIABLES='<your smtp variables>' -e MAIL_TO='<mail send to>' ttionya/bitwardenrs-backup:latest mail
```
## License
MIT

View File

@@ -3,7 +3,7 @@ version: '3'
services:
bitwardenrs:
image: bitwardenrs/server:1.15.0
image: bitwardenrs/server:1.15.1
restart: always
# environment:
# SIGNUPS_ALLOWED: 'false'
@@ -23,6 +23,11 @@ services:
# ZIP_ENABLE: 'TRUE'
# ZIP_PASSWORD: 'WHEREISMYPASSWORD?'
# BACKUP_KEEP_DAYS: 0
# MAIL_SMTP_ENABLE: 'FALSE'
# MAIL_SMTP_VARIABLES: ''
# MAIL_TO: ''
# MAIL_WHEN_SUCCESS: 'TRUE'
# MAIL_WHEN_FAILURE: 'TRUE'
volumes:
- bitwardenrs-data:/bitwarden/data/
- bitwardenrs-rclone-data:/config/

View File

@@ -94,6 +94,11 @@ function upload() {
rclone copy ${UPLOAD_FILE} ${RCLONE_REMOTE}
if [[ $? != 0 ]]; then
color red "upload failed"
if [[ "${MAIL_SMTP_ENABLE}" == "TRUE" && "${MAIL_WHEN_FAILURE}" == "TRUE" ]]; then
send_mail "BitwardenRS Backup Failed" "File upload failed at $(date +"%Y-%m-%d %H:%M:%S")."
fi
exit 1
fi
}
@@ -128,4 +133,8 @@ upload
clear_dir
clear_history
if [[ "${MAIL_SMTP_ENABLE}" == "TRUE" && "${MAIL_WHEN_SUCCESS}" == "TRUE" ]]; then
send_mail "BitwardenRS Backup Success" "The file was successfully uploaded at $(date +"%Y-%m-%d %H:%M:%S")."
fi
color none ""

View File

@@ -9,6 +9,22 @@ if [[ "$1" == "rclone" ]]; then
exit 0
fi
# mailx test
if [[ "$1" == "mail" ]]; then
MAIL_SMTP_ENABLE="TRUE"
MAIL_DEBUG="TRUE"
if [[ -n "$2" ]]; then
MAIL_TO="$2"
fi
init_env
send_mail "BitwardenRS Backup Test" "Your SMTP looks configured correctly."
exit 0
fi
function configure_cron() {
echo "${CRON} sh /app/backup.sh > /dev/stdout" >> /etc/crontabs/root
}

View File

@@ -32,6 +32,27 @@ function check_rclone_connection() {
fi
}
########################################
# Send mail by mailx.
# Arguments:
# mail subject
# mail content
# Outputs:
# send mail result
########################################
function send_mail() {
if [[ "${MAIL_DEBUG}" == "TRUE" ]]; then
local MAIL_VERBOSE="-v"
fi
echo "$2" | mailx ${MAIL_VERBOSE} -s "$1" ${MAIL_SMTP_VARIABLES} ${MAIL_TO}
if [[ $? != 0 ]]; then
color red "mail sending failed"
else
color blue "mail send was successfully"
fi
}
########################################
# Initialization environment variables.
# Arguments:
@@ -80,6 +101,31 @@ function init_env() {
BACKUP_KEEP_DAYS="${BACKUP_KEEP_DAYS_DEFAULT}"
fi
# MAIL_SMTP_ENABLE
# MAIL_TO
MAIL_SMTP_ENABLE=$(echo "${MAIL_SMTP_ENABLE}" | tr '[a-z]' '[A-Z]')
if [[ "${MAIL_SMTP_ENABLE}" == "TRUE" && -n "${MAIL_TO}" ]]; then
MAIL_SMTP_ENABLE="TRUE"
else
MAIL_SMTP_ENABLE="FALSE"
fi
# MAIL_WHEN_SUCCESS
MAIL_WHEN_SUCCESS=$(echo "${MAIL_WHEN_SUCCESS}" | tr '[a-z]' '[A-Z]')
if [[ "${MAIL_WHEN_SUCCESS}" == "FALSE" ]]; then
MAIL_WHEN_SUCCESS="FALSE"
else
MAIL_WHEN_SUCCESS="TRUE"
fi
# MAIL_WHEN_FAILURE
MAIL_WHEN_FAILURE=$(echo "${MAIL_WHEN_FAILURE}" | tr '[a-z]' '[A-Z]')
if [[ "${MAIL_WHEN_FAILURE}" == "FALSE" ]]; then
MAIL_WHEN_FAILURE="FALSE"
else
MAIL_WHEN_FAILURE="TRUE"
fi
color yellow "========================================"
color yellow "CRON: ${CRON}"
color yellow "RCLONE_REMOTE_NAME: ${RCLONE_REMOTE_NAME}"
@@ -88,5 +134,11 @@ function init_env() {
color yellow "ZIP_ENABLE: ${ZIP_ENABLE}"
color yellow "ZIP_PASSWORD: ${#ZIP_PASSWORD} Chars"
color yellow "BACKUP_KEEP_DAYS: ${BACKUP_KEEP_DAYS}"
color yellow "MAIL_SMTP_ENABLE: ${MAIL_SMTP_ENABLE}"
if [[ "${MAIL_SMTP_ENABLE}" == "TRUE" ]]; then
color yellow "MAIL_TO: ${MAIL_TO}"
color yellow "MAIL_WHEN_SUCCESS: ${MAIL_WHEN_SUCCESS}"
color yellow "MAIL_WHEN_FAILURE: ${MAIL_WHEN_FAILURE}"
fi
color yellow "========================================"
}