Skip to content

Instantly share code, notes, and snippets.

@NickHatBoecker
Last active December 21, 2020 10:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NickHatBoecker/b65694a4bd756a80baef6741a62358c9 to your computer and use it in GitHub Desktop.
Save NickHatBoecker/b65694a4bd756a80baef6741a62358c9 to your computer and use it in GitHub Desktop.
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