125 lines
3.0 KiB
Bash
Executable File

#!/bin/bash
# Paperless Service Backup Script
# Uses centralized configuration via backup.env
set -e
# Load template configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
# Set service name explicitly for template
export SERVICE_NAME="paperless"
source "$PROJECT_ROOT/backup/service-script-template.sh"
# Setup logging and cleanup
setup_logging
trap cleanup_on_exit EXIT
# Service-specific configuration
COMPOSE_FILE="$SERVICE_DIR/docker-compose.yml"
log "=== Starting $SERVICE_NAME Backup ==="
# Check if restic configuration exists
if [ ! -f "$RESTIC_CONFIG_FILE" ]; then
error "Restic configuration file $RESTIC_CONFIG_FILE not found!"
exit 1
fi
# Source Restic configuration
source "$RESTIC_CONFIG_FILE"
# Create temporary backup directory
log "Creating temporary backup directory: $TEMP_BACKUP_DIR"
mkdir -p "$TEMP_BACKUP_DIR"
# Navigate to service directory
cd "$SERVICE_DIR"
# Check if compose file exists
if [ ! -f "$COMPOSE_FILE" ]; then
error "Docker compose file $COMPOSE_FILE not found!"
exit 1
fi
log "Stopping $SERVICE_NAME containers..."
docker compose down
# Wait a moment for containers to fully stop
sleep 5
log "Creating PostgreSQL database dump..."
# Start only the database container for backup
docker compose up -d db
# Wait for database to be ready
sleep 10
# Get database credentials from .env
if [ -f "$SERVICE_DIR/.env" ]; then
source "$SERVICE_DIR/.env"
else
error ".env file not found!"
exit 1
fi
# Create database dump
DUMP_FILE="$TEMP_BACKUP_DIR/${SERVICE_NAME}_db_$(date +%Y%m%d_%H%M%S).sql"
log "Creating database dump: $DUMP_FILE"
docker compose exec -T db pg_dump -U "$POSTGRES_USER" -d "$POSTGRES_DB" > "$DUMP_FILE"
if [ $? -eq 0 ]; then
success "Database dump created successfully"
else
error "Database dump failed!"
exit 1
fi
# Stop database container
docker compose down
log "Copying application data to temporary directory..."
# Copy data directories
cp -r "$DATA_DIR"/* "$TEMP_BACKUP_DIR/" 2>/dev/null || true
# Copy service configuration
cp -r "$SERVICE_DIR" "$TEMP_BACKUP_DIR/service_config"
log "Creating Restic backup..."
restic backup "$TEMP_BACKUP_DIR" \
--tag "$SERVICE_NAME" \
--tag "daily"
if [ $? -eq 0 ]; then
success "Restic backup completed successfully with tag: $SERVICE_NAME"
else
error "Restic backup failed!"
exit 1
fi
log "Restarting $SERVICE_NAME containers..."
docker compose up -d
# Wait for services to be ready
sleep 15
# Check if services are running
if docker compose ps --services --filter "status=running" | grep -q "webserver"; then
success "$SERVICE_NAME containers restarted successfully"
else
log "Warning: Some containers may not be running properly"
fi
log "Running Restic maintenance (forget old snapshots)..."
# Apply retention policy using template function
apply_retention "$SERVICE_NAME"
log "=== Backup completed successfully ==="
# Show backup statistics
log "Current repository stats:"
restic stats --mode raw-data