{"id":22367161,"url":"https://github.com/sukhoy94/sync-wp-database-bash","last_synced_at":"2025-10-04T11:16:06.850Z","repository":{"id":237490018,"uuid":"794538637","full_name":"sukhoy94/sync-wp-database-bash","owner":"sukhoy94","description":"Scripts for syncing remote and local wordpress databases","archived":false,"fork":false,"pushed_at":"2024-05-01T12:42:54.000Z","size":168,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-31T17:13:20.945Z","etag":null,"topics":["bash-scripting","devops"],"latest_commit_sha":null,"homepage":"","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sukhoy94.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-05-01T12:12:47.000Z","updated_at":"2024-05-18T17:57:45.000Z","dependencies_parsed_at":null,"dependency_job_id":"1772fbf7-dcec-4173-a367-a5974f678d3a","html_url":"https://github.com/sukhoy94/sync-wp-database-bash","commit_stats":null,"previous_names":["sukhoy94/sync-wp-database-bash"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sukhoy94%2Fsync-wp-database-bash","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sukhoy94%2Fsync-wp-database-bash/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sukhoy94%2Fsync-wp-database-bash/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sukhoy94%2Fsync-wp-database-bash/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sukhoy94","download_url":"https://codeload.github.com/sukhoy94/sync-wp-database-bash/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245689451,"owners_count":20656414,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bash-scripting","devops"],"created_at":"2024-12-04T18:16:37.788Z","updated_at":"2025-10-04T11:16:01.785Z","avatar_url":"https://github.com/sukhoy94.png","language":"Shell","readme":"# Synchronize wordpress databases with bash (docker based version)\n\nHi! This project contains 2 scripts which I'm using to synchronize my wordpress docker-based projects databases in \ntwo directions: \n- sync local db (pull changes from production database)\n- sync production db (pull changes from local db to production)\n\n\n### Project structure\n\n- Makefile file with 2 commands to run:\n\n```\nmake sync_local_db\nmake sync_remote_db\n```\n\n- scripts/ folder which contains:\n```\n.env\nsync-local-db-from-production.sh\nsync-production-db-from-local.sh\n```\n### ENVs description\n\n- `SSH_LOGIN`: Your SSH login username for connecting to the remote server.\n- `SSH_SERVER`: The hostname or IP address of the remote server you want to connect to via SSH.\n- `LOCAL_DB_USER`: The username of the local MySQL/MariaDB database.\n- `LOCAL_DB_PASSWORD`: The password of the local MySQL/MariaDB database.\n- `LOCAL_DB_NAME`: The name of the local MySQL/MariaDB database.\n- `MARIADB_CONTAINER_NAME`: The name of the Docker container running MySQL/MariaDB locally.\n- `REMOTE_DB_HOST`: The hostname or IP address of the remote MySQL/MariaDB database server.\n- `REMOTE_DB_USER`: The username of the remote MySQL/MariaDB database.\n- `REMOTE_DB_PASSWORD`: The password of the remote MySQL/MariaDB database.\n- `REMOTE_DB_NAME`: The name of the remote MySQL/MariaDB database.\n- `REMOTE_ADMIN_USER`: The username used for administrative tasks on the remote server, if applicable.\n- `LOCAL_HOST`: The hostname or IP address of your local machine.\n- `LOCAL_PORT`: The port number where your local server is running (e.g., for a web server).\n- `REMOTE_URL`: The URL of the remote server, which may be used for updating URLs in WordPress or similar applications.\n\n# Flow\n\n## 1. Sync local database\n\nRun:\n```\nmake sync_local_db\n```\n\n### Script Overview\n\n#### Setting Up Environment\n- Loads environment variables from a `.env` file containing database and server configurations.\n\n#### Asking for Migration Description\n- Prompts the user to enter a short description for the migration.\n\n#### Backup Directory\n- Creates a directory structure for storing backups based on the current date and time.\n\n#### Dumping Local Database\n- Creates a backup of the local MySQL database running in a Docker container.\n- Saves the backup to a file with a timestamp in the backup directory.\n\n#### Dumping Remote Database\n- Dumps the remote MySQL database directly to a file on the remote server using SSH.\n- Excludes the `wp_users` table and saves the dump to a file named `database_dump.sql` on the remote server.\n\n#### Transferring Remote Dump\n- Transfers the database dump file from the remote server to the local machine using SCP.\n- Saves the file in the script's directory.\n\n#### Importing Remote Dump Locally\n- Imports the database dump file into the local MySQL database running in a Docker container.\n\n#### Executing SQL File\n- Executes the SQL file containing the database dump in the local MySQL container.\n\n#### Updating URLs\n- Updates URLs in the `wp_posts` and `wp_options` tables of the local database to replace remote URLs with local URLs.\n\n#### Cleaning Up\n- Removes the database dump file from the remote server.\n- Moves the remote dump file to the backup directory.\n\n#### Logging Migration\n- Logs migration details, including the migration description, timestamp, and updates to a migration log file in the backup directory.\n\n#### Completing Migration\n- Prints a message indicating that the migration is complete.\n\n## 2. Sync remote database\nRun:\n```\nmake sync_remote_db\n```\n### MySQL Database Migration Script Overview\n\n#### Setting Up Environment\n- Retrieves the directory of the script and loads environment variables from a `.env` file containing database and server configurations.\n\n#### Asking for Migration Description\n- Prompts the user to enter a short description for the migration.\n\n#### Backup Directory\n- Creates a directory structure for storing backups based on the current date and time.\n\n#### Dumping Local Database\n- Dumps the local MySQL database running in a Docker container.\n- Excludes the `wp_users` table and saves the dump to a file named `local_database_dump.sql` in the backup directory.\n\n#### Transferring Local Dump to Remote Server\n- Transfers the database dump file to the remote server using SCP.\n\n#### Dumping Remote Database\n- Dumps the remote MySQL database directly to a file on the remote server using SSH.\n- Excludes the `wp_users` table and saves the dump to a file named `database_dump.sql` on the remote server.\n\n#### Importing Remote Dump\n- Connects to the SSH server and imports the database dump into the remote MySQL database.\n- Updates URLs in the `wp_posts` and `wp_options` tables of the remote database.\n- Cleans up temporary files after the import process.\n\n#### Logging Migration\n- Logs migration details, including the migration description, timestamp, and updates, to a migration log file in the backup directory.\n\n#### Completing Migration\n- Prints a message indicating that the migration is complete.\n\n\n## Backups Result Screenshot\n![Backup Screenshot](https://github.com/sukhoy94/sync-wp-database-bash/blob/main/backups_screenshot_result.jpeg)","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsukhoy94%2Fsync-wp-database-bash","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsukhoy94%2Fsync-wp-database-bash","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsukhoy94%2Fsync-wp-database-bash/lists"}