• Bash
  • |
  • Shell
  • |
  • Ubuntu

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 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.

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.

Type: BASH

#!/bin/bash
#
#        Shell Script to Change all WordPress domain name OR String instances
#        After the Find and Replace operation, it will be re-imported back to DB
#
#        Can be used for:
#        1. Domain Migration
#        2. Update Domain to HTTPS
#        3. Find and Replace a string in the whole database
#      
#        ALWAYS KEEP A BACKUP OF THE DB
#
#        Basic Usage in console as: sh wpchangedomain.sh
#
#        from: www.RuhaniRabin.com
#

# Configurable variables
MYSQLUSER="mysqlusername"
MYSQLPASSWORD="mysqlpassword"
MYSQLDATABASE="mysqldatabase"
DB_BACKUP="exportfile.sql"  # include the sql extension for DB_BACKUP
DOMAIN_OLD="http://olddomain.com"
DOMAIN_NEW="https://newdomain.com"

# Backup MySQL
echo ". Dumping database to $DB_BACKUP"
mysqldump -u "$MYSQLUSER" -p"$MYSQLPASSWORD" "$MYSQLDATABASE" > "$DB_BACKUP" || { echo "ERROR: Could not dump mysql db to $DB_BACKUP"; exit 1; }

# Tar and compress backup
echo ".. Creating backup archive $DB_BACKUP.tar.gz"
tar czf "$DB_BACKUP.tar.gz" "$DB_BACKUP" || { echo "ERROR: Could not create backup archive"; exit 1; }

# Find and replace inside MySQL file
echo "... Find and Replace $DOMAIN_OLD"
echo ".... Changing it to $DOMAIN_NEW"
echo "...... Processing $DB_BACKUP"
sed -i "s|$DOMAIN_OLD|$DOMAIN_NEW|g" "$DB_BACKUP"
echo "........ Finished Processing $DB_BACKUP"

# Import MySQL
echo "........... Re-Importing database from $DB_BACKUP"
mysql -u "$MYSQLUSER" -p"$MYSQLPASSWORD" "$MYSQLDATABASE" < "$DB_BACKUP" || { echo "ERROR: Could not Import mysql db from $DB_BACKUP"; exit 1; }
echo "........... Script Finished"


More Codes

Enable Auto Update WordPress Plugins and Themes

Discover how to enable auto updates for WordPress plugins and themes with WPCodeBox. Add PHP snippets, save, and enjoy hassle-free updates!

Automatically Assign ALT Text to Images in WordPress Posts If They are Missing

Automatically assign ALT text to images in WordPress posts with WPCodeBox. Improve SEO and accessibility easily with this PHP script!

Dynamic Date Replace on WordPress Post Title, Excerpt, Content, Menu Items

“Dynamic Date Replace Plugin for WPCodeBox: Easily add dynamic date info to your WordPress site’s content, titles, and menu items with shortcodes!”