Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Moving Wordpress: Update SQL Statements
#!/bin/bash
#
# This script is used to update database url entries for wordpress.
#
# © Nick Böcker <https://nick-hat-boecker.de> 2019
# User input mysql username
read -p "Enter mySQL username [root]: " USER
USER=${USER:-root}
# User input mysql database
read -p "Enter database name: " DATABASE
# User input mysql database prefix
read -p "Enter database prefix [wp_]: " PREFIX
PREFIX=${PREFIX:-wp_}
# User input mysql new and old url
read -p "Enter old url: " OLD_URL
read -p "Enter new url: " NEW_URL
# Execute command with given credentials and parameters. Ask for password.
mysql -p --user="$USER" --database="$DATABASE" --execute="UPDATE ${PREFIX}posts SET guid = REPLACE(guid, '${OLD_URL}', '${NEW_URL}'); UPDATE ${PREFIX}posts SET post_content = REPLACE(post_content, '${OLD_URL}', '${NEW_URL}');UPDATE ${PREFIX}postmeta SET meta_value = REPLACE(meta_value, '${OLD_URL}', '${NEW_URL}'); UPDATE ${PREFIX}options SET option_value = REPLACE(option_value, '${OLD_URL}', '${NEW_URL}');"
echo 'Done'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.