A highly optimized Python tool for transferring directories between cPanel servers using FTP protocol only.
- FTP-Only Transfer: No SSH dependencies, works with standard FTP access
- Enhanced Reliability: Automatic retry logic with exponential backoff for failed transfers
- Optimized Performance: Binary mode, passive mode, and optimized buffer sizes for faster transfers
- Directory Transfer: Recursively transfer entire directory structures with progress tracking
- Compression Support: Optional tar.gz compression for faster transfers
- Chunked Transfer: Handle large directories by splitting into manageable chunks
- Detailed Reporting: Track transfer progress and generate comprehensive reports
- Cross-Platform: Works on Windows, macOS, and Linux
- Robust Error Handling: Better error detection, logging, and recovery mechanisms
- Python 3.6+
- FTP access to both source and target cPanel accounts
- Pipenv (for virtual environment management and automatic .env loading)
- Sufficient local disk space in
tmp_trans/directory for temporary files during transfer
-
Install Pipenv (if not already installed):
pip install pipenv
-
Install dependencies and create virtual environment:
pipenv install
-
Configure environment variables:
- Edit the
.envfile with your FTP server credentials
- Edit the
Edit your .env file:
# Source FTP Server
SOURCE_HOST=source.hostgator.com
SOURCE_PORT=21
SOURCE_USER=your_username
SOURCE_PASSWORD=your_password
# Target FTP Server
TARGET_HOST=target.hosting.com
TARGET_PORT=21
TARGET_USER=target_username
TARGET_PASSWORD=target_password
# Transfer Options
TRANSFER_PATH=mail/your-domain.com
USE_CHUNKING=true
MAX_CHUNK_SIZE=52428800You can run the tool in two ways with pipenv:
pipenv run python migrate.py --path mail/your-domain.compipenv shell
python migrate.py --path mail/your-domain.compipenv run python migrate.py --path mail/your-domain.comResults are automatically saved to transfers_results.csv
pipenv run python migrate.py --path mail/your-domain.com --chunking --chunk-size 10485760pipenv run python migrate.py --path mail/your-domain.com --compression-level 6pipenv run python migrate.py --path mail/your-domain.com --verbose--path PATH: Directory path to transfer (relative to FTP root)
--chunking: Enable chunked transfer for large directories--chunk-size SIZE: Maximum chunk size in bytes (default: 50MB)
--compression-level {1-9}: Compression level (1=fastest, 9=smallest, default: 1)
--verbose: Enable detailed logging
- Connect: Establishes FTP connections to both source and target servers with retry logic and timeout management
- Analyze: Calculates directory size and file count using optimized MLSD commands when available
- Download: Downloads the source directory structure via FTP to
tmp_trans/directory with automatic retry on failure - Process: Optionally compresses or chunks the data based on size and settings
- Upload: Uploads the processed data to the target server via FTP with progress tracking
- Cleanup: Automatically removes temporary files after transfer
- Report: Generates detailed transfer report with statistics and performance metrics
- Exponential Backoff: Failed operations automatically retry with increasing delays (2s, 4s, 8s)
- Connection Recovery: Dropped connections are automatically re-established
- File Verification: Downloaded files are verified for size match when possible
- Graceful Degradation: Falls back to basic FTP commands if modern MLSD is unavailable
- Best for: Small to medium directories (< 50MB)
- Process: Direct download → optional compression → upload
- Pros: Simple and fast
- Cons: Requires local disk space equal to source directory size
- Best for: Large directories or limited local disk space
- Process: Download in chunks → compress each chunk → upload chunks
- Pros: Handles very large directories, manages disk space usage
- Cons: More complex, potentially slower
- Level 1 (fastest): Minimal compression, fastest processing
- Level 6 (balanced): Good compression ratio with reasonable speed
- Level 9 (smallest): Maximum compression, slowest processing
- Database Transfer: Not supported - this tool only handles file transfers via FTP
- SSH/SCP/SFTP: Not supported in this FTP-only version
- Large Files: May be slower than SSH-based methods for very large transfers
- Temporary Space: Requires local disk space in
tmp_trans/directory for temporary files during transfer - FTP Limitations: Subject to FTP timeout and connection stability
- Verify credentials in
.envfile - Check FTP service is enabled on both servers
- Firewall settings - ensure FTP ports (21, and passive ports) are open
- Passive vs Active FTP - most hosting providers use passive FTP
- Port configuration - verify correct FTP ports (usually 21)
-
Use chunking for directories larger than available local disk space:
pipenv run python migrate.py --path large-directory --chunking --chunk-size 10485760
-
Lower compression level to reduce processing time:
pipenv run python migrate.py --path large-directory --compression-level 1
-
Ensure sufficient local disk space for temporary files
-
Check transfer report for detailed error information in
transfers_results.csv -
Use verbose logging for debugging:
pipenv run python migrate.py --path problem-directory --verbose
-
Try smaller subdirectories individually if large transfers fail
-
Check FTP timeouts - some servers have strict timeout limits
-
Network issues: The tool now automatically retries failed operations up to 3 times
-
Check logs: Review
general.logfor detailed error traces and timing information
-
Slow transfers:
- Verify network speed between servers
- Try disabling compression (
--compression-level 1) - Check if server has bandwidth limitations
-
Connection drops:
- The tool automatically retries with exponential backoff
- Consider transferring during off-peak hours
- Check firewall settings for FTP passive mode ports
-
Memory issues:
- Use chunked transfer for large directories
- Reduce chunk size:
--chunk-size 10485760(10MB) - Ensure sufficient disk space in
tmp_trans/
- "FTP connection failed": Check credentials, hostname, and port
- "Permission denied": Verify FTP user has read/write permissions
- "Directory not found": Ensure the source path exists and is accessible
- "Disk space exceeded": Use chunking or free up local disk space
pipenv run python migrate.py --path mail/example.com --chunkingpipenv run python migrate.py --path public_html --compression-level 6pipenv run python migrate.py --path backups/large-backup --chunking --chunk-size 5242880 --compression-level 3 --verboseAfter each transfer, a CSV report is automatically generated and appended to transfers_results.csv in the current directory. This allows you to track multiple transfers over time in a single spreadsheet-compatible file.
The CSV contains the following columns:
- timestamp: When the report was generated
- success: SUCCESS or FAILED
- protocol: Transfer protocol used (always "ftp")
- source_path: Source directory path
- target_path: Target directory path
- start_time: Transfer start time
- end_time: Transfer end time
- duration_seconds: Transfer duration
- total_size_mb: Total directory size in MB
- transferred_size_mb: Actual transferred size in MB
- transfer_speed_mbps: Transfer speed in MB/s
- file_count: Number of files transferred
- directory_count: Number of directories
- errors: Any error messages (semicolon-separated)
Example CSV output:
timestamp,success,protocol,source_path,target_path,start_time,end_time,duration_seconds,total_size_mb,transferred_size_mb,transfer_speed_mbps,file_count,directory_count,errors
2025-10-02 14:30:15,SUCCESS,ftp,mail/example.com,mail/example.com,2025-10-02 14:28:30,2025-10-02 14:30:15,105.0,50.0,50.0,0.48,1250,45,
- Use compression for directories with many text files (emails, logs)
- Disable compression for directories with already compressed files (images, videos)
- Use chunking for very large directories to avoid timeouts and manage disk space
- Stable internet connection is crucial for FTP transfers
- Transfer during off-peak hours for better server performance
- Use verbose mode (
--verbose) for debugging performance issues
- Automatic Retry Logic: Connections and transfers automatically retry with exponential backoff
- Better Error Recovery: More robust handling of network interruptions and timeouts
- Connection Optimization: FTP connections use passive mode and binary mode for better compatibility
- Optimized Buffer Sizes: 8KB transfer buffers for optimal throughput
- MLSD Support: Uses modern FTP MLSD command when available for faster directory listing
- Better Progress Tracking: Real-time progress updates during transfers
- Chunked Transfer Implementation: Properly implemented chunked transfer for large directories
- Enhanced Logging: Structured, informative logging with timing information
- Better Validation: Input validation and environment variable checking
- Type Hints: Improved code documentation and IDE support
Run the test suite to verify enhancements:
python test_enhancements.pyThis will test:
- Connection retry logic
- File transfer retry mechanisms
- Progress tracking
- Error handling
- Optimization features
- FTP transfers are not encrypted by default
- Credentials are sent in plain text over FTP
- Consider using secure hosting providers with proper network security
- This tool is designed for trusted server-to-server transfers
- This version does not require SSH access or external libraries - uses only built-in Python modules
- All transfers use standard FTP protocol (RFC 959)
- Temporary files are created in
tmp_trans/directory and automatically cleaned up after each transfer - The tool automatically handles directory structures and file permissions available via FTP
- Cross-platform compatibility with Windows, macOS, and Linux
- Transfer results are automatically saved to
transfers_results.csvin the current directory - Pipenv automatically loads .env files - no need for manual environment configuration when using
pipenv runorpipenv shell