# Shell/Bash Script Migrating WordPress – Find and Replace all Instance of Domain Name in WordPress or MySQL Database

Migrating your WordPress site to a new domain or switching from HTTP to HTTPS can be daunting, especially when updating every instance of your old domain…

Canonical: https://www.ruhanirabin.com/code-snippet/migrating-wordpress-find-and-replace-domain-name-shell-script/

May 31, 2024Generic

Migrating your WordPress site to a new domain or switching from HTTP to HTTPS can be daunting, especially when updating every instance of your old domain within your database. To simplify this process, we've created a powerful shell script that automates the entire procedure, ensuring a smooth and efficient transition.

## What This Script Does:

1. **Backup Your Database**: Before making any changes, the script creates a comprehensive backup of your MySQL database. This ensures that you have a fallback option in case anything goes wrong.
2. **Compress the Backup**: The script then compresses the SQL dump file into a `.tar.gz` Archive for easy storage and retrieval.
3. **Find and Replace Domain Instances**: Utilizing the `sed` Command: the script searches for all instances of your old domain and replaces them with the new domain across the entire SQL dump file.
4. **Re-import the Database**: Finally, the script re-imports the modified SQL file into your MySQL database, seamlessly updating all domain references.

## Additional Snippets

## How to Use This Script:

1. **Download and Prepare**: Save the script to your server and make it executable using `chmod +x wpchangedomain.sh`.
2. **Configure Variables**: Update the script with your MySQL username, password, database name, old domain, and new domain.
3. **Execute the Script**: Run the script by executing `sh wpchangedomain.sh` in your terminal.
4. **Verify Changes**: After the script completes, check your WordPress site to ensure all instances of the old domain have been replaced successfully.

### Benefits of Using This Script:

* **Automation**: Automates the tedious task of finding and replacing domain instances in your database.
* **Safety**: Creates a backup before making any changes, allowing for easy restoration if needed.
* **Efficiency**: Saves time and reduces the risk of errors associated with manual updates.

Migrating your WordPress domain doesn't have to be a complex and time-consuming task. With this shell script, you can ensure a smooth and efficient transition, allowing you to focus on what matters most – creating great content for your audience.

[View on GitHub](https://github.com/ruhanirabin/wp_changedomain_sh/raw/master/wp_changedomain.sh)
