A comprehensive Bash script for automated backup of n8n workflows and credentials from Docker containers, with support for encryption and detailed logging.
- Automatic Container Detection: Finds all running n8n containers automatically
- Manual Container Selection: Option to specify specific containers
- Encrypted Backups: Creates password-protected ZIP archives
- Detailed Logging: Maintains comprehensive logs of all backup operations
- Batch Processing: Handles multiple n8n containers in one run
- Error Handling: Robust error checking and reporting
- Credential Export: Backs up credentials in decrypted format for easy restoration
- Docker installed and running
ziputility installed on the host system- Sufficient permissions to execute Docker commands
- Running n8n container(s)
- Clone this repository:
git clone <repository-url>
cd n8n-backup-script- Make the script executable:
chmod +x backup.shEdit the backup.sh file to customize the following settings:
# Manual container names
MANUAL_CONTAINERS=(
# "n8n1"
# "n8n2"
)
# Auto-detect containers with 'n8n' in name
AUTO_DETECT_N8N=true# Enable/disable encryption
ENABLE_ENCRYPTION=true
# Prompt for password or use default
PROMPT_FOR_PASSWORD=true
# Default password (only if PROMPT_FOR_PASSWORD=false)
DEFAULT_PASSWORD=""By default, the script creates the following structure:
script-directory/
├── backup.sh
├── files/ # Temporary backup files
├── logs/ # Backup logs
└── n8n_backup_YYYYMMDD_HHMMSS.zip
./backup.shThis will automatically find and backup all running containers with "n8n" in their name.
./backup.sh container1 container2Edit the MANUAL_CONTAINERS array in the script:
MANUAL_CONTAINERS=(
"n8n_production"
"n8n_staging"
)Then run:
./backup.shThe script creates a timestamped ZIP file:
n8n_backup_20240115_143022.zip
If encryption is disabled:
n8n_backup_20240115_143022_unencrypted.zip
files/
├── container1/
│ ├── workflows/
│ │ └── [workflow JSON files]
│ └── credentials/
│ └── [credential JSON files]
└── container2/
├── workflows/
└── credentials/
Detailed logs are saved in the logs/ directory:
logs/n8n_backup_20240115_143022.log
To restore workflows and credentials:
- Extract the ZIP file (enter password if encrypted)
- Navigate to the container's directory
- Import workflows:
docker exec -u node <container_name> n8n import:workflow --input=/path/to/workflows/- Import credentials:
docker exec -u node <container_name> n8n import:credentials --input=/path/to/credentials/- Credentials are exported in decrypted format
- Backup files contain sensitive information
- Always use strong passwords for encryption
- Store backups in secure locations
- Consider adding
*.zipandfiles/to.gitignore - Never commit backup files to version control
ERROR: Container <name> not found
Solution: Verify container name with docker ps and update configuration.
ERROR: Permission denied
Solution: Ensure script has execute permissions (chmod +x backup.sh) and user has Docker access.
ERROR: zip command not installed on system
Solution: Install zip utility:
# Ubuntu/Debian
sudo apt-get install zip
# CentOS/RHEL
sudo yum install zip
# macOS
brew install zipERROR: Workflows export failed for <container>
Solution: Check that n8n is running properly in the container and has data to export.
To run automatic backups daily at 2 AM:
crontab -eAdd:
0 2 * * * /path/to/backup.sh
For password-protected backups with cron, set:
PROMPT_FOR_PASSWORD=false
DEFAULT_PASSWORD="your-secure-password"Modify these variables to change temporary paths inside containers:
CONTAINER_WORKFLOWS_PATH="/tmp/n8n_backup/workflows/"
CONTAINER_CREDENTIALS_PATH="/tmp/n8n_backup/credentials/"Change the user for n8n commands:
DOCKER_USER="node" # Default for n8n containersAdjust verbosity:
LOG_LEVEL="INFO" # Options: DEBUG, INFO, WARNING, ERRORContributions are welcome! Please feel free to submit a Pull Request.
For issues and questions, please open an issue on GitHub.
- Initial release
- Auto-detection of n8n containers
- Encrypted ZIP backups
- Comprehensive logging
- Multi-container support