diff --git a/CLAUDE.md b/CLAUDE.md index f90ee6c..b875e0d 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -39,6 +39,7 @@ This is **n8n-install**, a Docker Compose-based installer that provides a compre - `scripts/apply_update.sh`: Applies updates after git sync - `scripts/docker_cleanup.sh`: Removes unused Docker resources (used by `make clean`) - `scripts/download_top_workflows.sh`: Downloads community n8n workflows +- `scripts/import_workflows.sh`: Imports workflows from `n8n/backup/workflows/` into n8n (used by `make import`) **Project Name**: All docker-compose commands use `-p localai` (defined in Makefile as `PROJECT_NAME := localai`). @@ -75,6 +76,7 @@ make monitor # Live CPU/memory monitoring (docker stats) make restart # Restart all services make show-restarts # Show restart count per container make doctor # Run system diagnostics (DNS, SSL, containers, disk, memory) +make import # Import n8n workflows from backup make switch-beta # Switch to develop branch and update make switch-stable # Switch to main branch and update diff --git a/Makefile b/Makefile index 46e9b4b..c0dd255 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: help install update update-preview clean clean-all logs status monitor restart show-restarts doctor switch-beta switch-stable +.PHONY: help install update update-preview clean clean-all logs status monitor restart show-restarts doctor switch-beta switch-stable import PROJECT_NAME := localai @@ -18,6 +18,7 @@ help: @echo " make restart Restart all services" @echo " make show-restarts Show restart count per container" @echo " make doctor Run system diagnostics" + @echo " make import Import n8n workflows from backup" @echo "" @echo " make switch-beta Switch to beta (develop branch)" @echo " make switch-stable Switch to stable (main branch)" @@ -75,3 +76,6 @@ switch-stable: git restore docker-compose.yml git checkout main sudo bash ./scripts/update.sh + +import: + docker compose -p $(PROJECT_NAME) run --rm -e FORCE_IMPORT=true n8n-import diff --git a/README.md b/README.md index 279cc76..8804ef6 100644 --- a/README.md +++ b/README.md @@ -312,6 +312,7 @@ The project includes a Makefile for simplified command execution: | `make monitor` | Live CPU/memory monitoring | | `make restart` | Restart all services | | `make show-restarts` | Show restart count per container | +| `make import` | Import n8n workflows from backup | ### Diagnostics diff --git a/docker-compose.yml b/docker-compose.yml index 946d620..661d5ac 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -177,10 +177,10 @@ services: <<: *service-n8n-env RUN_N8N_IMPORT: ${RUN_N8N_IMPORT:-false} entrypoint: /bin/sh - command: /scripts/n8n_import_script.sh + command: /scripts/import_workflows.sh volumes: - ./n8n/backup:/backup - - ./n8n/n8n_import_script.sh:/scripts/n8n_import_script.sh:ro + - ./scripts/import_workflows.sh:/scripts/import_workflows.sh:ro depends_on: postgres: condition: service_healthy diff --git a/n8n/n8n_import_script.sh b/n8n/n8n_import_script.sh deleted file mode 100644 index d0501b2..0000000 --- a/n8n/n8n_import_script.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/bin/sh - -# Exit immediately if RUN_N8N_IMPORT is not set to true -if [ "$RUN_N8N_IMPORT" != "true" ]; then - echo 'Skipping n8n import based on RUN_N8N_IMPORT environment variable.' - exit 0 -fi - -set -e - -echo 'Importing credentials...' -find /backup/credentials -maxdepth 1 -type f -not -name '.gitkeep' -print -exec sh -c ' - echo "Attempting to import credential file: $1"; - n8n import:credentials --input="$1" || echo "Error importing credential file: $1" -' sh {} \; - -echo 'Importing workflows...' -find /backup/workflows -maxdepth 1 -type f -not -name '.gitkeep' -print -exec sh -c ' - echo "Attempting to import workflow file: $1"; - n8n import:workflow --input="$1" || echo "Error importing workflow file: $1" -' sh {} \; - -echo 'Import process finished.' \ No newline at end of file diff --git a/scripts/import_workflows.sh b/scripts/import_workflows.sh new file mode 100644 index 0000000..dfd821b --- /dev/null +++ b/scripts/import_workflows.sh @@ -0,0 +1,62 @@ +#!/bin/sh +# Import n8n workflows and credentials from backup directory +# This script runs inside the n8n-import container + +# Exit if neither import flag is set +if [ "$RUN_N8N_IMPORT" != "true" ] && [ "$FORCE_IMPORT" != "true" ]; then + echo 'Skipping n8n import based on environment variables.' + exit 0 +fi + +set -e + +# Import credentials first +echo 'Importing credentials...' +CRED_FILES=$(find /backup/credentials -maxdepth 1 -type f -not -name '.gitkeep' 2>/dev/null || true) +if [ -n "$CRED_FILES" ]; then + CRED_COUNT=$(echo "$CRED_FILES" | wc -l | tr -d ' ') + CURRENT=0 + echo "$CRED_FILES" | while IFS= read -r file; do + CURRENT=$((CURRENT + 1)) + filename=$(basename "$file") + echo "[$CURRENT/$CRED_COUNT] Importing credential: $filename" + n8n import:credentials --input="$file" 2>/dev/null || echo " Error importing: $filename" + done +fi + +# Import workflows +echo '' +echo 'Importing workflows...' +WORKFLOW_FILES=$(find /backup/workflows -maxdepth 1 -type f -not -name '.gitkeep' 2>/dev/null || true) +if [ -z "$WORKFLOW_FILES" ]; then + echo 'No workflows found to import.' + exit 0 +fi + +TOTAL=$(echo "$WORKFLOW_FILES" | wc -l | tr -d ' ') +echo "Found $TOTAL workflows to import" +echo '' + +# Use a counter file since pipes create subshells +COUNTER_FILE=$(mktemp) +echo "0" > "$COUNTER_FILE" + +echo "$WORKFLOW_FILES" | while IFS= read -r file; do + CURRENT=$(cat "$COUNTER_FILE") + CURRENT=$((CURRENT + 1)) + echo "$CURRENT" > "$COUNTER_FILE" + + filename=$(basename "$file") + printf "[%3d/%d] %s" "$CURRENT" "$TOTAL" "$filename" + + if n8n import:workflow --input="$file" >/dev/null 2>&1; then + echo " OK" + else + echo " FAILED" + fi +done + +rm -f "$COUNTER_FILE" + +echo '' +echo 'Import complete!' diff --git a/welcome/app.js b/welcome/app.js index b7d2b34..239afb4 100644 --- a/welcome/app.js +++ b/welcome/app.js @@ -418,6 +418,7 @@ { cmd: 'make show-restarts', desc: 'Show restart count per container' }, { cmd: 'make doctor', desc: 'Run system diagnostics' }, { cmd: 'make update', desc: 'Update system and services' }, + { cmd: 'make import', desc: 'Import n8n workflows from backup' }, { cmd: 'make clean', desc: 'Remove unused Docker resources' } ];