diff --git a/README.md b/README.md index 67878c9..f45f7de 100644 --- a/README.md +++ b/README.md @@ -16,32 +16,28 @@ gzip < wiki.tar > wiki.tgz ## Restore -1. Create `restore.yaml` +1. Create `restore.db.yaml` ```yaml apiVersion: batch/v1 kind: Job metadata: - name: restorejob + name: restorejobdb namespace: wiki spec: template: spec: containers: - - name: restorejob + - name: restorejobdb image: lscr.io/linuxserver/mariadb command: - /bin/bash - - /restore.sh + - /restore.db.sh volumeMounts: - name: backup-script - mountPath: /restore.sh - subPath: restore.sh + mountPath: /restore.db.sh + subPath: restore.db.sh readOnly: true - - name: config-vol - mountPath: /config - - name: image-uploads-vol - mountPath: /app/www/public/img env: - name: RESTORE_S3_URL value: "s3://.../backups/wiki.tgz" @@ -81,8 +77,57 @@ spec: configMap: name: backup-script items: - - key: restore.sh - path: restore.sh + - key: restore.db.sh + path: restore.db.sh +``` + +2. Create `restore.files.yaml` + +```yaml +apiVersion: batch/v1 +kind: Job +metadata: + name: restorejobfiles + namespace: wiki +spec: + template: + spec: + containers: + - name: restorejobfiles + image: lscr.io/linuxserver/bookstack:v23.02.2-ls71 + command: + - /bin/bash + - /restore.files.sh + volumeMounts: + - name: backup-script + mountPath: /restore.files.sh + subPath: restore.files.sh + readOnly: true + - name: config-vol + mountPath: /config + - name: image-uploads-vol + mountPath: /app/www/public/img + env: + - name: RESTORE_S3_URL + value: "s3://.../backups/wiki.tgz" + - name: AWS_ACCESS_KEY_ID + valueFrom: + secretKeyRef: + name: wiki-secrets + key: access-key-id + - name: AWS_SECRET_ACCESS_KEY + valueFrom: + secretKeyRef: + name: wiki-secrets + key: secret-access-key + restartPolicy: OnFailure + volumes: + - name: backup-script + configMap: + name: backup-script + items: + - key: restore.files.sh + path: restore.files.sh - name: config-vol persistentVolumeClaim: claimName: wikiconfig @@ -91,14 +136,19 @@ spec: claimName: wikiimages ``` -2. Run the restore job +2. Run the restore jobs ``` kubectl scale --replicas=0 deployment.apps/wiki-bookstack-helm-bookstack -n wiki -kubectl apply -f restore.yaml +kubectl apply -f restore.files.yaml # wait for it to complete kubectl get all -n wiki kubectl scale --replicas=1 deployment.apps/wiki-bookstack-helm-bookstack -n wiki + +kubectl apply -f restore.db.yaml +# wait for it to complete +kubectl get all -n wiki +kubectl scale --replicas=1 deployment.apps/wiki-bookstack-helm-db -n wiki ``` 3. If the url is changing, you may need the following: diff --git a/bookstack-helm/templates/backup.yaml b/bookstack-helm/templates/backup.yaml index 0e6a641..6a79029 100644 --- a/bookstack-helm/templates/backup.yaml +++ b/bookstack-helm/templates/backup.yaml @@ -1,7 +1,7 @@ apiVersion: batch/v1 kind: CronJob metadata: - name: backupjob + name: backupjob-db namespace: {{ .Values.meshwiki_app_namespace }} spec: schedule: {{ .Values.bookstack.backup.cron_schedule | quote }} @@ -11,16 +11,16 @@ spec: spec: restartPolicy: OnFailure containers: - - name: backupjob + - name: backupjob-db image: "{{ .Values.db_image.repository }}:{{ .Values.db_image.tag }}" imagePullPolicy: {{ .Values.db_image.pullPolicy }} command: - /bin/bash - - /backup.sh + - /backup.db.sh volumeMounts: - name: backup-script - mountPath: /backup.sh - subPath: backup.sh + mountPath: /backup.db.sh + subPath: backup.db.sh readOnly: true env: - name: DB_HOST @@ -63,5 +63,5 @@ spec: configMap: name: backup-script items: - - key: backup.sh - path: backup.sh + - key: backup.db.sh + path: backup.db.sh diff --git a/bookstack-helm/templates/backupfiles.yaml b/bookstack-helm/templates/backupfiles.yaml new file mode 100644 index 0000000..77d3ae6 --- /dev/null +++ b/bookstack-helm/templates/backupfiles.yaml @@ -0,0 +1,47 @@ +apiVersion: batch/v1 +kind: CronJob +metadata: + name: backupjob-files + namespace: {{ .Values.meshwiki_app_namespace }} +spec: + schedule: {{ .Values.bookstack.backup.cron_schedule | quote }} + jobTemplate: + spec: + template: + spec: + restartPolicy: OnFailure + containers: + - name: backupjob-files + image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + command: + - /bin/bash + - /backup.files.sh + volumeMounts: + - name: backup-script + mountPath: /backup.files.sh + subPath: backup.files.sh + readOnly: true + env: + - name: BACKUP_S3_URL + valueFrom: + secretKeyRef: + name: wiki-secrets + key: backup-s3-url + - name: AWS_ACCESS_KEY_ID + valueFrom: + secretKeyRef: + name: wiki-secrets + key: access-key-id + - name: AWS_SECRET_ACCESS_KEY + valueFrom: + secretKeyRef: + name: wiki-secrets + key: secret-access-key + volumes: + - name: backup-script + configMap: + name: backup-script + items: + - key: backup.files.sh + path: backup.files.sh diff --git a/bookstack-helm/templates/backupscript.yaml b/bookstack-helm/templates/backupscript.yaml index be0044d..58009ec 100644 --- a/bookstack-helm/templates/backupscript.yaml +++ b/bookstack-helm/templates/backupscript.yaml @@ -4,17 +4,25 @@ metadata: name: backup-script namespace: {{ .Values.meshwiki_app_namespace }} data: - backup.sh: | + backup.db.sh: | echo "Dumping db" mysqldump -h "$DB_HOST" -u "$DB_USER" --password="$DB_PASS" "$DB_DATABASE" > wiki.sql echo "Creating tarball" - backup_name="wiki_backup_$(date +%s).tar.gz" - tar -chzvf $backup_name wiki.sql /app/www/public/uploads/ /app/www/storage/uploads/ /app/www/public/img/ + backup_name="wiki_backup_db_$(date +%s).tar.gz" + tar -chzvf $backup_name wiki.sql echo "push to s3" apk add aws-cli aws s3 cp ./$backup_name "$BACKUP_S3_URL" echo "done" - restore.sh: | + backup.files.sh: | + echo "Creating tarball" + backup_name="wiki_backup_files_$(date +%s).tar.gz" + tar -chzvf $backup_name /app/www/public/uploads/ /app/www/storage/uploads/ /app/www/public/img/ + echo "push to s3" + apk add aws-cli + aws s3 cp ./$backup_name "$BACKUP_S3_URL" + echo "done" + restore.db.sh: | echo "Checking if restore is needed" echo "Restore file is: $RESTORE_S3_URL" if [ ! -z "$RESTORE_S3_URL" ]; then @@ -23,9 +31,21 @@ data: aws s3 cp "$RESTORE_S3_URL" /tmp/restore.tgz echo "downloaded tarball" tar -xzf /tmp/restore.tgz -C / - echo "Files restored" mysql -h "$DB_HOST" -u "$DB_USER" --password="$DB_PASS" $DB_DATABASE < /wiki.sql else echo "Not restoring" fi echo "done" + restore.files.sh: | + echo "Checking if restore is needed" + echo "Restore file is: $RESTORE_S3_URL" + if [ ! -z "$RESTORE_S3_URL" ]; then + echo "Restoring from $RESTORE_S3_URL" + apk add aws-cli + aws s3 cp "$RESTORE_S3_URL" /tmp/restore.tgz + echo "downloaded tarball" + tar -xzf /tmp/restore.tgz -C / + else + echo "Not restoring" + fi + echo "done"