diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a6818cc --- /dev/null +++ b/Dockerfile @@ -0,0 +1,31 @@ +FROM gliderlabs/alpine:3.3 +MAINTAINER info@lobaro.com + +RUN echo http://nl.alpinelinux.org/alpine/v3.4/community >> /etc/apk/repositories +RUN apk add --no-cache git go nfs-utils fuse +RUN git clone https://github.com/restic/restic \ + && cd restic \ + && go run build.go \ + && cp restic /usr/local/bin/ +RUN apk del git go + +RUN mkdir /mnt/restic + +ENV RESTIC_REPOSITORY=/mnt/restic +ENV RESTIC_PASSWORD="" +ENV RESTIC_TAG="" +ENV NFS_TARGET="" +# By default backup every 6 hours +ENV BACKUP_CRON="* */6 * * *" + +# /data is the dir where you have to put the data to be backed up +VOLUME /data + +COPY backup.sh /bin/backup +RUN chmod +x /bin/backup + +COPY entry.sh /entry.sh + +RUN touch /var/log/cron.log +ENTRYPOINT ["/entry.sh"] + diff --git a/backup.sh b/backup.sh new file mode 100644 index 0000000..3e0954d --- /dev/null +++ b/backup.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +echo "Starting Backup" + +restic backup /data --tag=${RESTIC_TAG?"Missing environment variable RESTIC_TAG"} >> /var/log/cron.log + + diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..18a27c9 --- /dev/null +++ b/build.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +docker build --rm -t backup-test . diff --git a/entry.sh b/entry.sh new file mode 100755 index 0000000..d558b5d --- /dev/null +++ b/entry.sh @@ -0,0 +1,20 @@ +#!bin/sh +set -e + + +if [ -n "${NFS_TARGET}" ]; then + echo "Mounting NFS based on NFS_TARGET" + mount -o nolock -v ${NFS_TARGET} /mnt/restic +fi + +if [ ! -f "$RESTIC_REPOSITORY/config" ]; then + echo "Restic repository does not exists. Running restic init." + restic init +fi + +echo "Setup backup cron job with cron expression: ${BACKUP_CRON}" +echo "${BACKUP_CRON} /bin/backup >> /var/log/cron.log 2>&1" > /var/spool/cron/crontabs/root + +# start the cron deamon +crond +tail -f /var/log/cron.log diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..e94916c --- /dev/null +++ b/run.sh @@ -0,0 +1,15 @@ +#!/bin/sh + +echo "Removing old container names 'backup-test' if exists" +docker rm -f -v backup-test || true + +echo "Start backup-test container. Backup of ~/test-data/ to repository ~/test-repo/ every minute" +docker run --privileged --name backup-test \ +-e "RESTIC_PASSWORD=test" \ +-e "RESTIC_TAG=test" \ +-e "BACKUP_CRON=* * * * *" \ +-v ~/test-data:/data \ +-v ~/test-repo/:/mnt/restic \ +-t backup-test + + diff --git a/test-data/test.log b/test-data/test.log new file mode 100644 index 0000000..7c40853 --- /dev/null +++ b/test-data/test.log @@ -0,0 +1 @@ +Just a file to test the backup