A comprehensive bash script to backup and migrate your entire Coolify instance from one server to another. This script handles Docker volumes, the Coolify database, SSH keys, and all associated data.
- Overview
- Features
- Prerequisites
- Installation
- Configuration
- Usage
- What Gets Migrated
- How It Works
- Supported Operating Systems
- Troubleshooting
- Safety Considerations
- Contributing
- License
This script automates the complete migration of a Coolify instance, including:
- All Docker volumes from running containers
- Coolify database and configuration files
- SSH authorized keys
- Complete data integrity preservation
The script runs on the source server and transfers everything to the destination server via SSH.
- Automatic Docker Volume Detection: Automatically discovers and backs up all volumes from running containers
- Parallel Compression: Uses
pigz(parallel gzip) for faster backups when available, with automatic fallback togzip - Auto-Installation: Can automatically install
pigzif not present (supports multiple package managers) - Interactive Configuration: Prompts for SSH key and destination host if not pre-configured
- Comprehensive Error Handling: Validates prerequisites and provides clear error messages
- SSH Key Merging: Safely merges existing SSH keys on destination server
- Automatic Coolify Installation: Installs Coolify on the destination server if needed
- Size Reporting: Shows total size of data to be migrated before starting
- Safe Operations: Includes confirmation prompts for critical operations
- Bash shell
- Docker installed and running
- SSH access to destination server
- Sufficient disk space for backup file
- Root or sudo access (for stopping Docker if needed)
- Root SSH access
- Sufficient disk space for all migrated data
- Internet connection (for Coolify installation)
- SSH connectivity from source to destination server
- SSH key-based authentication configured
The script needs a private key on the source server that can authenticate to the destination server. If you don't already have this:
-
Generate a keypair on the source server:
ssh-keygen -t ed25519 -f ~/.ssh/coolify_migration_keyThis creates two files:
~/.ssh/coolify_migration_keyβ private key (stays on source server)~/.ssh/coolify_migration_key.pubβ public key (goes on destination server)
-
Copy the public key to the destination server:
ssh-copy-id -i ~/.ssh/coolify_migration_key.pub root@<destination-ip>
Or manually β append the
.pubfile contents to~/.ssh/authorized_keyson the destination server. -
Test the connection:
ssh -i ~/.ssh/coolify_migration_key root@<destination-ip> "echo works"
-
Set the path in the script:
sshKeyPath="$HOME/.ssh/coolify_migration_key" destinationHost="<destination-ip>"
Note: If running the script with
sudo,$HOMEbecomes/root. Either copy the key to/root/.ssh/or use the full path instead of$HOME.
- Clone or download this repository:
git clone https://github.com/rogerb831/coolify-migration.git
cd coolify-migration- Edit the configuration section (optional - you can also be prompted at runtime):
nano migrate.sh- Run the script:
sudo bash migrate.shThe script has two configuration options at the top of the file:
sshKeyPath="$HOME/.ssh/your_private_key" # Path to SSH private key
destinationHost="server.example.com" # Destination server hostname/IPOption 1: Edit the script directly
- Modify lines 9-10 in
migrate.sh - Set your actual SSH key path and destination host
Option 2: Interactive prompts
- Leave the defaults as-is
- The script will prompt you for values at runtime
The script uses these default paths (can be modified in the script):
- Backup source directory:
/data/coolify/ - Backup filename:
coolify_backup.tar.gz(created in current directory)
-
Ensure all containers you want to migrate are running on the source server
-
Run the script:
./migrate.sh- Follow the interactive prompts:
- Configure SSH key and destination (if not pre-configured)
- Choose whether to install
pigzif not available - Confirm Docker stop (recommended for data consistency)
- Confirm backup file cleanup after migration
- Configuration Check: Script verifies or prompts for SSH key and destination host
- Pigz Detection: Checks for
pigzand offers auto-installation if missing - Prerequisites Validation:
- Verifies source directory exists
- Checks SSH key file exists
- Tests SSH connectivity to destination
- Docker Volume Discovery: Scans all running containers for volumes
- Size Calculation: Reports total data size to be migrated
- Backup Creation:
- Optionally stops Docker for consistency
- Creates compressed backup archive
- Remote Transfer:
- Transfers backup to destination server
- Extracts files
- Merges SSH keys
- Installs/updates Coolify
- Cleanup: Optionally removes local backup file
The script migrates the following:
- Location:
/data/coolify/ - Contains: Database, configuration files, application data
- All volumes attached to running containers
- Location:
/var/lib/docker/volumes/ - Automatically discovered from running containers
- Source:
~/.ssh/authorized_keys - Safely merged with existing keys on destination server
-
Volume Discovery:
- Lists all running Docker containers
- Inspects each container for mounted volumes
- Collects volume paths
-
Compression:
- Uses
pigz(parallel gzip) if available for faster compression - Falls back to
gzipifpigzis not available - Excludes socket files (
*.sock) from backup - Suppresses file-changed warnings during compression
- Uses
-
Archive Creation:
- Creates a tar archive with all data
- Compresses using the selected compressor
- Saves as
coolify_backup.tar.gz
-
Transfer:
- Streams backup file to destination via SSH
- Uses stdin/stdout for efficient transfer
-
Extraction:
- Stops Docker on destination (if running as service)
- Extracts backup archive
- Detects and uses
pigzfor decompression if available
-
SSH Key Management:
- Backs up existing authorized_keys
- Merges with new keys from source
- Removes duplicates
- Sets proper permissions
-
Coolify Installation:
- Installs curl if needed (with OS detection)
- Runs official Coolify installation script
- Ensures Coolify is ready to use
The script supports auto-installation of pigz on:
- Debian/Ubuntu/Raspberry Pi OS: Uses
apt-get - Red Hat/CentOS/Fedora: Uses
yumordnf - SUSE/openSUSE: Uses
zypper - Arch Linux: Uses
pacman - Alpine Linux: Uses
apk
For other distributions, you can manually install pigz or the script will use gzip as fallback.
- Cause: Network connectivity or authentication issues
- Solution:
- Verify destination server is reachable
- Check SSH key permissions:
chmod 600 your_key - Test SSH manually:
ssh -i your_key root@destination
- Cause: Coolify data directory not at
/data/coolify/ - Solution: Modify
backupSourceDirvariable in the script
- Cause: Docker not in PATH or not installed
- Solution: Install Docker or ensure it's in your PATH
- Cause: Package manager issues or insufficient permissions
- Solution:
- Install manually:
sudo apt-get install pigz(or equivalent) - Or continue with
gzip(slower but functional)
- Install manually:
- Cause: Insufficient disk space or permission issues
- Solution:
- Check available disk space:
df -h - Ensure write permissions in current directory
- Check if backup file already exists and remove if needed
- Check available disk space:
- Cause: Container may have been stopped during migration
- Solution: Ensure all containers remain running during volume discovery
If you encounter issues:
- Check the error messages - they provide specific guidance
- Verify all prerequisites are met
- Ensure sufficient disk space on both servers
- Test SSH connectivity manually before running the script
- Backup First: Always have a backup of your data before migration
- Test Connectivity: Verify SSH access works before running the script
- Check Disk Space: Ensure destination has enough space for all data
- Stop Services: Consider stopping non-critical services during migration
- Don't Interrupt: Let the script complete - interrupting may leave data in inconsistent state
- Monitor Progress: Watch for error messages
- Network Stability: Ensure stable network connection throughout
- Verify Data: Check that all containers and data are present
- Test Functionality: Verify Coolify is working correctly
- Clean Up: Remove backup file after confirming successful migration
- The script stops Docker on the destination server during extraction
- Existing SSH keys on destination are merged, not replaced
- The script requires root access on the destination server
- Socket files (
*.sock) are excluded from backup (they're runtime-only)
Contributions are welcome! This repository was converted from a popular gist to better manage PRs and updates.
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
- Original gist by Geczy
- Community contributors who have improved the script
- Added early
pigzdetection with auto-installation - Added interactive configuration prompts
- Improved error handling and validation
- Fixed variable quoting issues
- Added comprehensive Docker error handling
- Improved OS detection patterns
- Added fallback for
nproccommand - Enhanced SSH key merging logic
Note: Always test the migration process in a non-production environment first to ensure it meets your specific requirements.