Skip to content

Instantly share code, notes, and snippets.

@zoltanctoth
Last active September 25, 2015 06:26
Show Gist options
  • Save zoltanctoth/e9cbd83a1861b352da92 to your computer and use it in GitHub Desktop.
Save zoltanctoth/e9cbd83a1861b352da92 to your computer and use it in GitHub Desktop.
Moving wordpress to an other domain can be a hassle. Here is a script on how to do it in without the pain.
#!/bin/bash -xeu
# This script moves your wordrpress page under a different domain
# Zoltan C. Toth
export HISTCONTROL=ignorespace
ORIGIN_DOMAIN=teszt2.gyulahus.hu
TARGET_DOMAIN=teszt.gyulahus.hu
ORIGIN_DIR=/home/gyulahus/public_html/$ORIGIN_DOMAIN
TARGET_DIR=/home/gyulahus/public_html/$TARGET_DOMAIN
TARGET_DB=teszt2_gyh
# get MySQL Credentials from the original wordpress site
$(
echo -e '<?php\n' $(cat $ORIGIN_DIR/wp-config.php | grep DB_) '
echo "export MYSQL_USER=".DB_USER."
export MYSQL_PASSWORD=".DB_PASSWORD."
export MYSQL_DB=".DB_NAME
?>' | php
)
mkdir -pf db_dumps
DUMP=db_dumps/gyulahus-$ORIGIN_DOMAIN.dump
mysqldump -u$MYSQL_USER -p$MYSQL_PASSWORD $MYSQL_DB > $DUMP
# Set up site in a new directory
rm -rf $TARGET_DIR/*
cp -ra $ORIGIN_DIR/. $TARGET_DIR/
sed -i "s|define('DB_NAME', '$MYSQL_DB');|define('DB_NAME', '$TARGET_DB');|" $TARGET_DIR/wp-config.php
mysql -u$MYSQL_USER -p$MYSQL_PASSWORD -Nse 'show tables' $TARGET_DB | \
while read table; do mysql -u$MYSQL_USER -p$MYSQL_PASSWORD -e "drop table $table" $TARGET_DB; done
mysql -u$MYSQL_USER -p$MYSQL_PASSWORD $TARGET_DB < $DUMP
# Migrate site to an other domain
# `wp` command: http://wp-cli.org/
## Migrate DB
wp --path=$TARGET_DIR search-replace $ORIGIN_DOMAIN $TARGET_DOMAIN
wp --path=$TARGET_DIR cache flush
## Migrate file
find $TARGET_DIR -iname '*.cache' | xargs rm -f
fgrep -lr $ORIGIN_DOMAIN $TARGET_DIR | xargs sed -i "s|$ORIGIN_DOMAIN|$TARGET_DOMAIN|g"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment