forked from mtusi2026/mtusi2026lab4
36 lines
1.6 KiB
Bash
Executable File
36 lines
1.6 KiB
Bash
Executable File
#!/bin/sh
|
|
# Сверка bucket и БД (только чтение): orphans — объект без строки, missing —
|
|
# ready-строка без объекта. Код выхода 1, если что-то найдено.
|
|
set -eu
|
|
cd "$(dirname "$0")/../.."
|
|
env_file="${COMPOSE_ENV_FILE:-.env}"
|
|
# shellcheck disable=SC1090
|
|
set -a; . "./$env_file"; set +a
|
|
bucket="${S3_BUCKET:-messenger-attachments}"
|
|
compose="docker compose --env-file $env_file"
|
|
|
|
objects=$(mktemp)
|
|
rows=$(mktemp)
|
|
trap 'rm -f "$objects" "$rows"' EXIT
|
|
|
|
$compose run --rm --no-deps -T --entrypoint /bin/sh s3-init -c '
|
|
IFS= read -r u < /run/secrets/minio_root_user
|
|
IFS= read -r p < /run/secrets/minio_root_password
|
|
export MC_CONFIG_DIR=/tmp/.mc MC_HOST_stand="http://$u:$p@s3:9000"
|
|
exec mc ls --recursive "stand/$S3_BUCKET"' 2>/dev/null \
|
|
| sed -n 's/.* \(attachments\/.*\)$/\1/p' | sort > "$objects"
|
|
$compose exec -T postgres psql -U "${POSTGRES_USER:-messenger}" -d "${POSTGRES_DB:-messenger}" -At \
|
|
-c "SELECT object_key, status FROM attachments" | sort > "$rows"
|
|
|
|
echo "bucket: $bucket"
|
|
echo "objects: $(wc -l < "$objects" | tr -d ' ')"
|
|
echo "rows by status:"
|
|
cut -d'|' -f2 "$rows" | sort | uniq -c | sed 's/^ */ /'
|
|
orphans=$(cut -d'|' -f1 "$rows" | sort | comm -13 - "$objects")
|
|
missing=$(grep '|ready$' "$rows" | cut -d'|' -f1 | sort | comm -23 - "$objects")
|
|
echo "orphans: $(printf '%s' "$orphans" | grep -c . || true)"
|
|
[ -z "$orphans" ] || printf '%s\n' "$orphans" | sed 's/^/ orphan /'
|
|
echo "missing: $(printf '%s' "$missing" | grep -c . || true)"
|
|
[ -z "$missing" ] || printf '%s\n' "$missing" | sed 's/^/ missing /'
|
|
[ -z "$orphans$missing" ]
|