stack.basicstack.de/apps/directus/directus-backup-cronjob.yaml

88 lines
2.6 KiB
YAML
Raw Permalink Normal View History

apiVersion: batch/v1
kind: CronJob
metadata:
name: directus-backup
namespace: directus
labels:
app: directus
component: backup
spec:
schedule: "0 2 * * *"
successfulJobsHistoryLimit: 3
failedJobsHistoryLimit: 3
jobTemplate:
spec:
template:
metadata:
labels:
app: directus
component: backup
spec:
restartPolicy: OnFailure
containers:
- name: backup
image: postgres:16-alpine
command:
- /bin/sh
- -c
- |
set -e
BACKUP_DIR="/backups/directus"
BACKUP_FILE="${BACKUP_DIR}/directus-backup-$(date +%Y%m%d-%H%M%S).sql.gz"
echo "Starting Directus database backup to $BACKUP_FILE"
# Create backup directory if it doesn't exist
mkdir -p "$BACKUP_DIR"
# Perform PostgreSQL dump and compress
PGPASSWORD="$POSTGRES_PASSWORD" pg_dump \
-h directus-postgres \
-U directus \
-d directus \
--clean \
--if-exists \
--no-owner \
--no-privileges \
| gzip > "$BACKUP_FILE"
# Verify backup was created
if [ -f "$BACKUP_FILE" ]; then
SIZE=$(du -h "$BACKUP_FILE" | cut -f1)
echo "Backup completed successfully: $BACKUP_FILE ($SIZE)"
ls -lh "$BACKUP_FILE"
else
echo "ERROR: Backup file was not created"
exit 1
fi
# Clean up backups older than 30 days
echo "Cleaning up backups older than 30 days..."
find "$BACKUP_DIR" -name "directus-backup-*.sql.gz" -type f -mtime +30 -delete
# Show remaining backups
echo "Current backups:"
ls -lh "$BACKUP_DIR" | grep "directus-backup-" || echo "No backups found"
echo "Backup job completed"
env:
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: directus-db-credentials
key: postgres-password
resources:
requests:
cpu: 100m
memory: 256Mi
limits:
cpu: 500m
memory: 512Mi
volumeMounts:
- name: backup-storage
mountPath: /backups
volumes:
- name: backup-storage
hostPath:
path: /var/backups
type: DirectoryOrCreate