3 Commits

Author SHA1 Message Date
cd307aeafd docs: example deployment 2023-04-28 21:40:35 +02:00
ad09b6c906 docs: function and env 2023-04-28 21:38:34 +02:00
6bdf45534f feat: migration 2023-04-28 21:37:39 +02:00
6 changed files with 146 additions and 1 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
dockerBuildAndPush.sh

14
BackupSideCar.Dockerfile Normal file
View File

@@ -0,0 +1,14 @@
FROM alpine:3.17
# Setup correct TZ
RUN apk add alpine-conf
RUN /sbin/setup-timezone -z UTC
RUN apk add restic curl
WORKDIR /app
COPY ./src/entry.sh /app/
COPY ./src/backup.sh /app/
CMD [ "/bin/sh", "entry.sh" ]

View File

@@ -1,3 +1,20 @@
# backupsidecar
Backup sidecar that automatically creates backups of one PVC and saves it to another PVC via restic
## Function
A cronjob inside the container runs in the configured interval creating the backup and purging old backups.
A notification is sent to gotify on completion of the backup or on error of either the backup or purge.
## Environment Variables
| ENV Variable | Required |Description| Example Value
|--------------|----------|--------------|-
|INTERVAL|yes|cronjob interval string|15 14 * * *|
|RESTIC_REPOSITORY|yes|path of the restic repository|/mnt/backups/gitea|
|SOURCEDIR |yes|path of the path to backup|/mnt/toBackup/|
|KEEPLAST|yes|number of increments to keep (keep in mind that the number of backups to keep correlates with the interval in which they are created) |10|
|RESTIC_PASSWORD|yes|password for the restic repository|******|
|RUNONSTART|no|set to true to force a backup at the start of the container|true|
|GOTIFYHOST|yes|URL of the gotify server (without trailing slash)|https://gotify.example.com|
|GOTIFYTOKEN|yes|gotify app token|******|
|GOTIFYTOPIC|yes|gotify topic to include in the notification|gotify|

55
example-deployment.yaml Normal file
View File

@@ -0,0 +1,55 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: backup-sidecar
spec:
selector:
matchLabels:
app: backup-sidecar
template:
metadata:
labels:
app: backup-sidecar
spec:
containers:
- name: backup-sidecar
image: gitea.t000-n.de/t.behrendt/backupsidecar:latest
volumeMounts:
- mountPath: /mnt/toBackup
name: data
readOnly: true
- mountPath: /mnt/backups/gitea
name: nfs-backup
subPath: gitea
env:
- name: INTERVAL
value: "15 14 * * *"
- name: RESTIC_REPOSITORY
value: /mnt/backups/gitea
- name: SOURCEDIR
value: "/mnt/toBackup/"
- name: KEEPLAST
value: "100"
- name: RESTIC_PASSWORD
valueFrom:
secretKeyRef:
name: restic-credentials
key: password
- name: RUNONSTART
value: "false"
- name: GOTIFYHOST
value: "https://<gotify-URL>"
- name: GOTIFYTOKEN
valueFrom:
secretKeyRef:
name: gotify-credentials
key: token
- name: GOTIFYTOPIC
value: "gitea"
volumes:
- name: data
persistentVolumeClaim:
claimName: data
- name: nfs-backup
persistentVolumeClaim:
claimName: backup-nfs

40
src/backup.sh Normal file
View File

@@ -0,0 +1,40 @@
#!/bin/sh
GOTIFYURL="$GOTIFYHOST/message?token=$GOTIFYTOKEN"
echo "$(date +"%Y-%m-%dT%T") - Starting backup"
restic backup \
--verbose \
$SOURCEDIR
RESTIC_BACKUP_RETURN=$?
if [ $RESTIC_BACKUP_RETURN -eq 0 ]; then
MSG_BACKUP_SUCCESS="Backup successful"
echo "$(date +"%Y-%m-%dT%T") - $MSG_BACKUP_SUCCESS"
curl -s -X POST "$GOTIFYURL" -F "title=$GOTIFYTOPIC" -F "message=$MSG_BACKUP_SUCCESS"
else
MSG_BACKUP_ERR="Backup failed with error code $RESTIC_BACKUP_RETURN"
echo "$(date +"%Y-%m-%dT%T") - MSG_BACKUP_ERR"
curl -s -X POST "$GOTIFYURL" -F "title=$GOTIFYTOPIC" -F "message=$MSG_BACKUP_ERR"
exit $RESTIC_BACKUP_RETURN
fi
MSG_PURGE_START="$(date +"%Y-%m-%dT%T") - Removing old backups"
echo $MSG_PURGE_START
restic forget --keep-last $KEEPLAST --prune
RESTIC_PURGE_RETURN=$?
if [ $RESTIC_PURGE_RETURN -eq 0 ]; then
echo "$(date +"%Y-%m-%dT%T") - Purge successful"
else
MSG_PURGE_ERR="Purge failed with error code $MSG_PURGE_ERR"
echo "$(date +"%Y-%m-%dT%T") - $MSG_PURGE_ERR"
curl -s -X POST "$GOTIFYURL" -F "title=$GOTIFYTOPIC" -F "message=$MSG_PURGE_ERR"
exit $RESTIC_PURGE_RETURN
fi
echo "$(date +"%Y-%m-%dT%T") - Going back to sleep..."

18
src/entry.sh Normal file
View File

@@ -0,0 +1,18 @@
#!/bin/sh
mkdir /etc/cron.d
touch /etc/cron.d/backup
echo "$INTERVAL /bin/sh /app/backup.sh" > /etc/cron.d/backup
# change ownership and make the cron known to crontab
chmod 0644 /etc/cron.d/backup && crontab /etc/cron.d/backup
if [ $RUNONSTART = 'true' ]; then
echo $(date +"%Y-%m-%dT%T") "- Running initial backup"
/bin/sh /app/backup.sh
fi
# Wait until infinity
echo $(date +"%Y-%m-%dT%T") "- Starting cron"
crond -f