Skip to content

Instantly share code, notes, and snippets.

@samzong
Last active September 4, 2024 15:14
Show Gist options
  • Save samzong/6d230ad327ff5aae903377b5b47182a8 to your computer and use it in GitHub Desktop.
Save samzong/6d230ad327ff5aae903377b5b47182a8 to your computer and use it in GitHub Desktop.
auto input n8n workflow backup and activate all workflows
#!/usr/bin/env bash
# Set strict mode
set -euo pipefail
IFS=$'\n\t'
# Script version
VERSION="1.3.0"
# Default values
WORKFLOW_URL=""
CREDENTIALS_URL=""
WORKFLOW_FILE="workflows_backup.json"
CREDENTIALS_FILE="credentials_backup.json"
LOG_FILE="n8n_import.log"
TIMEOUT=30
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color
# Function to log messages
log_message() {
local level=$1
local message=$2
local timestamp=$(date "+%Y-%m-%d %H:%M:%S")
echo -e "${timestamp} [${level}] ${message}" | tee -a "$LOG_FILE"
}
# Function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to display script usage
usage() {
echo "Usage: $0 [-u WORKFLOW_URL] [-c CREDENTIALS_URL] [-f WORKFLOW_FILE] [-d CREDENTIALS_FILE] [-t TIMEOUT] [-h]"
echo " -u URL Specify the URL to download the workflow backup"
echo " -c URL Specify the URL to download the credentials backup"
echo " -f FILE Specify the name of the workflow backup file (default: $WORKFLOW_FILE)"
echo " -d FILE Specify the name of the credentials backup file (default: $CREDENTIALS_FILE)"
echo " -t TIMEOUT Specify the timeout for wget in seconds (default: $TIMEOUT)"
echo " -h Display this help message"
exit 1
}
# Parse command-line options
while getopts ":u:c:f:d:t:h" opt; do
case ${opt} in
u ) WORKFLOW_URL=$OPTARG ;;
c ) CREDENTIALS_URL=$OPTARG ;;
f ) WORKFLOW_FILE=$OPTARG ;;
d ) CREDENTIALS_FILE=$OPTARG ;;
t ) TIMEOUT=$OPTARG ;;
h ) usage ;;
\? ) echo "Invalid option: $OPTARG" 1>&2; usage ;;
: ) echo "Invalid option: $OPTARG requires an argument" 1>&2; usage ;;
esac
done
# Function to clean up temporary files
cleanup() {
log_message "INFO" "Cleaning up temporary files..."
[[ -f "$WORKFLOW_FILE" ]] && rm -f "$WORKFLOW_FILE"
[[ -f "$CREDENTIALS_FILE" ]] && rm -f "$CREDENTIALS_FILE"
}
# Trap to ensure cleanup on script exit
trap cleanup EXIT
# Display script version and start message
log_message "INFO" "Starting N8N Import Script v$VERSION"
# Function to download file
download_file() {
local url=$1
local file=$2
log_message "INFO" "Downloading file from $url"
if ! wget --timeout="$TIMEOUT" -q "$url" -O "$file"; then
log_message "ERROR" "Unable to download the file. Please check your network connection or contact the developer."
return 1
fi
return 0
}
# Function to import workflows
import_workflows() {
if [ -n "$WORKFLOW_URL" ]; then
log_message "INFO" "Importing workflow from $WORKFLOW_URL"
if ! download_file "$WORKFLOW_URL" "$WORKFLOW_FILE"; then
return 1
fi
import_output=$(n8n import:workflow --input="$WORKFLOW_FILE" 2>&1)
if echo "$import_output" | grep -qi "error"; then
log_message "ERROR" "There was a problem importing the workflow. Please check if the n8n service is running properly."
log_message "ERROR" "Error output: $import_output"
return 1
fi
log_message "INFO" "Updating all workflows and setting them to active"
update_output=$(n8n update:workflow --all --active=true 2>&1)
if echo "$update_output" | grep -qi "error"; then
log_message "ERROR" "There was a problem updating the workflows. Please check if the n8n service is running properly."
log_message "ERROR" "Error output: $update_output"
return 1
fi
log_message "INFO" "All workflows have been successfully imported and activated."
fi
return 0
}
# Function to import credentials
import_credentials() {
if [ -n "$CREDENTIALS_URL" ]; then
log_message "INFO" "Importing credentials from $CREDENTIALS_URL"
if ! download_file "$CREDENTIALS_URL" "$CREDENTIALS_FILE"; then
return 1
fi
import_output=$(n8n import:credentials --input="$CREDENTIALS_FILE" 2>&1)
if echo "$import_output" | grep -qi "error"; then
log_message "ERROR" "There was a problem importing the credentials. Please check if the n8n service is running properly."
log_message "ERROR" "Error output: $import_output"
return 1
fi
log_message "INFO" "All credentials have been successfully imported."
fi
return 0
}
# Check if n8n command exists
if ! command_exists n8n; then
log_message "ERROR" "n8n command not found. Please ensure n8n is installed and in your PATH."
exit 1
fi
# Perform imports
import_workflows
import_credentials
# Check if any import was performed
if [ -z "$WORKFLOW_URL" ] && [ -z "$CREDENTIALS_URL" ]; then
log_message "WARNING" "No import operation was specified. Use -u for workflow URL and/or -c for credentials URL."
echo -e "${YELLOW}Warning: No import operation was specified. Use -u for workflow URL and/or -c for credentials URL.${NC}"
else
# All steps completed successfully
echo -e "${GREEN}Success: All specified imports have been completed successfully.${NC}"
fi
echo "Log file: $LOG_FILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment