Skip to content

Instantly share code, notes, and snippets.

@mouradev
Created June 4, 2019 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mouradev/b4fc799d5cf8e2a195d7644cc0aba232 to your computer and use it in GitHub Desktop.
Save mouradev/b4fc799d5cf8e2a195d7644cc0aba232 to your computer and use it in GitHub Desktop.
This script can help you to replace a Wordpress site url in the database
#!/bin/bash
# Bash script written by Lucas Moura mouradev.com
mysql_host="localhost"
read -p "Please enter the MySQL root user: " db_user
read -p "Please enter root user MySQL password: " root_password
read -p "Please enter the database name: " db_name
read -p "Please enter the database prefix (blank to no prefix): " db_prefix
echo ""
read -p "Do you want to run Wordpress url sql replace? (y/n) " sql_replace
if [ "$sql_replace" != "${sql_replace#[Yy]}" ] ;then
read -p "Please enter the old url (with 'http://'): " old_url
read -p "Please enter the NEW url (with 'http://'): " new_url
echo "Running sql replace..."
mysql -u${db_user} -p${root_password} ${db_name} -e "UPDATE ${db_prefix}options SET option_value = replace(option_value, '${old_url}','${new_url}') WHERE option_name = 'home' OR option_name = 'siteurl';UPDATE ${db_prefix}posts SET guid = replace(guid, '${old_url}','${new_url}');UPDATE ${db_prefix}posts SET post_content = replace(post_content, '${old_url}', '${new_url}');UPDATE ${db_prefix}postmeta SET meta_value = replace(meta_value, '${old_url}', '${new_url}');"
fi
echo "Your database is ready :)"
exit
@mouradev
Copy link
Author

mouradev commented Jun 4, 2019

Do not forget to give execute permission to this file chmod +x sql_replace.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment