1+ apiVersion : v1
2+ kind : ConfigMap
3+ metadata :
4+ name : {{ .Release.Name }}-config
5+ namespace : {{ .Release.Namespace }}
6+ data :
7+ backup.sh : |
8+ #!/bin/sh
9+ set -e
10+
11+ # Log functions
12+ log() {
13+ local level="$1"
14+ local message="$2"
15+ echo "[${level}] ${message}"
16+ }
17+
18+ info() { log "INFO" "$1"; }
19+ warn() { log "WARN" "$1"; }
20+ error() { log "ERROR" "$1"; }
21+
22+ # Validate environment variables
23+ : "${S3_BUCKET:?$(error 'Environment variable S3_BUCKET not set')}"
24+ : "${S3_ENDPOINT:?$(error 'Environment variable S3_ENDPOINT not set')}"
25+ : "${S3_REGION:?$(error 'Environment variable S3_REGION not set')}"
26+
27+ # Get the bookstack pod name & container name
28+ BOOKSTACK_POD=$(kubectl get pod -n $BOOKSTACK_NAMESPACE -l app=$BOOKSTACK_APP -o jsonpath="{.items[0].metadata.name}")
29+
30+ # Set the timestamp and file name
31+ TIMESTAMP=$(date +%Y-%m-%d_%H-%M-%S)
32+ FILE_NAME="bookstack-backup-$TIMESTAMP"
33+
34+ # Import the key if S3_SSE_KEY is set
35+ if [ -n "$S3_SSE_KEY" ]; then
36+ S3_SSE_C_PATH="/mnt/backup/.sse-c.key"
37+ echo "$S3_SSE_KEY" | xxd -r -p > "$S3_SSE_C_PATH"
38+ fi
39+
40+
41+ #sleep infinity
42+
43+ info "Creating backup with document exporter..."
44+ kubectl exec ${BOOKSTACK_POD} --container $BOOKSTACK_CONTAINER_NAME -- \
45+ sh -c 'mysqldump --skip-ssl -h $DB_HOST -u $DB_USERNAME -p$DB_PASSWORD $DB_DATABASE > /tmp/'${FILE_NAME}'.sql'
46+
47+ kubectl exec ${BOOKSTACK_POD} --container $BOOKSTACK_CONTAINER_NAME -- \
48+ sh -c 'cd /app/www/ && tar --dereference -czvf /tmp/bookstack-files-backup.tar.gz public/uploads storage/uploads themes'
49+
50+ info "Copying sql backup file to this pod..."
51+ kubectl cp \
52+ --container="${BOOKSTACK_CONTAINER_NAME}" \
53+ ${BOOKSTACK_POD}:/tmp/${FILE_NAME}.sql \
54+ /mnt/backup/bookstack-backup-db.sql
55+
56+ info "Copying bookstack folder backup file to the host..."
57+ kubectl cp \
58+ --container="${BOOKSTACK_CONTAINER_NAME}" \
59+ ${BOOKSTACK_POD}:/tmp/bookstack-files-backup.tar.gz \
60+ /mnt/backup/bookstack-files-backup.tar.gz
61+
62+ info "Unzipping backup file..."
63+ tar -xzvf /mnt/backup/bookstack-files-backup.tar.gz -C /mnt/backup/
64+
65+ info "Removing tar file local..."
66+ rm /mnt/backup/bookstack-files-backup.tar.gz
67+
68+ info "Cleaning up backup files remote..."
69+ kubectl exec ${BOOKSTACK_POD} --container ${BOOKSTACK_CONTAINER_NAME} -- \
70+ bash -c "rm /tmp/${FILE_NAME}.sql && \
71+ rm /tmp/bookstack-files-backup.tar.gz"
72+
73+ info "Uploading backup to S3..."
74+ CMD="aws s3 sync /mnt/backup s3://$S3_BUCKET --endpoint-url $S3_ENDPOINT --region $S3_REGION --exclude '.sse-c.key'"
75+
76+ if [ -n "$S3_SSE_KEY" ]; then
77+ CMD="$CMD --sse-c AES256 --sse-c-key fileb://$S3_SSE_C_PATH"
78+ info "Using server-side encryption with customer-provided key"
79+ fi
80+
81+ eval "$CMD"
0 commit comments