Initial commit

This commit is contained in:
Laurent Le Houerou 2019-06-06 10:37:26 +04:00
commit 4477516204
5 changed files with 119 additions and 0 deletions

30
Dockerfile Normal file
View File

@ -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"]

35
backup.sh Normal file
View File

@ -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"

4
build.sh Normal file
View File

@ -0,0 +1,4 @@
#!/bin/sh
docker build -t llehouerou/restic-backup .
docker push llehouerou/restic-backup

21
check.sh Normal file
View File

@ -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"

29
entry.sh Normal file
View File

@ -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