Files
scripts/gitea/delete-users-by-creation-date.sh
Dobromir Popov b92a02a770 fix gitea db
2026-02-13 11:26:47 +02:00

55 lines
2.0 KiB
Bash

#!/usr/bin/env bash
# Mark Gitea users for deletion by creation date (e.g. after a hack/spam wave).
# Users created ON or AFTER the cutoff date will be marked inactive; then run
# Gitea admin task "Delete all unactivated accounts" to remove them.
#
# Usage:
# ./delete-users-by-creation-date.sh 2025-02-01
# ./delete-users-by-creation-date.sh "2025-02-10 14:00"
# ./delete-users-by-creation-date.sh 2025-02-01 --whitelist "admin,myuser"
#
# Steps after running this script:
# 1. Backup your Gitea database.
# 2. Run the generated SQL against your DB (PostgreSQL or SQLite).
# 3. In Gitea: Site Administration -> Maintenance -> "Delete all unactivated accounts".
# 4. Consider DISABLE_REGISTRATION = true in app.ini if registration was abused.
set -e
CUTOFF_DATE="${1:?Usage: $0 YYYY-MM-DD [--whitelist user1,user2]}"
WHITELIST=""
if [[ "$2" == "--whitelist" && -n "$3" ]]; then
WHITELIST="$3"
fi
# Convert cutoff to Unix timestamp (GNU date on Linux)
CUTOFF_UNIX=$(date -d "$CUTOFF_DATE" +%s 2>/dev/null) || true
if [[ -z "$CUTOFF_UNIX" ]]; then
echo "Invalid date: $CUTOFF_DATE (use YYYY-MM-DD or \"YYYY-MM-DD HH:MM\")"
echo "Run this script on the Gitea server (Linux) or pass the Unix timestamp manually."
exit 1
fi
echo "# Cutoff: $CUTOFF_DATE -> Unix $CUTOFF_UNIX (users created on or after this will be marked inactive)"
echo "# Backup your database before running any of the below."
echo ""
if [[ -n "$WHITELIST" ]]; then
# Build NOT IN list for SQL
WL=$(echo "$WHITELIST" | sed "s/,/','/g" | sed "s/^/'/; s/$/'/")
WHERE_EXTRA=" AND name NOT IN ($WL)"
WHERE_EXTRA_PG=" AND name NOT IN ($WL)"
else
WHERE_EXTRA=""
WHERE_EXTRA_PG=""
fi
echo "# --- PostgreSQL (table: public.user) ---"
echo "UPDATE public.user SET is_active = false WHERE created_unix >= $CUTOFF_UNIX${WHERE_EXTRA_PG};"
echo ""
echo "# --- SQLite / MySQL (table: user) ---"
echo "UPDATE user SET is_active = 0 WHERE created_unix >= $CUTOFF_UNIX${WHERE_EXTRA};"
echo ""
echo "# Then in Gitea: Site Administration -> Maintenance -> 'Delete all unactivated accounts'"