MySql / Wordpress Copy / Clone via shell scrupt using MsSqlDump and file system copy

I want to be able to make a "clone" of a wordpress install on linix machine using a script to copy datatabase and subdirectory. The script should contain 4 variaibles: SourceWebsite, DestinationWebsite, SourceDB, DestinationDB.  The script assume I have root shell access.

1. Run MySqlDump to create backup dump file of {SourceDB}
2. Create an empty MqSQL database called {DestinationDB}
3. Restore the contents of dump file to database {DestinationDB}
4. Run the following (dynamically contructed) SQL statement on new Destination DB:
     "update wp_custom_options set websitedir=' " + {DestinationWebsite} +" ' where sitename=" + {SourceWebsite}
5. Copy all files/folders AND permisions from directory "\home\public_html\" + {SourceWebsite}
  to "\home\public_html\" + {DesinationWebsite}
6. Find all occurances of text equal to {SourceDB} in a file called "\home\public_html\" +{DestinationWebsite}+ "wp-config.php" and replace with text {DestinationDB}
Louis CapeceAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
gr8gonzoConnect With a Mentor ConsultantCommented:
All you shouldd have to do is create a bash file and put all your commands into it in the right order:

Contents of /root/cloneWordpress.sh:

#!/bin/bash

# Dump the database
mysqldump -Q -usample_user -pSecretPassword123 originalWPDatabase > clone.sql

# Search and replace
sed -i 's/oldDB/newDB/g' clone.sql

# Create the empty new database
cp -p setupDatabase.sql.template setupDatabase.sql
sed -i 's/---DATABASE---/newDB/g' setupDatabase.sql
sed -i 's/---USER---/sample_user/g' setupDatabase.sql
mysql -usample_user -pSecretPassword123 < setupDatabase.sql

# Populate the new database
mysql -usample_user -pSecretPassword123 newWPDatabase < clone.sql

# Copy the files
cp -Rp /home/public_html/sourceWebsite /home/public_html/destinationWebsite

# Update the config file
sed -i 's/oldDB/newDB/g' /home/public_html/destinationWebsite/wp-config.php



Then, make the above /root/cloneWordpress.sh file executable with chmod and run it. But DO NOT use the above file as-is. You still need to make the appropriate changes and search-and-replaces (ESPECIALLY change the database name in the dumped SQL file so you don't end up dropping your existing database when you load in clone.sql ). But that should at least give you 90% of what you need to do.
0
 
gr8gonzoConsultantCommented:
So what's the question? You've listed off all the steps, so it seems like you know how to do it...?
0
 
Louis CapeceAuthor Commented:
Need help to actually use the commands, for example, I do not know know to specify the parameters for MySqlDump, do not know you to cycle thru file tree, etc
0
 
gr8gonzoConsultantCommented:
Well, I would probably do this all in a bash script, if I were you. I can't give you the full script without knowing all the different pieces on your system, but it's better if you're the one to put it together anyway. Here are the puzzle pieces:

(I'm also using an example username/password combo of sample_user / SecretPassword123)

1. To dump a database called originalWPDatabase in MySQLDump into a SQL file called clone.sql:
mysqldump -Q -usample_user -pSecretPassword123 originalWPDatabase > clone.sql

2. To perform a search-and-replace on clone.sql to replace all occurrences of oldDB with newDB:
sed -i 's/oldDB/newDB/g' clone.sql

(Note: This works for any file and any keywords, so you can update your config file this way)

3. To load a SQL file called clone.sql into the database:
mysql -usample_user -pSecretPassword123 newWPDatabase < clone.sql

4. To copy a directory structure:
cp -Rp /home/public_html/sourceWebsite /home/public_html/destinationWebsite

A note about #3 - you should probably consider having another SQL file that creates the new database and grants the appropriate permissions on it to sample_user. It can be a template file that you can just reuse:

setupDatabase.sql.template:
CREATE DATABASE ---DATABASE---;
GRANT ALL ON ---DATABASE---.*  TO '---USER---'@'localhost';

Script to copy and search&replace:
cp -p setupDatabase.sql.template setupDatabase.sql
sed -i 's/---DATABASE---/newDB/g' setupDatabase.sql
sed -i 's/---USER---/sample_user/g' setupDatabase.sql
mysql -usample_user -pSecretPassword123 < setupDatabase.sql

0
All Courses

From novice to tech pro — start learning today.