# Backup Web Application Deployment Guide

This guide covers multiple methods to keep the backup web application running perpetually on your server.

## Deployment Options

### 1. 🚀 Systemd Service (Recommended for Production)

**Best for:** Production environments, automatic startup on boot, proper logging, and system integration.

#### Setup Steps:

```bash
# Install the service
sudo ./manage-backup-web-service.sh install

# Start the service
sudo ./manage-backup-web-service.sh start

# Check status
./manage-backup-web-service.sh status

# View logs
./manage-backup-web-service.sh logs
```

#### Service Management:

```bash
# Start/Stop/Restart
sudo systemctl start backup-web-app
sudo systemctl stop backup-web-app
sudo systemctl restart backup-web-app

# Enable/Disable auto-start on boot
sudo systemctl enable backup-web-app
sudo systemctl disable backup-web-app

# Check logs
sudo journalctl -u backup-web-app -f
```

### 2. 🐳 Docker (Recommended for Isolation)

**Best for:** Containerized environments, easy deployment, consistent runtime.

#### Using Docker Compose:

```bash
# Build and start
docker-compose up -d

# View logs
docker-compose logs -f

# Stop
docker-compose down

# Rebuild and restart
docker-compose up -d --build
```

#### Using Docker directly:

```bash
# Build image
docker build -t backup-web-app .

# Run container
docker run -d \
  --name backup-web-app \
  -p 5000:5000 \
  -v /mnt/share/media/backups:/data/backups:ro \
  -e BACKUP_ROOT=/data/backups \
  --restart unless-stopped \
  backup-web-app
```

### 3. 📺 Screen Session (Quick & Simple)

**Best for:** Development, testing, quick deployments.

```bash
# Start the application
./run-backup-web-screen.sh start

# Check status
./run-backup-web-screen.sh status

# View logs (connect to session)
./run-backup-web-screen.sh logs

# Stop the application
./run-backup-web-screen.sh stop
```

### 4. ⚡ Production with Gunicorn

**Best for:** High-performance production deployments.

```bash
# Install gunicorn
pip install gunicorn

# Run with production settings
./run-production.sh
```

## Configuration

### Environment Variables

- `BACKUP_ROOT`: Path to backup directory (default: `/mnt/share/media/backups`)
- `PORT`: Application port (default: `5000`)
- `FLASK_ENV`: Environment mode (`development` or `production`)
- `FLASK_DEBUG`: Enable debug mode (`true` or `false`)

### Security Considerations

1. **Firewall**: Ensure port 5000 is properly secured
2. **Reverse Proxy**: Consider using nginx for SSL termination
3. **Authentication**: Add authentication for production use
4. **File Permissions**: Ensure proper read permissions for backup directories

## Monitoring & Maintenance

### Health Checks

The application provides a health endpoint:
```bash
curl http://localhost:5000/health
```

### Log Locations

- **Systemd**: `sudo journalctl -u backup-web-app`
- **Docker**: `docker-compose logs` or `docker logs backup-web-app`
- **Screen**: Connect to session with `screen -r backup-web-app`
- **Gunicorn**: `/tmp/backup-web-app-access.log` and `/tmp/backup-web-app-error.log`

### Automatic Restarts

- **Systemd**: Built-in restart on failure
- **Docker**: Use `--restart unless-stopped` or `restart: unless-stopped` in compose
- **Screen**: Manual restart required

## Troubleshooting

### Common Issues

1. **Port already in use**:
   ```bash
   sudo lsof -i :5000
   sudo netstat -tulpn | grep :5000
   ```

2. **Permission denied for backup directory**:
   ```bash
   sudo chown -R acedanger:acedanger /mnt/share/media/backups
   chmod -R 755 /mnt/share/media/backups
   ```

3. **Service won't start**:
   ```bash
   sudo journalctl -u backup-web-app -n 50
   ```

### Performance Tuning

1. **Gunicorn Workers**: Adjust in `gunicorn.conf.py`
2. **Memory Limits**: Set in systemd service or docker-compose
3. **Log Rotation**: Configure logrotate for production

## Quick Start Commands

```bash
# For development/testing (Screen)
./run-backup-web-screen.sh start

# For production (Systemd)
sudo ./manage-backup-web-service.sh install
sudo ./manage-backup-web-service.sh start

# For containerized (Docker)
docker-compose up -d

# Check if running
curl http://localhost:5000/health
```

## Recommended Setup

For a production server, use this combination:

1. **Primary**: Systemd service for reliability
2. **Backup**: Docker setup for easy maintenance
3. **Monitoring**: Set up log monitoring and alerts
4. **Security**: Add reverse proxy with SSL

Choose the method that best fits your infrastructure and requirements!
