WordPress Clone an Instance: Difference between revisions

From WikiMLT
m (Стадий: 3 [Фаза:Разработване, Статус:Разутвърден]; Категория:WordPress)
mNo edit summary
Line 1: Line 1:
<noinclude><!--[[Category:WordPress|?]]-->{{ContentArticleHeader/WordPress}}</noinclude>
<noinclude><!--[[Category:WordPress|?]]-->{{ContentArticleHeader/WordPress}}</noinclude>
== By Using a Plugin ==
{{media|n=1|img=WordPress Migration Plugin – Duplicator.png|size=460|label=figure}}{{AutoLangSwitch}}
{{media|n=1|img=WordPress Migration Plugin – Duplicator.png|size=460|label=figure}}{{AutoLangSwitch}}
An easy and robust way to Clone an WordPress Instance is to use the WordPress migration plugin [https://wordpress.org/plugins/duplicator/ Duplicator] , provided by [http://www.snapcreek.com/duplicator/ Snap Creek]. There are few easy steps you need to do.
An easy and robust way to Clone an WordPress Instance is to use the WordPress migration plugin [https://wordpress.org/plugins/duplicator/ Duplicator] , provided by [http://www.snapcreek.com/duplicator/ Snap Creek]. There are few easy steps you need to do.
Line 17: Line 19:
</syntaxhighlight>Further, for these who don't have the pro version, they can chance the database prefix of the new instance by the help of the plugin [https://wordpress.org/plugins/brozzme-db-prefix-change/ Brozzme DB Prefix & Tools Addons] (tested on WP 5.7.2).
</syntaxhighlight>Further, for these who don't have the pro version, they can chance the database prefix of the new instance by the help of the plugin [https://wordpress.org/plugins/brozzme-db-prefix-change/ Brozzme DB Prefix & Tools Addons] (tested on WP 5.7.2).


References:
== Via the Command-line ==
 
=== Prepare a backup ===
'''1.''' Create an archive of the document root of the instance.
 
'''2.''' Export the existing database - by WP-CLI: <code>wp db export</code>. This command will export the database in SQL format.
 
=== Prepare a New Virtual Host ===
'''3.''' Prepare a new virtual host within the web server's configuration at the destination instance.
 
=== Deploy the backup ===
'''4.''' Move the archive to the destination instance and extract it to the new document root.
 
'''5.''' Prepare new empty MySQL data base and import the SQL file. Create also database user and grant it all privileges to the new database.
 
'''6.''' Import the data base in MySQL.
 
'''7.''' Edit <code>wp-config.php</code> within the new document root, change the database name, database user and its password - it its necessary.
 
'''8.''' Edit <code>functions.php</code> of the active theme and add the [https://wordpress.org/support/article/changing-the-site-url/ following lines at the beginning] of the file.<syntaxhighlight lang="php" line="1">
<?php
update_option( 'siteurl', 'https://new-fqdn.example.com' );
update_option( 'home', 'https://new-fqdn.example.com' );
 
</syntaxhighlight>'''9.''' Access the new instance via the web interface. Thus the above PHP functions will be activated and will update the site URL within the database.
 
'''10.''' Remove the lines added at the beginning of the <code>functions.php</code> file.
 
'''11.''' Use WP-CLI to do additional replaces within the database - [https://wpbeaches.com/change-wordpress-domain-url-with-wp-cli-tool/ reference].<syntaxhighlight lang="shell" line="1">
wp search-replace 'old-fqdn.example.com' 'new-fqdn.example.com' --dry-run
</syntaxhighlight>'''12.''' Use <code>grep</code>, <code>sed</code>, <code>xargs</code> to do the same replaces within the new instance document root - if this is necessary, i.e.: some extensions writes their in their cache hardheaded URLs, etc.
 
'''13.''' Login to the new WordPress instance and inspect weather everything is correct. If you do not have an account you can create on via WP-CLI: <code>sudo -u www-data wp user create newUser example@email.com --role=administrator</code>


== References ==
*[https://snapcreek.com/duplicator/docs/quick-start/?utm_source=duplicator_free&utm_medium=wordpress_plugin&utm_content=package_built_install_help&utm_campaign=duplicator_free#quick-040-q Duplicator For WordPress - Quick Start]
*[https://snapcreek.com/duplicator/docs/quick-start/?utm_source=duplicator_free&utm_medium=wordpress_plugin&utm_content=package_built_install_help&utm_campaign=duplicator_free#quick-040-q Duplicator For WordPress - Quick Start]
* [https://wordpress.org/plugins/brozzme-db-prefix-change/ WP Staging: 3 Ways to Change the WordPress Database Table Prefix]
* WP Staging: [https://wordpress.org/plugins/brozzme-db-prefix-change/ 3 Ways to Change the WordPress Database Table Prefix]
* [https://wordpress.org/support/article/changing-the-site-url/ WordPress: Changing The Site URL] via [https://wordpress.org/support/article/changing-the-site-url/#wp-cli '''WP-CLI''']
* WordPress: [https://wordpress.org/support/article/changing-the-site-url/ Changing The Site URL] via [https://wordpress.org/support/article/changing-the-site-url/#wp-cli '''WP-CLI''']
* WP Beaches: [https://wpbeaches.com/change-wordpress-domain-url-with-wp-cli-tool/ '''Change WordPress Domain URL with WP-CLI Tool''']


<noinclude>
<noinclude>

Revision as of 09:27, 9 September 2022

By Us­ing a Plu­g­in

Figure 1. Du­pli­ca­tor – Word­Press Mi­gra­tion Plu­g­in – Snap Creek.

An easy and ro­bust way to Clone an Word­Press In­stance is to use the Word­Press mi­gra­tion plu­g­in Du­pli­ca­tor , pro­vid­ed by Snap Creek. There are few easy steps you need to do.

  1. In­stall the plu­g­in at the source Word­Press site.
  2. Go to the ad­min menu item "Du­pli­ca­tor" and pass through the three easy steps process in or­der to "Cre­ate new Pack­age".
  3. Down­load the new­ly cre­at­ed <package>.zip and the installer.php file.
  4. Set­up a new Vir­tu­al Host and new a Data­base for the des­ti­na­tion site.
  5. Place <package>.zip and installer.php in­to the Doc­u­men­t­Root di­rec­to­ry of the new Vir­tu­al Host.
  6. Ac­cess https://​destination​.example​.com/​i​n​s​t​a​l​l​e​r.php via the web brows­er and fol­low the pro­vid­ed steps.

Op­tion­al­ly, if you need to clone the site at the same serv­er, yo do not need to down­load the <package>.zip file, it is lo­cat­ed in­to the di­rec­to­ry wp-con­tent/back­ups-dup-lite/.

One you fin­ish you must cleanup the source and the des­ti­na­tion site di­rec­to­ries. One way is by us­ing the com­mand line:

# remove 'ech' in order to do the actual changes
sudo find /var/www -type f -name '<package>.*' -exec echo rm {} \;

Fur­ther, for these who don't have the pro ver­sion, they can chance the data­base pre­fix of the new in­stance by the help of the plu­g­in Brozzme DB Pre­fix & Tools Ad­dons (test­ed on WP 5.7.2).

Via the Com­mand-line

Pre­pare a back­up

1. Cre­ate an archive of the doc­u­ment root of the in­stance.

2. Ex­port the ex­ist­ing data­base – by WP-CLI: wp db ex­port. This com­mand will ex­port the data­base in SQL for­mat.

Pre­pare a New Vir­tu­al Host

3. Pre­pare a new vir­tu­al host with­in the web server's con­fig­u­ra­tion at the des­ti­na­tion in­stance.

De­ploy the back­up

4. Move the archive to the des­ti­na­tion in­stance and ex­tract it to the new doc­u­ment root.

5. Pre­pare new emp­ty MySQL da­ta base and im­port the SQL file. Cre­ate al­so data­base user and grant it all priv­i­leges to the new data­base.

6. Im­port the da­ta base in MySQL.

7. Ed­it wp-config.php with­in the new doc­u­ment root, change the data­base name, data­base user and its pass­word – it its nec­es­sary.

8. Ed­it functions.php of the ac­tive theme and add the fol­low­ing lines at the be­gin­ning of the file.

<?php
update_option( 'siteurl', 'https://new-fqdn.example.com' );
update_option( 'home', 'https://new-fqdn.example.com' );

9. Ac­cess the new in­stance via the web in­ter­face. Thus the above PHP func­tions will be ac­ti­vat­ed and will up­date the site URL with­in the data­base.

10. Re­move the lines added at the be­gin­ning of the functions.php file.

11. Use WP-CLI to do ad­di­tion­al re­places with­in the data­base – ref­er­ence.

wp search-replace 'old-fqdn.example.com' 'new-fqdn.example.com' --dry-run

12. Use grep, sed, xargs to do the same re­places with­in the new in­stance doc­u­ment root – if this is nec­es­sary, i.e.: some ex­ten­sions writes their in their cache hard­head­ed URLs, etc.

13. Lo­gin to the new Word­Press in­stance and in­spect weath­er every­thing is cor­rect. If you do not have an ac­count you can cre­ate on via WP-CLI: su­do ‑u www-da­ta wp user cre­ate newUser example@email.com –role=administrator

Ref­er­ences