Files
telegram-helper-bot/scripts/migrate_from_systemctl.sh
Andrey 8f338196b7 Refactor Docker and configuration files for improved structure and functionality
- Updated `.dockerignore` to include additional development and temporary files, enhancing build efficiency.
- Modified `.gitignore` to remove unnecessary entries and streamline ignored files.
- Enhanced `docker-compose.yml` with health checks, resource limits, and improved environment variable handling for better service management.
- Refactored `Dockerfile.bot` to utilize a multi-stage build for optimized image size and security.
- Improved `Makefile` with new commands for deployment, migration, and backup, along with enhanced help documentation.
- Updated `requirements.txt` to include new dependencies for environment variable management.
- Refactored metrics handling in the bot to ensure proper initialization and collection.
2025-08-29 23:15:06 +03:00

105 lines
3.0 KiB
Bash

#!/bin/bash
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
echo -e "${GREEN}🔄 Starting migration from systemctl + cron to Docker${NC}"
# Check if running as root
if [ "$EUID" -ne 0 ]; then
echo -e "${RED}❌ This script must be run as root for systemctl operations${NC}"
exit 1
fi
# Configuration
SERVICE_NAME="telegram-helper-bot"
CRON_USER="root"
echo -e "${YELLOW}📋 Migration steps:${NC}"
echo "1. Stop systemctl service"
echo "2. Disable systemctl service"
echo "3. Remove cron jobs"
echo "4. Backup existing data"
echo "5. Deploy Docker version"
# Step 1: Stop systemctl service
echo -e "${YELLOW}🛑 Stopping systemctl service...${NC}"
if systemctl is-active --quiet "$SERVICE_NAME"; then
systemctl stop "$SERVICE_NAME"
echo -e "${GREEN}✅ Service stopped${NC}"
else
echo -e "${YELLOW}⚠️ Service was not running${NC}"
fi
# Step 2: Disable systemctl service
echo -e "${YELLOW}🚫 Disabling systemctl service...${NC}"
if systemctl is-enabled --quiet "$SERVICE_NAME"; then
systemctl disable "$SERVICE_NAME"
echo -e "${GREEN}✅ Service disabled${NC}"
else
echo -e "${YELLOW}⚠️ Service was not enabled${NC}"
fi
# Step 3: Remove cron jobs
echo -e "${YELLOW}🗑️ Removing cron jobs...${NC}"
if crontab -u "$CRON_USER" -l 2>/dev/null | grep -q "telegram-helper-bot"; then
crontab -u "$CRON_USER" -l 2>/dev/null | grep -v "telegram-helper-bot" | crontab -u "$CRON_USER" -
echo -e "${GREEN}✅ Cron jobs removed${NC}"
else
echo -e "${YELLOW}⚠️ No cron jobs found${NC}"
fi
# Step 4: Backup existing data
echo -e "${YELLOW}💾 Creating backup...${NC}"
BACKUP_DIR="/backup/telegram-bot-$(date +%Y%m%d-%H%M%S)"
mkdir -p "$BACKUP_DIR"
# Backup database
if [ -f "database/tg-bot-database.db" ]; then
cp -r database "$BACKUP_DIR/"
echo -e "${GREEN}✅ Database backed up to $BACKUP_DIR/database${NC}"
fi
# Backup logs
if [ -d "logs" ]; then
cp -r logs "$BACKUP_DIR/"
echo -e "${GREEN}✅ Logs backed up to $BACKUP_DIR/logs${NC}"
fi
# Backup settings
if [ -f ".env" ]; then
cp .env "$BACKUP_DIR/"
echo -e "${GREEN}✅ Settings backed up to $BACKUP_DIR/.env${NC}"
fi
# Step 5: Deploy Docker version
echo -e "${YELLOW}🐳 Deploying Docker version...${NC}"
# Check if Docker is installed
if ! command -v docker &> /dev/null; then
echo -e "${RED}❌ Docker is not installed. Please install Docker first.${NC}"
exit 1
fi
if ! command -v docker-compose &> /dev/null; then
echo -e "${RED}❌ Docker Compose is not installed. Please install Docker Compose first.${NC}"
exit 1
fi
# Make deploy script executable and run it
chmod +x scripts/deploy.sh
./scripts/deploy.sh
echo -e "${GREEN}✅ Migration completed successfully!${NC}"
echo -e "${GREEN}📁 Backup location: $BACKUP_DIR${NC}"
echo -e "${YELLOW}📝 Next steps:${NC}"
echo "1. Verify the bot is working correctly"
echo "2. Check monitoring dashboards"
echo "3. Remove old systemctl service file if no longer needed"
echo "4. Update any external monitoring/alerting systems"