Portainer Stack Backup Script
This script connects to a Portainer instance and exports all stacks with their configurations, environment variables, and docker-compose files.
Prerequisites
curl(usually pre-installed on macOS)jqfor JSON processing:brew install jq
Usage
-
Make sure the script is executable:
chmod +x portainer-backup.sh -
Run the script:
./portainer-backup.sh -
When prompted, enter:
- Portainer URL (e.g.,
http://localhost:9000) - Username
- Password
- Portainer URL (e.g.,
Alternatively, you can edit the script and set these variables at the top:
PORTAINER_URL="http://your-portainer-url:9000"
USERNAME="your-username"
PASSWORD="your-password"
Output
The script creates a portainer-stacks-backup directory containing:
portainer-stacks-backup/
├── stack-name-1/
│ ├── stack-info.json # Complete stack information
│ ├── environment-variables.json # Environment variables as JSON
│ ├── .env # Environment variables in .env format
│ ├── docker-compose.yml # Stack's compose file
│ └── metadata.txt # Human-readable metadata
├── stack-name-2/
│ └── ...
What gets backed up
For each stack:
- Complete stack configuration (JSON format)
- Environment variables (both JSON and .env formats)
- Docker Compose file content
- Metadata (creation date, status, type, etc.)
Error Handling
The script includes error handling for:
- Missing dependencies
- Authentication failures
- Network connectivity issues
- API errors
Security Note
The script handles authentication via JWT tokens. Passwords are prompted securely (hidden input) if not set in the script.