Files
BrainKP/scripts/backup-git-repos.sh

67 lines
1.9 KiB
Bash
Executable File

#!/bin/bash
# Backup individual de cada repo Gitea via mirror clone
set -e
git config --global --add safe.directory '*' 2>/dev/null
DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_DIR="/tmp/backup-git-repos"
DEST="gdrive:Git/repos"
GITEA_DATA="/var/lib/docker/volumes/yccsckck4g004gosccwc4kg4_gitea-data/_data/git/repositories"
echo "[$(date)] Backup repos Git started"
rm -rf "$BACKUP_DIR"
mkdir -p "$BACKUP_DIR"
# Find all bare repos (*.git directories)
REPOS=$(find "$GITEA_DATA" -maxdepth 2 -name "*.git" -type d 2>/dev/null)
COUNT=0
SKIPPED=0
for REPO_PATH in $REPOS; do
# Extract owner and repo name from path
REL_PATH="${REPO_PATH#$GITEA_DATA/}"
OWNER=$(dirname "$REL_PATH")
REPO_NAME=$(basename "$REPO_PATH" .git)
# Skip non-repo directories (keys, lfs, etc)
if [[ "$OWNER" == "." ]] || [[ "$OWNER" == "keys" ]] || [[ "$OWNER" == "lfs" ]]; then
continue
fi
# Check if repo has any commits (non-empty)
COMMITS=$(git -C "$REPO_PATH" rev-list --count HEAD 2>/dev/null || echo "0")
if [[ "$COMMITS" == "0" ]]; then
echo "Skipping empty: $OWNER/$REPO_NAME"
SKIPPED=$((SKIPPED + 1))
continue
fi
# Create directory structure
mkdir -p "$BACKUP_DIR/$OWNER"
# Mirror clone to a .git file
echo "Backing up: $OWNER/$REPO_NAME ($COMMITS commits)"
git clone --mirror "$REPO_PATH" "$BACKUP_DIR/$OWNER/${REPO_NAME}.git" 2>/dev/null
# Compress
tar -czf "$BACKUP_DIR/$OWNER/${REPO_NAME}.git.tar.gz" -C "$BACKUP_DIR/$OWNER" "${REPO_NAME}.git" 2>/dev/null
rm -rf "$BACKUP_DIR/$OWNER/${REPO_NAME}.git"
COUNT=$((COUNT + 1))
done
echo "Backed up $COUNT repos ($SKIPPED empty skipped)"
# Sync to Google Drive
rclone copy "$BACKUP_DIR" "$DEST" --progress
# Keep only last 7 days in GDrive
rclone delete "$DEST" --min-age 7d
# Cleanup local
rm -rf "$BACKUP_DIR"
echo "[$(date)] Backup repos Git finished"