Shell/Bash Script Migrating WordPress – Find and Replace all Instance of Domain Name in WordPress or MySQL Database
Generic
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:
- 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.
- Compress the Backup: The script then compresses the SQL dump file into a
.tar.gzArchive for easy storage and retrieval. - Find and Replace Domain Instances: Utilizing the
sedCommand: the script searches for all instances of your old domain and replaces them with the new domain across the entire SQL dump file. - 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:
- Download and Prepare: Save the script to your server and make it executable using
chmod +x wpchangedomain.sh. - Configure Variables: Update the script with your MySQL username, password, database name, old domain, and new domain.
- Execute the Script: Run the script by executing
sh wpchangedomain.shin your terminal. - 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.