Skip to content

Commit 31bab96

Browse files
feat: add implementation of paperless s3 backup
1 parent f0d2f40 commit 31bab96

File tree

17 files changed

+182
-419
lines changed

17 files changed

+182
-419
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Helm-related files
2+
*.tgz
3+
*.lock
4+
.chart-releaser.yaml
5+
.release.yaml
6+
.local.values.yaml
Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,6 @@
11
apiVersion: v2
22
name: helm-paperless-s3-backup
33
description: A Helm chart for Kubernetes
4-
5-
# A chart can be either an 'application' or a 'library' chart.
6-
#
7-
# Application charts are a collection of templates that can be packaged into versioned archives
8-
# to be deployed.
9-
#
10-
# Library charts provide useful utilities or functions for the chart developer. They're included as
11-
# a dependency of application charts to inject those utilities and functions into the rendering
12-
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
134
type: application
14-
15-
# This is the chart version. This version number should be incremented each time you make changes
16-
# to the chart and its templates, including the app version.
17-
# Versions are expected to follow Semantic Versioning (https://semver.org/)
18-
version: 0.1.0
19-
20-
# This is the version number of the application being deployed. This version number should be
21-
# incremented each time you make changes to the application. Versions are not expected to
22-
# follow Semantic Versioning. They should reflect the version the application is using.
23-
# It is recommended to use it with quotes.
5+
version: 0.1.1
246
appVersion: "1.16.0"

charts/helm-paperless-s3-backup/templates/NOTES.txt

Lines changed: 0 additions & 22 deletions
This file was deleted.

charts/helm-paperless-s3-backup/templates/_helpers.tpl

Lines changed: 0 additions & 62 deletions
This file was deleted.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: {{ .Release.Name }}-config
5+
data:
6+
backup.sh: |
7+
#!/bin/sh
8+
set -e
9+
10+
# Log functions
11+
log() {
12+
local level="$1"
13+
local message="$2"
14+
echo "[${level}] ${message}"
15+
}
16+
info() { log "INFO" "$1"; }
17+
warn() { log "WARN" "$1"; }
18+
error() { log "ERROR" "$1"; }
19+
20+
# Validate environment variables
21+
: "${S3_SSE_KEY:?$(error 'Environment variable S3_SSE_KEY not set')}"
22+
: "${S3_BUCKET:?$(error 'Environment variable S3_BUCKET not set')}"
23+
: "${S3_ENDPOINT:?$(error 'Environment variable S3_ENDPOINT not set')}"
24+
: "${S3_REGION:?$(error 'Environment variable S3_REGION not set')}"
25+
26+
# Get the paperless pod name & container name
27+
PAPERLESS_POD=$(kubectl get pod -n $PAPERLESS_NAMESPACE -l app=$PAPERLESS_APP -o jsonpath="{.items[0].metadata.name}")
28+
29+
# Set the timestamp and file name
30+
TIMESTAMP=$(date +%Y-%m-%d_%H-%M-%S)
31+
FILE_NAME="paperless-backup-$TIMESTAMP"
32+
33+
# Import the key
34+
S3_SSE_C_PATH="/mnt/backup/.sse-c.key"
35+
echo $S3_SSE_KEY | xxd -r -p > $S3_SSE_C_PATH
36+
37+
info "Creating backup with document exporter..."
38+
kubectl exec ${PAPERLESS_POD} --container $PAPERLESS_CONTAINER_NAME -- \
39+
document_exporter \
40+
--use-folder-prefix \
41+
--zip \
42+
--zip-name ${FILE_NAME} \
43+
../export
44+
45+
info "Copy backup zip to this pod..."
46+
kubectl cp \
47+
--container="${PAPERLESS_CONTAINER_NAME}" \
48+
${PAPERLESS_POD}:../export/${FILE_NAME}.zip \
49+
/mnt/backup/${FILE_NAME}.zip
50+
51+
info "Unzipping backup file..."
52+
7z x /mnt/backup/paperless-backup-*.zip -o/mnt/backup/export/
53+
54+
info "Uploading backup to S3..."
55+
aws s3 sync \
56+
/mnt/backup/export \
57+
s3://$S3_BUCKET \
58+
--endpoint-url $S3_ENDPOINT \
59+
--region $S3_REGION \
60+
--sse-c AES256 \
61+
--sse-c-key fileb://$S3_SSE_C_PATH
62+
63+
info "Cleaning up backup file..."
64+
kubectl exec ${PAPERLESS_POD} --container ${PAPERLESS_CONTAINER_NAME} -- \
65+
bash -c "rm /usr/src/paperless/export/${FILE_NAME}.zip"
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
apiVersion: batch/v1
2+
kind: CronJob
3+
metadata:
4+
name: {{ .Release.Name }}-cronjob
5+
spec:
6+
schedule: "{{ .Values.cron }}"
7+
successfulJobsHistoryLimit: 2
8+
failedJobsHistoryLimit: 2
9+
jobTemplate:
10+
spec:
11+
backoffLimit: 2
12+
template:
13+
metadata:
14+
name: {{ .Release.Name }}
15+
labels:
16+
app: {{ .Release.Name }}
17+
spec:
18+
serviceAccountName: {{ .Release.Name }}
19+
restartPolicy: OnFailure
20+
21+
containers:
22+
- name: paperless-backup
23+
image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
24+
command: ["bash", "-c", "/opt/paperless-s3-backup/backup.sh"]
25+
envFrom:
26+
- secretRef:
27+
name: {{ .Release.Name }}-secret
28+
securityContext:
29+
runAsUser: 34
30+
seccompProfile:
31+
type: RuntimeDefault
32+
allowPrivilegeEscalation: false
33+
runAsNonRoot: true
34+
capabilities:
35+
drop:
36+
- ALL
37+
volumeMounts:
38+
- name: backup-volume
39+
mountPath: /mnt/backup
40+
- name: backup-script
41+
mountPath: /opt/paperless-s3-backup/backup.sh
42+
subPath: backup.sh
43+
44+
volumes:
45+
- name: backup-volume
46+
emptyDir: {}
47+
- name: backup-script
48+
configMap:
49+
name: {{ .Release.Name }}-config
50+
items:
51+
- key: backup.sh
52+
path: backup.sh
53+
defaultMode: 0555

charts/helm-paperless-s3-backup/templates/deployment.yaml

Lines changed: 0 additions & 78 deletions
This file was deleted.

charts/helm-paperless-s3-backup/templates/hpa.yaml

Lines changed: 0 additions & 32 deletions
This file was deleted.

charts/helm-paperless-s3-backup/templates/ingress.yaml

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)