Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/arturniklewicz/wordpress-db-sync
Simple but reliable PHP script to safely sync WordPress databases between environments with proper error handling, logging, and backups.
https://github.com/arturniklewicz/wordpress-db-sync
cicd devops mariadb mysql php wordpress
Last synced: about 2 months ago
JSON representation
Simple but reliable PHP script to safely sync WordPress databases between environments with proper error handling, logging, and backups.
- Host: GitHub
- URL: https://github.com/arturniklewicz/wordpress-db-sync
- Owner: ArturNiklewicz
- Created: 2024-12-02T14:17:01.000Z (about 2 months ago)
- Default Branch: main
- Last Pushed: 2024-12-12T13:51:04.000Z (about 2 months ago)
- Last Synced: 2024-12-12T14:35:06.432Z (about 2 months ago)
- Topics: cicd, devops, mariadb, mysql, php, wordpress
- Language: PHP
- Homepage:
- Size: 30.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# WordPress Database Sync Tool
Simple but reliable PHP script to safely sync WordPress databases between environments with proper error handling, logging, and backups.## TL;DR
```bash
Edit config.php with your database credentials
chmod +x wp-sync.php# Usage
./wp-sync.php prod_to_dev # Sync production to development
./wp-sync.php dev_to_prod # Sync development to production
```## Flowchart
```mermaid
flowchart TD
A[Start Sync Process] --> B{Check Current Directory}
B -->|Not Found| C[Throw Exception:
No WordPress Installation]
B -->|Found| D[Detect WordPress Setup]
D --> E[Find wp-config.php]
E --> F[Parse WordPress Config]
F --> G[Load Sync Config]
G -->|Config Missing| H[Create Default Config]
H --> I[Exit: Edit Config First]
G -->|Config Found| J[Validate Environment]
J --> K{Check Required Commands}
K -->|Missing| L[Throw Exception:
Missing Requirements]
K -->|All Present| M{Parse Sync Direction}
M -->|Invalid| N[Throw Exception:
Invalid Direction]
M -->|Valid| O{Target is Production?}
O -->|Yes| P[Prompt for Confirmation]
P -->|Denied| Q[Cancel Sync]
P -->|Confirmed| R[Continue Sync]
O -->|No| R
R --> S[Create Target DB Backup]
S --> T[Export Source Database]
T --> U[Import to Target]
U --> V[Update Site URLs]
V --> W[Cleanup Temp Files]
W --> X[Log Success]
subgraph "Safety Checks"
Y[Excluded Tables:
wp_users
wp_usermeta]
Z[Backup Creation
Before Any Changes]
end
subgraph "Error Handling"
AA[All Operations
in Try-Catch Block]
BB[Detailed Error Logging]
CC[Cleanup on Failure]
end
```## Features
- Automatic database backups
- Safe URL replacement
- Production sync confirmation
- Error logging
- Excluded sensitive tables (users, usermeta)
- Transaction-safe dumps
- Shell argument escaping## Requirements
- PHP 7.0+
- MySQL/MariaDB
- `mysqldump` and `mysql` commands available
- Write permissions in script directory## Config Example
```php
// config.php
return [
'production' => [
'db_host' => 'production-host',
'db_name' => 'prod_wordpress',
'db_user' => 'prod_user',
'db_pass' => 'prod_pass',
'site_url' => 'https://www.example.com'
],
'development' => [
'db_host' => 'localhost',
'db_name' => 'dev_wordpress',
'db_user' => 'dev_user',
'db_pass' => 'dev_pass',
'site_url' => 'http://localhost:8080'
]
];
```## Security
- Never commit `config.php`
- Always use restricted database users
- Review excluded tables in `$excludedTables`## License
MIT