commit 44775162049676e3785ace232b07f80206325843 Author: Laurent Le Houerou Date: Thu Jun 6 10:37:26 2019 +0400 Initial commit diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0279562 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,30 @@ +FROM alpine:latest as alpine +RUN apk add --no-cache \ + ca-certificates \ + tzdata + +FROM busybox:glibc + +COPY --from=alpine /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt +COPY --from=alpine /usr/share/zoneinfo /usr/share/zoneinfo + +# Get restic executable +ENV RESTIC_VERSION=0.9.5 +ADD https://github.com/restic/restic/releases/download/v${RESTIC_VERSION}/restic_${RESTIC_VERSION}_linux_amd64.bz2 / +RUN bzip2 -d restic_${RESTIC_VERSION}_linux_amd64.bz2 && mv restic_${RESTIC_VERSION}_linux_amd64 /bin/restic && chmod +x /bin/restic + +RUN mkdir -p /mnt/restic /var/spool/cron/crontabs /var/log + +# /data is the dir where you have to put the data to be backed up +VOLUME /data + +COPY backup.sh /bin/backup +COPY check.sh /bin/check +COPY entry.sh /entry.sh + +RUN touch /var/log/cron.log + +WORKDIR "/" + +ENTRYPOINT ["/entry.sh"] + diff --git a/backup.sh b/backup.sh new file mode 100644 index 0000000..87ed496 --- /dev/null +++ b/backup.sh @@ -0,0 +1,35 @@ +#!/bin/sh + +start=`date +%s` +echo "Starting Backup at $(date +"%Y-%m-%d %H:%M:%S")" +echo "RESTIC_REPOSITORY: ${RESTIC_REPOSITORY}" +echo "BACKUP_CRON: ${BACKUP_CRON}" +echo "RESTIC_JOB_ARGS: ${RESTIC_JOB_ARGS}" +echo "RESTIC_FORGET_ARGS: ${RESTIC_FORGET_ARGS}" + +restic backup /data ${RESTIC_JOB_ARGS} +rc=$? +echo "Finished backup at $(date)" +if [[ $rc == 0 ]]; then + echo "Backup Successful" +else + echo "Backup Failed with status ${rc}" + restic unlock + kill 1 +fi + +if [ -n "${RESTIC_FORGET_ARGS}" ]; then + echo "Forget about old snapshots based on RESTIC_FORGET_ARGS = ${RESTIC_FORGET_ARGS}" + restic forget ${RESTIC_FORGET_ARGS} + rc=$? + echo "Finished forget at $(date)" + if [[ $rc == 0 ]]; then + echo "Forget Successfull" + else + echo "Forget Failed with Status ${rc}" + restic unlock + fi +fi + +end=`date +%s` +echo "Finished Backup at $(date +"%Y-%m-%d %H:%M:%S") after $((end-start)) seconds" diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..4149aae --- /dev/null +++ b/build.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +docker build -t llehouerou/restic-backup . +docker push llehouerou/restic-backup diff --git a/check.sh b/check.sh new file mode 100644 index 0000000..becaf21 --- /dev/null +++ b/check.sh @@ -0,0 +1,21 @@ +#!/bin/sh + +start=`date +%s` +echo "Starting Check at $(date +"%Y-%m-%d %H:%M:%S")" +echo "RESTIC_REPOSITORY: ${RESTIC_REPOSITORY}" +echo "CHECK_CRON: ${CHECK_CRON}" +echo "RESTIC_CHECK_ARGS: ${RESTIC_CHECK_ARGS}" + +restic check ${RESTIC_CHECK_ARGS} +rc=$? +echo "Finished check at $(date)" +if [[ $rc == 0 ]]; then + echo "Backup Successful" +else + echo "Backup Failed with status ${rc}" + restic unlock + kill 1 +fi + +end=`date +%s` +echo "Finished check at $(date +"%Y-%m-%d %H:%M:%S") after $((end-start)) seconds" diff --git a/entry.sh b/entry.sh new file mode 100644 index 0000000..e0d7ea2 --- /dev/null +++ b/entry.sh @@ -0,0 +1,29 @@ +#!bin/sh +echo "Starting container ..." +restic snapshots +rc=$? +if [[ $rc != 0 ]]; then + echo "Restic repository '${RESTIC_REPOSITORY}' does not exists. Running restic init." + restic init +fi + +echo "Setup backup cron job with cron expression BACKUP_CRON: ${BACKUP_CRON}" +echo "${BACKUP_CRON} flock -n /tmp/backup.lockfile /bin/backup >> /var/log/cron.log 2>&1" > /var/spool/cron/crontabs/root +echo "Setup check cron job with cron expression CHECK_CRON: ${CHECK_CRON}" +echo "${CHECK_CRON} flock /tmp/backup.lockfile /bin/check >> /var/log/cron.log 2>&1" >> /var/spool/cron/crontabs/root + +cat /var/spool/cron/crontabs/root + +# Make sure the file exists before we start tail +touch /var/log/cron.log + +# start the cron deamon +crond + +echo "Container started." + +if [ -n "${RESTIC_BACKUPONSTART}" ]; then + flock -n /tmp/backup.lockfile /bin/backup +fi + +tail -fn0 /var/log/cron.log