Link to home
Start Free TrialLog in
Avatar of Louis Capece
Louis Capece

asked on

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}
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

So what's the question? You've listed off all the steps, so it seems like you know how to do it...?
Avatar of Louis Capece
Louis Capece

ASKER

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
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

ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial