A Bash script for synchronizing a WordPress site between a remote Hostinger-hosted environment and a local DDEV-managed environment. The script supports bidirectional syncing: remote-to-local (Hostinger to DDEV) and local-to-remote (DDEV to Hostinger). It syncs the wp-content
directory, exports and imports the database, updates URLs, and handles caches, with robust error handling and backup creation.
- Bidirectional Sync: Choose to sync from Hostinger to DDEV or from DDEV to Hostinger via an interactive prompt.
- Secure Configuration: Uses a
.env
file to store sensitive data like SSH and database credentials. - Efficient Syncing: Uses
rsync
with exclusions (e.g., cache, logs) to optimize file transfers. - Database Management: Exports and imports databases, with backups stored locally.
- URL Updates: Updates WordPress URLs (
siteurl
,home
, and content) to match the target environment. - Robustness: Includes dependency checks, error handling, and color-coded logging.
- Safety: Prompts for confirmation before overwriting data and creates backups of databases.
- Hostinger Compatibility: Configured for Hostinger’s SSH port (65002) and typical directory structure.
- DDEV Integration: Optimized for DDEV, with automatic project status checks and
wp-cli
usage.
- Local Environment (Linux):
- Tools:
rsync
,ssh
,scp
,mysql
,ddev
,wp-cli
. - Install dependencies:
sudo apt install openssh-client rsync mysql-client
- Install WP-CLI: Follow WP-CLI installation guide.
- Install DDEV: Follow DDEV installation guide.
- Tools:
- Remote Environment (Hostinger):
- SSH access enabled (Hostinger control panel: "Advanced" > "SSH Access").
mysqldump
andmysql
available (typically pre-installed).- Database credentials (from Hostinger control panel: "Databases" > "MySQL Databases").
- DDEV Project: A WordPress site configured in DDEV (run
ddev list
to verify). - SSH Key: Set up for passwordless SSH to Hostinger:
ssh-copy-id -p 65002 <REMOTE_USER>@<REMOTE_HOST>
-
Clone or Download:
- Save the script as
wp-sync.sh
in your local DDEV project directory (e.g.,~/projects/my-wordpress-site
). - Make it executable:
chmod +x wp-sync.sh
- Save the script as
-
Create
.env
File:- Create a
.env
file in the same directory aswp-sync.sh
with the following content:# Hostinger SSH settings REMOTE_USER="u123456789" # Hostinger SSH username REMOTE_HOST="your-domain.com" # Hostinger domain or IP REMOTE_WP_PATH="/home/u123456789/domains/your-domain.com/public_html" # Path to WordPress root SSH_PORT="65002" # Hostinger default SSH port # Remote database credentials (from Hostinger control panel) REMOTE_DB_NAME="u123456789_dbname" # Database name REMOTE_DB_USER="u123456789_dbuser" # Database user REMOTE_DB_PASS="your_db_password" # Database password REMOTE_DB_HOST="mysql.hostinger.com" # Database host # Local DDEV settings LOCAL_PROJECT_NAME="my-wordpress-site" # DDEV project name
- Replace placeholders with your actual Hostinger and DDEV details.
- Secure the
.env
file:chmod 600 .env echo ".env" >> .gitignore
- Create a
-
Verify DDEV Project:
- Ensure your DDEV project is set up and running:
ddev start ddev list
- Ensure your DDEV project is set up and running:
-
Run the Script:
- Navigate to the directory containing
wp-sync.sh
and.env
:cd ~/projects/my-wordpress-site ./wp-sync.sh
- Navigate to the directory containing
-
Choose Sync Direction:
- When prompted, select:
1
for remote-to-local (Hostinger to DDEV).2
for local-to-remote (DDEV to Hostinger).
- Example prompt:
[INFO] Select sync direction: 1) Remote to Local (Hostinger to DDEV) 2) Local to Remote (DDEV to Hostinger) Enter choice (1 or 2):
- When prompted, select:
-
Confirm Overwrite:
- For remote-to-local: Confirms overwrite of local database and files.
- For local-to-remote: Warns about overwriting live site data.
- Enter
y
to proceed orn
to cancel.
-
Monitor Output:
- Follow the color-coded logs (blue for info, green for success, yellow for warnings, red for errors).
- Backups are stored in
./backups/
with timestamps. - A summary provides the target URL and next steps.
-
Verify Sync:
- Remote-to-Local: Visit
https://<LOCAL_PROJECT_NAME>.ddev.site
to verify the local site. - Local-to-Remote: Visit
https://<REMOTE_HOST>
to verify the live site. - Check for mixed content warnings or broken links.
- Backups are stored in
./backups/
for recovery if needed.
- Remote-to-Local: Visit
.env
Variables:REMOTE_USER
: Hostinger SSH username (e.g.,u123456789
).REMOTE_HOST
: Hostinger domain or IP (e.g.,your-domain.com
).REMOTE_WP_PATH
: Path to WordPress root on Hostinger (e.g.,/home/u123456789/domains/your-domain.com/public_html
).SSH_PORT
: Hostinger SSH port (default:65002
).REMOTE_DB_NAME
,REMOTE_DB_USER
,REMOTE_DB_PASS
,REMOTE_DB_HOST
: Database credentials from Hostinger control panel.LOCAL_PROJECT_NAME
: DDEV project name (e.g.,my-wordpress-site
).
- Paths:
- The script assumes the local DDEV project root is the current directory (
./
). - The
wp-content
directory is synced between$REMOTE_WP_PATH/wp-content/
and./wp-content/
. - Database backups are stored in
./backups/
.
- The script assumes the local DDEV project root is the current directory (
- URLs:
- Remote-to-local: Updates URLs from
https://<REMOTE_HOST>
tohttps://<LOCAL_PROJECT_NAME>.ddev.site
. - Local-to-remote: Updates URLs from
https://<LOCAL_PROJECT_NAME>.ddev.site
tohttps://<REMOTE_HOST>
.
- Remote-to-local: Updates URLs from
- Security:
- Keep the
.env
file secure and excluded from version control. - Use SSH keys for passwordless authentication to avoid storing passwords.
- Keep the
- Performance:
- Large
wp-content
directories or databases may take time to sync. Exclusions (e.g.,cache/*
,*.log
) reduce transfer size. - Consider compressing database dumps with
gzip
for faster transfers if needed.
- Large
- Hostinger Restrictions:
- Verify SSH port and database host in Hostinger’s control panel, as they may vary.
- Shared hosting may limit shell access; ensure SSH is enabled.
- Testing:
- Test the script on a staging site (both local and remote) to avoid data loss.
- Verify backups in
./backups/
before running, especially for local-to-remote syncs.
- WP-CLI on Hostinger:
- The script assumes
wp-cli
is unavailable on Hostinger and uses SQL queries for URL updates in local-to-remote syncs. - If
wp-cli
is installed, modify theupdate_urls
function to use:ssh -p "$SSH_PORT" "$REMOTE_USER@$REMOTE_HOST" "wp --path='$REMOTE_WP_PATH' search-replace '$LOCAL_URL' '$REMOTE_URL' --all-tables"
- The script assumes
- SSH Connection Issues:
- Verify SSH access:
ssh -p 65002 <REMOTE_USER>@<REMOTE_HOST>
. - Ensure SSH key is set up or password authentication is enabled.
- Verify SSH access:
- Database Errors:
- Check database credentials in
.env
and Hostinger control panel. - Ensure
REMOTE_DB_HOST
is correct (e.g.,mysql.hostinger.com
).
- Check database credentials in
- DDEV Issues:
- Run
ddev describe
to verify project configuration. - Ensure the DDEV project is running (
ddev start
).
- Run
- Sync Failures:
- Check logs for errors (red text).
- Verify write permissions for
wp-content
directories and database access. - Restore from backups in
./backups/
if needed.
Feel free to submit issues or pull requests to improve the script. Suggestions for additional features (e.g., dynamic URL detection, support for multiple sites) are welcome.
This script is provided under the MIT License. Use it freely, but at your own risk.
Generated on July 22, 2025
### Instructions
1. **Save the README**:
- Save the above content as `README.md` in the same directory as `wp-sync.sh` and `.env`.
- This ensures users can access documentation alongside the script.
2. **Customize if Needed**:
- Update the `Contributing` or `License` sections to match your project’s requirements.
- Add a `LICENSE` file if you choose a specific license (e.g., MIT).
3. **Host on Repository**:
- If using GitHub/GitLab, push `README.md`, `wp-sync.sh`, and ensure `.env` is excluded via `.gitignore`.
- The Markdown format ensures proper rendering on repository platforms.
If you need adjustments (e.g., adding specific usage examples, project-specific details, or a different license), let me know!