#!/bin/sh set -e BACKUP_DIR="/backup" RETAIN_DAYS="${BACKUP_RETAIN_DAYS:-7}" TIMESTAMP="$(date +%Y-%m-%d_%H%M)" BACKUP_FILE="gitea-dump-${TIMESTAMP}.zip" echo "========================================" echo "Gitea backup started at $(date)" echo "========================================" # Pre-flight check if ! docker inspect gitea >/dev/null 2>&1; then echo "ERROR: gitea container not found or not running." exit 1 fi # Ensure backup directory exists inside gitea container docker exec -u git gitea sh -c "mkdir -p /tmp/backup" # Run gitea dump echo "Running gitea dump..." docker exec -u git -w /tmp gitea \ /app/gitea/gitea dump \ -c /data/gitea/conf/app.ini \ --file "/tmp/backup/${BACKUP_FILE}" \ --skip-log \ --type zip # Verify backup was created if [ ! -f "${BACKUP_DIR}/${BACKUP_FILE}" ]; then echo "ERROR: Backup file not found at ${BACKUP_DIR}/${BACKUP_FILE}" exit 1 fi FILESIZE=$(stat -c%s "${BACKUP_DIR}/${BACKUP_FILE}" 2>/dev/null || stat -f%z "${BACKUP_DIR}/${BACKUP_FILE}" 2>/dev/null) echo "Backup created: ${BACKUP_FILE} (${FILESIZE} bytes)" # Retention: delete backups older than N days echo "Cleaning up backups older than ${RETAIN_DAYS} days..." DELETED=$(find "${BACKUP_DIR}" -name "gitea-dump-*.zip" -mtime "+${RETAIN_DAYS}" -print -delete) if [ -n "${DELETED}" ]; then echo "Deleted old backups:" echo "${DELETED}" else echo "No old backups to delete." fi echo "" echo "Current backups:" ls -lh "${BACKUP_DIR}"/gitea-dump-*.zip 2>/dev/null || echo "(none)" echo "" echo "========================================" echo "Gitea backup completed at $(date)" echo "========================================"