This project is no longer actively maintained by the original author because I no longer use Portainer. The repository remains public for archival purposes and community use. Pull requests and forks are welcome. If someone is interested in taking over maintenance of the project, feel free to open an issue or contact me.
This Docker container automates the backup of Docker stacks and environment files from Portainer to a GitHub repository. The solution is highly configurable, allowing you to schedule backups at specific intervals or via cron expressions.
You can start using this container by deploying it with:
version: '3.9'
services:
backup-portainer-github:
container_name: ${TARGET_CONTAINER_NAME}
image: ghcr.io/lepikouze/backup-portainer-github:latest
environment:
PUID: 1000
PGID: 1000
PORTAINER_URL: ${PORTAINER_URL}
PORTAINER_USERNAME: ${PORTAINER_USERNAME}
PORTAINER_PASSWORD: ${PORTAINER_PASSWORD}
TARGET_CONTAINER_NAME: ${TARGET_CONTAINER_NAME}
STACKS_BASE_PATH: /stacks
GITHUB_USERNAME: ${GITHUB_USERNAME}
GITHUB_TOKEN: ${GITHUB_TOKEN}
GITHUB_REPO_NAME: ${GITHUB_REPO_NAME}
README_TITLE: ${README_TITLE} # Optional
README_DESCRIPTION: ${README_DESCRIPTION} # Optional
GITHUB_REPO_PATH: ${GITHUB_REPO_PATH} # Optional
#CRON_SCHEDULE: 0 0 4 * * 1 # Optional, use this OR INTERVAL_SECONDS, not both
INTERVAL_SECONDS: 60 # Optional, use this OR CRON_SCHEDULE, not both
ports:
- "5000:5000"
volumes:
- /folder/data:${GITHUB_REPO_PATH} # Optional
- /var/run/docker.sock:/var/run/docker.sock:ro
- /portainer_compose_directory:/stacks:ro
restart: unless-stopped
# Set environment variables (replace with your actual values)
export PUID=1000
export PGID=1000
export PORTAINER_URL=https://your-portainer-url
export PORTAINER_USERNAME=your-portainer-username
export PORTAINER_PASSWORD=your-portainer-password
export TARGET_CONTAINER_NAME=portainer-backup
export STACKS_BASE_PATH=/stacks
export GITHUB_USERNAME=your-github-username
export GITHUB_TOKEN=your-github-token
export GITHUB_REPO_NAME=your-github-repo-name
export README_TITLE="Portainer Backup Automation" # Optional
export README_DESCRIPTION="Automated backup of Portainer stacks and environment files." # Optional
export GITHUB_REPO_PATH=/folder/data/repo # Optional
# export CRON_SCHEDULE="0 0 4 * * 1" # Optional, uncomment if using cron schedule
export INTERVAL_SECONDS=60 # Optional, comment if using cron schedule
# Run the Docker container
docker run -d \
--name $TARGET_CONTAINER_NAME \
--env PUID=$PUID \
--env PGID=$PGID \
--env PORTAINER_URL=$PORTAINER_URL \
--env PORTAINER_USERNAME=$PORTAINER_USERNAME \
--env PORTAINER_PASSWORD=$PORTAINER_PASSWORD \
--env TARGET_CONTAINER_NAME=$TARGET_CONTAINER_NAME \
--env STACKS_BASE_PATH=$STACKS_BASE_PATH \
--env GITHUB_USERNAME=$GITHUB_USERNAME \
--env GITHUB_TOKEN=$GITHUB_TOKEN \
--env GITHUB_REPO_NAME=$GITHUB_REPO_NAME \
--env README_TITLE="$README_TITLE" \ # Optional
--env README_DESCRIPTION="$README_DESCRIPTION" \ # Optional
--env GITHUB_REPO_PATH=$GITHUB_REPO_PATH \ # Optional
--env INTERVAL_SECONDS=$INTERVAL_SECONDS \ # Optional, use this OR CRON_SCHEDULE, not both
-p 5000:5000 \
-v /folder/data:$GITHUB_REPO_PATH \ # Optional
-v /var/run/docker.sock:/var/run/docker.sock:ro \
-v /portainer_compose_directory:/stacks:ro \
ghcr.io/lepikouze/backup-portainer-github:latest \
--restart unless-stopped
| Name | Description | Default |
|---|---|---|
PUID |
User ID for file permissions within the container. | 1000 |
PGID |
Group ID for file permissions within the container. | 1000 |
PORTAINER_URL |
The URL of the Portainer instance to be backed up. | Required |
PORTAINER_USERNAME |
Username for Portainer authentication. | Required |
PORTAINER_PASSWORD |
Password for Portainer authentication. | Required |
TARGET_CONTAINER_NAME |
The name of this backup container within the Portainer environment. | Required |
STACKS_BASE_PATH |
Path inside the container where the Portainer stacks are stored. | /stacks |
GITHUB_USERNAME |
GitHub username to authenticate and push backups. | Required |
GITHUB_TOKEN |
GitHub personal access token to authenticate and push backups. | Required |
GITHUB_REPO_NAME |
Name of the GitHub repository where backups are stored. | Required |
README_TITLE |
Title for the README.md file in the GitHub repository. |
Portainer Backup Automation |
README_DESCRIPTION |
Description for the README.md file in the GitHub repository. |
Automated backup of Portainer stacks and environment files. |
GITHUB_REPO_PATH |
Local path inside the container where the GitHub repository is cloned. | /tmp/repo |
CRON_SCHEDULE |
(Optional) Cron expression for scheduling backups. | use this OR INTERVAL_SECONDS, not both |
INTERVAL_SECONDS |
(Optional) Interval in seconds between each backup execution. | use this OR CRON_SCHEDULE, not both |
| Path (Host) | Path (Container) | Description |
|---|---|---|
/folder/data |
${GITHUB_REPO_PATH} |
Directory where backup data will be stored inside the container. |
/var/run/docker.sock |
/var/run/docker.sock:ro |
Docker socket to interact with the Docker API on the host (read-only). |
/portainer_compose_directory |
/stacks:ro |
Directory containing Portainer stacks, mounted in read-only mode. |
Clone the repository and navigate to its directory:
git clone https://github.com/lepikouze/backup-portainer-github.git
cd backup-portainer-github
docker-compose up -d
Once the container is running, you can access the web interface via:
http://localhost:5000
For support, please open an issue in the GitHub repository. When reporting issues, please include logs and relevant configuration details to help with troubleshooting.
Contributions are welcome! Please submit a Pull Request or open an Issue to discuss any changes or improvements.
This project is licensed under the MIT License - see the LICENSE file for details.
Project Link: lepikouze/backup-portainer-github
This project was inspired by SavageSoftware/portainer-backup.