Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vunb/3137113f0b749c6c30fcd17f738292f7 to your computer and use it in GitHub Desktop.
Save vunb/3137113f0b749c6c30fcd17f738292f7 to your computer and use it in GitHub Desktop.
Use this SQL script when changing domains on a WordPress site. Whether you’re moving from an old domain to a new domain or you’re changing from a development domain to a production domain this will work. __STEP1: always backup your database. __STEP2: change the ‘oldsite.com’ and ‘newsite.com’ variables to your own. __STEP3: make sure your databa…
SET @oldsite='http://oldsite.com';
SET @newsite='http://newsite.com';
UPDATE wp_options SET option_value = replace(option_value, @oldsite, @newsite) WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts SET post_content = replace(post_content, @oldsite, @newsite);
UPDATE wp_links SET link_url = replace(link_url, @oldsite, @newsite);
UPDATE wp_postmeta SET meta_value = replace(meta_value, @oldsite, @newsite);
/* only uncomment next line if you want all your current posts to post to RSS again as new */
#UPDATE wp_posts SET guid = replace(guid, @oldsite, @newsite);
@RaymondDay61
Copy link

RaymondDay61 commented Aug 20, 2024

This works super and fast. But if can add to this to copy only the changes from one WordPress to the other. Like rsync command or something.

I got a 2nd WordPress as a backup and like to copy the database to it. But auto have it do it every day. To not do it by hand and some times forget or do it wrong. So a sql script would be super to do this to only copy the little changes so will not were out a SSD.

Thank you for making this script I got a large database and it does this in like 3 or 4 sec. fast.

You made this one so I guess it would not be to hard to add a little to it.

The next post that is what I did and it works grate. Just witch there was a way to only copy the changes. Not the whole database.

@RaymondDay61
Copy link

With the help of your code I got a backup of my WordPress on another WordPress and with the help of ChatGPT made this text up to make scripts to copy all the database and media to a backup of WordPress. Here is what came up with:

A script to copy WordPress to a backup WordPress from rday1 to server. Replace them name in here with what your WordPress domin name.

curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
mv wp-cli.phar /usr/local/bin/wp
ssh-keygen -t rsa -b 4096
ssh-copy-id root@rday1
nano /root/.my.cnf

[client]
user=root
password=your_password

chmod 600 /root/.my.cnf

On rday1

nano Script on rday1: WP-copy-DB_to_server.sh

#!/bin/bash

wp db export --allow-root --path=/var/www/html backup.sql
scp "/root/backup.sql" root@server:/root/backup.sql

back on other WordPress

nano Get-WP-DB-from_rday1.sh

#!/bin/bash
wp db import /root/backup.sql --allow-root --path=/var/www/html
mysql wordpressdb < /root/wordpress-change-domain-migration.sql
rsync -avP --delete root@rday1:/var/www/html/wp-content/uploads/ /var/www/html/wp-content/uploads

chmod 700 Get-WP-DB-from_rday1.sh

nano wordpress-change-domain-migration.sql

SET @oldsite='https://rday.me/blog';
SET @newsite='http://server';
UPDATE wp_options SET option_value = replace(option_value, @oldsite, @newsite) WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts SET post_content = replace(post_content, @oldsite, @newsite);
UPDATE wp_links SET link_url = replace(link_url, @oldsite, @newsite);
UPDATE wp_postmeta SET meta_value = replace(meta_value, @oldsite, @newsite);

/* only uncomment next line if you want all your current posts to post to RSS again as new */
#UPDATE wp_posts SET guid = replace(guid, @oldsite, @newsite)

on rday1 again.

crontab -e
0 2 * * * /root/WP-copy-DB_to_server.sh

Then back on server

crontab -e

20 2 * * * /root/Get-WP-DB-from_rday1.sh

On rday1 again

ssh-copy-id
ssh-copy-id root@server

That it's.

Cane test by running this on rday1

sh WP-copy-DB_to_hp2.sh

Then on server

sh Get-WP-DB-from_rday1.sh

I any one does this post back let me know if it works.

You can easy install WordPress from a script here:

https://linux.how2shout.com/script-to-install-lamp-wordpress-on-ubuntu-20-04-lts-server-quickly-with-one-command/

@RaymondDay61
Copy link

I made a web page that shows this better.

http://wordpresscopyscript.infinityfreeapp.com/

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