This document explains how to set up and run the Bitwarden Backup API using Docker.
.env
file configured (copy from env.example
)# Make the script executable (if not already)
chmod +x run-api.sh
# Run the API
./run-api.sh
# Build and start the API service with Redis
docker-compose -f docker-compose.api.yml up --build -d
# View logs
docker-compose -f docker-compose.api.yml logs -f
# Stop the service
docker-compose -f docker-compose.api.yml down
# Build and start both services
docker-compose up --build -d
# View API logs
docker-compose logs -f bitwarden-backup-api
# View backup logs
docker-compose logs -f bitwarden-backup
# Stop all services
docker-compose down
Create a .env
file in the root directory with your configuration. Use env.example
as a template.
Core Backup Variables:
BW_CLIENTID
- Your Bitwarden API Client IDBW_CLIENTSECRET
- Your Bitwarden API Client SecretBW_PASSWORD
- Your Bitwarden Master PasswordENCRYPTION_PASSWORD
- Strong password for backup encryptionRCLONE_CONFIG_BASE64
- Your base64-encoded rclone configurationAPI-Specific Variables:
API_TOKEN
- Authentication token for API access (use a strong, random token)REDIS_URL
- Redis connection URL (default: redis://localhost:6379/0
)BACKUP_PATH
- Remote path/bucket for storing backups (default: bitwarden-backup
)# Bitwarden credentials
BW_CLIENTID="user.1234567890abcdef"
BW_CLIENTSECRET="abcdef1234567890"
BW_PASSWORD="MySecretPassword123!"
ENCRYPTION_PASSWORD="BackupEncryption456!"
RCLONE_CONFIG_BASE64="W215LXMzXQp0eXBlID0gczMK..."
# API configuration
API_TOKEN="your_secure_random_api_token_here"
REDIS_URL="redis://localhost:6379/0"
BACKUP_PATH="bitwarden-backup"
The API runs on port 5050
by default. To change this:
Update the port mapping in docker-compose.api.yml
:
ports:
- "YOUR_PORT:5050"
Or set a custom port in the Dockerfile.api CMD section.
Once running, the API will be available at:
All API endpoints (except health checks) require authentication using the Authorization
header:
curl -H "Authorization: Bearer your_api_token" http://localhost:5050/api/v1/backups
System:
GET /
- API root informationGET /api/v1/health
- Health check (API, Redis, rclone)GET /api/v1/info
- System informationBackups:
GET /api/v1/backups
- List backup filesGET /api/v1/backups/{remote}/{filename}
- Get backup metadataDELETE /api/v1/backups/{remote}/{filename}
- Delete a backupPOST /api/v1/backups/refresh_cache
- Refresh backup cacheGET /api/v1/download/{remote}/{filename}
- Download backup fileRemotes:
GET /api/v1/remotes
- List rclone remotesGET /api/v1/remotes/{remote}/check
- Check remote connectivityGET /api/v1/remotes/{remote}/usage
- Get storage usageThe API setup includes:
βββ Dockerfile.api # API-specific Dockerfile
βββ docker-compose.api.yml # API + Redis Docker Compose
βββ docker-compose.yml # Backup service Docker Compose
βββ run-api.sh # Helper script to run API
βββ API.md # This documentation
βββ api/ # API source code
βββ main.py # FastAPI application
βββ config.py # Configuration management
βββ auth.py # Authentication
βββ cache.py # Redis caching
βββ models.py # Data models
βββ requirements.txt # Python dependencies
βββ routes/ # API route handlers
βββ backups.py
βββ remotes.py
βββ system.py
The API container is configured with --reload
for development. Changes to your code will automatically restart the server.
To run the container interactively for debugging:
docker-compose -f docker-compose.api.yml exec bitwarden-backup-api bash
To build the image without starting:
docker-compose -f docker-compose.api.yml build
View API logs:
docker-compose -f docker-compose.api.yml logs -f bitwarden-backup-api
View Redis logs:
docker-compose -f docker-compose.api.yml logs -f redis
Check .env file: Ensure all required variables are set
# Verify required variables
grep -E "^(API_TOKEN|REDIS_URL|BW_CLIENTID)" .env
Verify Docker: docker info
Check logs: docker-compose -f docker-compose.api.yml logs
Check Redis health:
docker-compose -f docker-compose.api.yml exec redis redis-cli ping
Verify Redis URL: Ensure REDIS_URL
points to the correct Redis instance
Network connectivity: Ensure services are on the same Docker network
API_TOKEN
is set and matches your requestsAuthorization: Bearer your_token
If port 5050 is already in use:
docker ps
and docker stop <container>
The containers run as non-root users for security. If you encounter permission issues:
.env
file is readable: chmod 644 .env
The API includes comprehensive health checks. If they fail:
Check individual service health:
curl http://localhost:5050/api/v1/health
For production deployment:
Configure resource limits in Docker Compose:
deploy:
resources:
limits:
cpus: '1.0'
memory: 512M
If you were previously running:
uvicorn api.main:app --reload --host 0.0.0.0 --port 5050
You can now simply run:
./run-api.sh
The Docker setup provides the same functionality with additional benefits:
curl -H "Authorization: Bearer your_api_token" \
"http://localhost:5050/api/v1/backups?remote=s3-backup"
curl -H "Authorization: Bearer your_api_token" \
"http://localhost:5050/api/v1/download/s3-backup/bw_backup_20241218123456.json.gz.enc" \
-o backup.enc
curl "http://localhost:5050/api/v1/health"
curl -X POST -H "Authorization: Bearer your_api_token" \
"http://localhost:5050/api/v1/trigger-backup"
For more examples, visit the interactive API documentation at http://localhost:5050/api/v1/docs when the API is running.