Skip to content
This repository was archived by the owner on Sep 3, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ ENV RESTIC_TAG=latest
RUN apk add --update --no-cache ca-certificates fuse openssh-client bash tzdata
WORKDIR /root
COPY --from=build /usr/local/src/restic/restic /usr/local/bin
COPY entrypoint.sh backup.sh prune.sh forget.sh ./
COPY entrypoint.sh backup.sh do_backup.sh prune.sh forget.sh ./
ENV PATH="./:${PATH}"
RUN chmod a+x ./*.sh
RUN mkdir -p /var/spool/cron/crontabs \
Expand Down
50 changes: 4 additions & 46 deletions backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,9 @@ log() {
echo "[$(date +"%Y-%m-%d %H:%M:%S")]$1" | tee -a "$logFile"
}

showTime() {
num=$1
min=0
hour=0
day=0
if((num>59));then
((sec=num%60))
((num=num/60))
if((num>59));then
((min=num%60))
((num=num/60))
if((num>23));then
((hour=num%24))
((day=num/24))
else
((hour=num))
fi
else
((min=num))
fi
else
((sec=num))
fi
time="${day}d ${hour}h ${min}m ${sec}s"
log "[INFO] Total backup time: ${time}"
}

start=$(date +%s)
log "[INFO] Starting backup"
log "[INFO] Log filename: ${logFile}"

if [ -n "${RESTIC_BACKUP_ARGS}" ]; then
log "[INFO] RESTIC_BACKUP_ARGS: ${RESTIC_BACKUP_ARGS}"
fi

restic backup /data ${RESTIC_BACKUP_ARGS} --tag="${RESTIC_TAG}" | tee -a "$logFile"
flock -xn /root/restic.lock /root/do_backup.sh
rc=$?
if [[ $rc == 0 ]]; then
log "[INFO] Backup succeeded"
else
log "[ERROR] Backup failed with status ${rc}"
restic unlock
copyErrorLog
kill 1
if [[ $rc != 0 ]]; then
log "[INFO] Backup already running. Exiting"
fi
end=$(date +%s)
log "[INFO] Finished backup at $(date)"
showTime $((end-start))

58 changes: 58 additions & 0 deletions do_backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/bin/bash
set -e
# credit: some of the code for this script inspired by https://github.com/lobaro/restic-backup-docker

logFile="/var/log/restic/backup/$(date +"%Y-%m-%d-%H-%M-%S").log"

log() {
echo "[$(date +"%Y-%m-%d %H:%M:%S")]$1" | tee -a "$logFile"
}

showTime() {
num=$1
min=0
hour=0
day=0
if((num>59));then
((sec=num%60))
((num=num/60))
if((num>59));then
((min=num%60))
((num=num/60))
if((num>23));then
((hour=num%24))
((day=num/24))
else
((hour=num))
fi
else
((min=num))
fi
else
((sec=num))
fi
time="${day}d ${hour}h ${min}m ${sec}s"
log "[INFO] Total backup time: ${time}"
}

start=$(date +%s)
log "[INFO] Starting backup"
log "[INFO] Log filename: ${logFile}"

if [ -n "${RESTIC_BACKUP_ARGS}" ]; then
log "[INFO] RESTIC_BACKUP_ARGS: ${RESTIC_BACKUP_ARGS}"
fi

restic backup /data ${RESTIC_BACKUP_ARGS} --tag="${RESTIC_TAG}" | tee -a "$logFile"
rc=$?
if [[ $rc == 0 ]]; then
log "[INFO] Backup succeeded"
else
log "[ERROR] Backup failed with status ${rc}"
restic unlock
copyErrorLog
kill 1
fi
end=$(date +%s)
log "[INFO] Finished backup at $(date)"
showTime $((end-start))