Switch to cron for scheduling.

master
Jarno Rantanen 2018-11-26 11:46:26 +02:00
rodzic 2269f45a73
commit c6ebbe3937
3 zmienionych plików z 48 dodań i 17 usunięć

Wyświetl plik

@ -1,12 +1,15 @@
FROM ubuntu:18.04
RUN apt-get update && apt-get install -y curl awscli
RUN apt-get update && apt-get install -y curl cron awscli
# https://docs.docker.com/engine/installation/linux/docker-ce/ubuntu/#install-using-the-convenience-script
RUN curl -fsSL get.docker.com -o get-docker.sh
RUN sh get-docker.sh
COPY ./entrypoint.sh /root/
COPY ./backup.sh /root/
RUN chmod a+x /root/entrypoint.sh
RUN chmod a+x /root/backup.sh
CMD [ "/root/backup.sh" ]
WORKDIR /root
CMD [ "/root/entrypoint.sh" ]

Wyświetl plik

@ -3,17 +3,8 @@
# Exit immediately on error
set -e
# Set defaults for any missing environment variables
BACKUP_SOURCES="${BACKUP_SOURCES:-/backup}"
BACKUP_FILENAME="${BACKUP_FILENAME:-latest.tar.gz}"
BACKUP_PERIOD_SECONDS="${BACKUP_PERIOD_SECONDS:-86400}" # i.e. every 24 hours
BACKUP_UPLOAD_WAIT_SECONDS="${BACKUP_UPLOAD_WAIT_SECONDS:-30}" # to wait out the load spike of starting the containers back up
BACKUP_HOSTNAME="${BACKUP_HOSTNAME:-$(hostname)}"
DOCKER_STOP_OPT_IN_LABEL="${DOCKER_STOP_OPT_IN_LABEL:-docker-volume-backup-companion.stop-during-backup}"
INFLUXDB_URL="${INFLUXDB_URL:-}"
INFLUXDB_DB="${INFLUXDB_DB:-}"
INFLUXDB_CREDENTIALS="${INFLUXDB_CREDENTIALS:-}"
INFLUXDB_MEASUREMENT="${INFLUXDB_MEASUREMENT:-docker_volume_backup}"
# Cronjobs don't inherit their env, so load from file
source env.sh
function info {
bold="\033[1m"
@ -21,10 +12,6 @@ function info {
echo -e "\n$bold[INFO] $1\n$reset"
}
info "Backup sleeping"
echo "Sleeping $BACKUP_PERIOD_SECONDS seconds..."
sleep "$BACKUP_PERIOD_SECONDS"
info "Backup starting"
TIME_START="$(date +%s.%N)"
CONTAINERS_TO_STOP="$(docker ps --format "{{.ID}}" --filter "label=$DOCKER_STOP_OPT_IN_LABEL=true" | tr '\n' ' ')"

41
entrypoint.sh 100644
Wyświetl plik

@ -0,0 +1,41 @@
#!/bin/bash
# Exit immediately on error
set -e
# Write cronjob env to file, fill in sensible defaults, and read them back in
cat <<EOF > env.sh
BACKUP_SOURCES="${BACKUP_SOURCES:-/backup}"
BACKUP_CRON_EXPRESSION="${BACKUP_CRON_EXPRESSION:-@daily}"
BACKUP_BUCKET_NAME="${BACKUP_BUCKET_NAME:-}"
BACKUP_FILENAME="${BACKUP_FILENAME:-latest.tar.gz}"
BACKUP_UPLOAD_WAIT_SECONDS="${BACKUP_UPLOAD_WAIT_SECONDS:-30}"
BACKUP_HOSTNAME="${BACKUP_HOSTNAME:-$(hostname)}"
DOCKER_STOP_OPT_IN_LABEL="${DOCKER_STOP_OPT_IN_LABEL:-docker-volume-backup-companion.stop-during-backup}"
INFLUXDB_URL="${INFLUXDB_URL:-}"
INFLUXDB_DB="${INFLUXDB_DB:-}"
INFLUXDB_CREDENTIALS="${INFLUXDB_CREDENTIALS:-}"
INFLUXDB_MEASUREMENT="${INFLUXDB_MEASUREMENT:-docker_volume_backup}"
EOF
chmod a+x env.sh
source env.sh
# Configure AWS CLI
mkdir .aws
cat <<EOF > .aws/credentials
[default]
aws_access_key_id = ${AWS_ACCESS_KEY_ID}
aws_secret_access_key = ${AWS_SECRET_ACCESS_KEY}
EOF
cat <<EOF > .aws/config
[default]
region = ${AWS_DEFAULT_REGION}
EOF
# Add our cron entry, and direct stdout & stderr to Docker commands stdout
echo "Installing cron.d entry: docker-volume-backup-companion"
echo "$BACKUP_CRON_EXPRESSION root /root/backup.sh > /proc/1/fd/1 2>&1" > /etc/cron.d/docker-volume-backup-companion
# Let cron take the wheel
echo "Starting cron in foreground with expression: $BACKUP_CRON_EXPRESSION"
cron -f