Files
my-git-server/scripts/setup-git-ssh.sh
2025-11-26 22:04:28 +01:00

127 lines
3.3 KiB
Bash

#!/bin/bash
# SSH Setup for git.etoth.dev
# Usage: bash setup-git-ssh.sh
set -e
# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
echo ""
echo "========================================"
echo " SSH Setup for git.etoth.dev"
echo "========================================"
echo ""
# Create SSH directory
SSH_DIR="$HOME/.ssh"
SSH_CONFIG="$SSH_DIR/config"
if [ ! -d "$SSH_DIR" ]; then
echo -e "${BLUE}→ Creating SSH directory...${NC}"
mkdir -p "$SSH_DIR"
chmod 700 "$SSH_DIR"
echo -e "${GREEN}✓ SSH directory created${NC}"
else
echo -e "${GREEN}✓ SSH directory exists${NC}"
fi
# Create backup
if [ -f "$SSH_CONFIG" ]; then
BACKUP_FILE="$SSH_CONFIG.backup.$(date +%Y%m%d_%H%M%S)"
cp "$SSH_CONFIG" "$BACKUP_FILE"
echo -e "${GREEN}✓ Backup created: $BACKUP_FILE${NC}"
fi
# Check if already configured
if grep -q "^Host git.etoth.dev" "$SSH_CONFIG" 2>/dev/null; then
echo ""
echo -e "${YELLOW}⚠ git.etoth.dev is already configured!${NC}"
echo ""
read -p "Do you want to overwrite the configuration? (y/n): " OVERWRITE
if [ "$OVERWRITE" != "y" ] && [ "$OVERWRITE" != "Y" ]; then
echo -e "${BLUE}→ Setup cancelled.${NC}"
exit 0
fi
# Remove old configuration
sed -i.bak '/^# Gitea Server git.etoth.dev/,/^$/d' "$SSH_CONFIG" 2>/dev/null || \
sed -i '' '/^# Gitea Server git.etoth.dev/,/^$/d' "$SSH_CONFIG" 2>/dev/null || true
echo -e "${GREEN}✓ Old configuration removed${NC}"
fi
# Add new configuration
echo -e "${BLUE}→ Adding SSH configuration...${NC}"
cat >> "$SSH_CONFIG" << 'EOF'
# Gitea Server git.etoth.dev
Host git.etoth.dev
HostName git.etoth.dev
User git
Port 222
StrictHostKeyChecking accept-new
EOF
chmod 600 "$SSH_CONFIG"
echo -e "${GREEN}✓ SSH configuration added!${NC}"
echo ""
# Check for SSH key
if [ ! -f "$SSH_DIR/id_ed25519" ] && [ ! -f "$SSH_DIR/id_rsa" ]; then
echo -e "${YELLOW}⚠ No SSH key found!${NC}"
echo ""
read -p "Do you want to create a new SSH key? (y/n): " CREATE_KEY
if [ "$CREATE_KEY" == "y" ] || [ "$CREATE_KEY" == "Y" ]; then
echo ""
read -p "Your email address: " EMAIL
ssh-keygen -t ed25519 -C "$EMAIL" -f "$SSH_DIR/id_ed25519"
echo ""
echo -e "${GREEN}✓ SSH key created!${NC}"
echo ""
echo -e "${BLUE}Your public key:${NC}"
echo "----------------------------------------"
cat "$SSH_DIR/id_ed25519.pub"
echo "----------------------------------------"
echo ""
echo "Add this key to Gitea:"
echo " Settings → SSH/GPG Keys → Add Key"
echo ""
fi
fi
# Test connection
echo ""
read -p "Do you want to test the connection? (y/n): " TEST
if [ "$TEST" == "y" ] || [ "$TEST" == "Y" ]; then
echo ""
echo -e "${BLUE}→ Testing connection to git.etoth.dev...${NC}"
echo ""
ssh -T git@git.etoth.dev 2>&1 || true
echo ""
fi
echo ""
echo -e "${GREEN}========================================"
echo " Setup completed successfully!"
echo "========================================${NC}"
echo ""
echo "You can now use Git:"
echo ""
echo " git clone git@git.etoth.dev:user/repo.git"
echo " git push"
echo " git pull"
echo ""
echo "More information:"
echo " Show SSH config: cat ~/.ssh/config"
echo " Edit SSH config: nano ~/.ssh/config"
echo ""