diff --git a/Dockerfile b/Dockerfile index cd24154..9f211f2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,8 @@ -FROM ovski/ansible:v2.10.6 +FROM ovski/ansible:v4.0.0 # Clone ansible playbooks -RUN apt-get update && apt-get install -y git +RUN apt-get --allow-releaseinfo-change update && apt-get install -y git +RUN git clone https://github.com/Ovski4/ansible-playbook-smtp-email.git /var/smtp-email-playbook RUN git clone https://github.com/Ovski4/ansible-playbook-mysql-dump.git /var/mysql-dump-playbook RUN git clone https://github.com/Ovski4/ansible-playbook-mongo-dump.git /var/mongo-dump-playbook RUN git clone https://github.com/Ovski4/ansible-playbook-borg-backup.git /var/borg-backup-playbook diff --git a/README.md b/README.md index d009953..1ec36e9 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,21 @@ Borg backup cron ================= A docker image to backup periodically a folder using borg. -Additionnally this image can dump a mysql database in the same folder beforehand. +Additionnally this image can: +- dump a mysql database in the same folder beforehand +- dump a mongo database +- create an elasticsearch snapshot +- send an email on failure You can also run the cron job directly by overriding the command with `/var/backup_script.sh` +- [Borg backup cron](#borg-backup-cron) + - [Build](#build) + - [Usage](#usage) + - [With mysql dump](#with-mysql-dump) + - [With mongo dump](#with-mongo-dump) + - [With elasticsearch snapshot](#with-elasticsearch-snapshot) + - [Sending an email on failure](#sending-an-email-on-failure) + Build ----- @@ -36,7 +48,7 @@ docker run \ ovski/borgbackup-cron ``` -With mysql dump +### With mysql dump ```bash docker run \ @@ -48,7 +60,7 @@ docker run \ ovski/borgbackup-cron ``` -With mongo dump +### With mongo dump ```bash docker run \ @@ -59,7 +71,7 @@ docker run \ ovski/borgbackup-cron ``` -With elasticsearch snapshot +### With elasticsearch snapshot ```bash docker run \ @@ -70,10 +82,26 @@ docker run \ ovski/borgbackup-cron ``` +### Sending an email on failure + +```bash +docker run \ + # ... other options + -e SMTP_USER=smtpuser@gmail.com \ + -e SMTP_PASSWORD=smtppassword \ + -e SMTP_PORT=465 \ + -e SMTP_HOST=smtp.gmail.com \ + -e MAIL_TO=user@recipient.com \ + -e MAIL_BODY="Email content" \ + -e MAIL_SUBJECT="Email subject" \ + ovski/borgbackup-cron +``` + You can also use secrets in a stack to store sensitive information. Instead of specifiying environment variables, create the following secrets in /var/secrets (default location): ``` /run/secrets/borg_passphrase instead of BORG_PASSPHRASE /run/secrets/db_password instead of MYSQL_PASSWORD +/run/secrets/smtp_password instead of SMTP_PASSWORD ``` diff --git a/backup_script.sh b/backup_script.sh index 86006af..ca34341 100644 --- a/backup_script.sh +++ b/backup_script.sh @@ -1,5 +1,21 @@ #!/bin/bash +send_email() { + ansible-playbook /var/smtp-email-playbook/main.yml \ + -e "smtp_user=$SMTP_USER" \ + -e "smtp_password=$SMTP_PASSWORD" \ + -e "smtp_port=$SMTP_PORT" \ + -e "smtp_host=$SMTP_HOST" \ + -e "mail_to='$MAIL_TO'" \ + -e "mail_body='$MAIL_BODY'" \ + -e "mail_subject='$MAIL_SUBJECT'" +} + +if [[ ! -z "$SMTP_USER" && ! -z "$SMTP_PASSWORD" && ! -z "$SMTP_PORT" && ! -z "$SMTP_HOST" && ! -z "$MAIL_TO" && ! -z "$MAIL_BODY" && ! -z "$MAIL_SUBJECT" ]]; then + set -o errexit -o errtrace + trap send_email ERR +fi + if [[ ! -z "$MYSQL_USER" && ! -z "$MYSQL_DATABASE" && ! -z "$MYSQL_PASSWORD" && ! -z "$MYSQL_HOST" ]]; then ansible-playbook /var/mysql-dump-playbook/main.yml \ -e "mysql_dumps_target_folder=$LOCAL_FOLDER" \ diff --git a/entrypoint.sh b/entrypoint.sh index 359eb60..b7ba5b3 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -27,6 +27,12 @@ if [[ -f /run/secrets/db_password ]]; then export MYSQL_PASSWORD=$(cat /run/secrets/db_password) fi + +if [[ -f /run/secrets/smtp_password ]]; then + echo "Setting SMTP_PASSWORD env variable from secret" + export SMTP_PASSWORD=$(cat /run/secrets/smtp_password) +fi + # Make env variables accessible in crontab declare -p | grep -Ev 'BASHOPTS|BASH_VERSINFO|EUID|PPID|SHELLOPTS|UID' > /container.env