{"id":3675,"date":"2019-06-21T19:11:14","date_gmt":"2019-06-21T07:11:14","guid":{"rendered":"http:\/\/www.zoyinc.com\/?p=3675"},"modified":"2019-06-22T23:30:25","modified_gmt":"2019-06-22T11:30:25","slug":"backup-and-restore-wordpress","status":"publish","type":"post","link":"http:\/\/www.zoyinc.com\/?p=3675","title":{"rendered":"Backup and restore WordPress"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3694\" src=\"http:\/\/www.zoyinc.com\/wp-content\/uploads\/2019\/06\/BackupAndRestoreWordPress02.jpg\" alt=\"\" width=\"2048\" height=\"249\" srcset=\"http:\/\/www.zoyinc.com\/wp-content\/uploads\/2019\/06\/BackupAndRestoreWordPress02.jpg 2048w, http:\/\/www.zoyinc.com\/wp-content\/uploads\/2019\/06\/BackupAndRestoreWordPress02-150x18.jpg 150w, http:\/\/www.zoyinc.com\/wp-content\/uploads\/2019\/06\/BackupAndRestoreWordPress02-300x36.jpg 300w, http:\/\/www.zoyinc.com\/wp-content\/uploads\/2019\/06\/BackupAndRestoreWordPress02-768x93.jpg 768w, http:\/\/www.zoyinc.com\/wp-content\/uploads\/2019\/06\/BackupAndRestoreWordPress02-500x61.jpg 500w\" sizes=\"auto, (max-width: 2048px) 100vw, 2048px\" \/>I run WordPress on Apache 2.2 on RedHat. A WordPress instance, is just a bunch of PHP files, various image files and uploaded files and a database &#8211; that&#8217;s pretty much it. So to backup and restore you need to back up one directory tree and a DB.<\/p>\n<p>This post details what I did to go from a backup of WordPress on one server to a completely new server. The old host was PHP 5.4 and Apache httpd 2.4.4-3. The new server is PHP 7.2 and Apache httpd 2.4.6-88. The original host is Fedora 18, August 2013 and the new one is CentOS 7 June 2019 &#8211; so six years difference.<\/p>\n<p>When I am doing this restore I am doing it as root because if I really screw it up it&#8217;s only a VM and I can roll back. Clearly on an important server you would do as much as possible as a non-root user.<\/p>\n<h2>Resources<\/h2>\n<p>Backup shell script for folders:<a  href=\"http:\/\/www.zoyinc.com\/wp-content\/uploads\/2019\/06\/daily_backup.sh_-1.zip\">daily_backup.sh<\/a><br \/>\nBackup MySQL databases: <a  href=\"http:\/\/www.zoyinc.com\/wp-content\/uploads\/2019\/06\/mysql_dumps.sh_.v2-1.zip\">mysql_dumps.sh.v2<\/a><\/p>\n<h2>Backup<\/h2>\n<p>I have two backup scripts that run on my servers, one for backing up the folders &#8220;daily_backup.sh&#8221; and another for backing up the DB &#8220;mysql_dumps.sh&#8221; &#8211; both can be download for you to review &#8211; see &#8220;Resources&#8221; section of this post.<\/p>\n<p>The folder backup includes backing up the wordpress folder &#8220;\/srv\/wordpress&#8221; and the apache config &#8220;\/etc\/httpd&#8221;. The other folders in the example script, like &#8220;\/svr\/conf&#8221; are non-WordPress folders.<\/p>\n<p>The MySQL backup simply backs up the database by running:<\/p>\n<pre class=\"crayon:false;\">mysqldump -f -u${DBUSER[$i]} -p${DBPASSWD[$i]} ${DBNAME[$i]} &gt; $ARCHIVE_NAME<\/pre>\n<p>Then the dump is zipped up.<\/p>\n<h2>Restoring the backups<\/h2>\n<h3>Restoring the DB<\/h3>\n<p>First thing to do is the DB. I have copied the DB dump to:<\/p>\n<pre class=\"crayon:false;\">\/srv\/backup\/MySQL_DB_Dump_#0_wordp_VMValiant.gz<\/pre>\n<p>So open a terminal and run the following to extract the dump file:<\/p>\n<pre class=\"crayon:false;\">gunzip -cd MySQL_DB_Dump_#0_wordp_VMValiant.gz &gt; MySQL_DB_Dump_#0_wordp_VMValiant.dmp<\/pre>\n<p>Then restore it by first logging in to the DB as root<\/p>\n<pre class=\"crayon:false;\">mysql -u root -p<\/pre>\n<p>Then run the following:<\/p>\n<pre class=\"crayon:false;\">drop database wordpress; -- Drop the database, if it exists and is called 'wordpress'\r\ndrop user wordp; -- Delete the user 'wordp'\r\ncreate user wordp identified by '&lt;my secret password&gt;'; -- Create DB user\r\ncreate database wordpress; -- Create the DB called 'wordpress'\r\ngrant all privileges on wordpress.* to 'wordp'@'%'; -- Give the user full rights to the DB from any location<\/pre>\n<p>Now that your DB is setup run the following to restore the DB dump:<\/p>\n<pre class=\"crayon:false;\">mysql -f -u wordp -p wordpress&lt; \/srv\/backup\/MySQL_DB_Dump_#0_wordp_VMValiant.dmp<\/pre>\n<h3>Restoring the file system<\/h3>\n<p>In my case the backup script I run creates a structure like:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3689\" src=\"http:\/\/www.zoyinc.com\/wp-content\/uploads\/2019\/06\/BackupAndRestoreWordPress01.jpg\" alt=\"\" width=\"250\" height=\"132\" srcset=\"http:\/\/www.zoyinc.com\/wp-content\/uploads\/2019\/06\/BackupAndRestoreWordPress01.jpg 250w, http:\/\/www.zoyinc.com\/wp-content\/uploads\/2019\/06\/BackupAndRestoreWordPress01-150x79.jpg 150w\" sizes=\"auto, (max-width: 250px) 100vw, 250px\" \/><\/p>\n<p>To be safe we will extract to a temp location. So the file is in &#8220;\/svr\/backup&#8221; so I run:<\/p>\n<pre class=\"crayon:false;\">cd \/srv\/backup\r\ntar xvzf WordPress_WWW_Dir_on_VMValiant.tar.gz<\/pre>\n<p>You need to copy the folder to where you are putting the files, so it is most likely different for you but for me:<\/p>\n<pre class=\"crayon:false;\">mkdir -p \/srv\/www\r\ncd \/srv\/backup\/srv\/www\r\ncp -r wordpress \/srv\/www\r\ncd \/srv\r\nchown -R apache.apache www<\/pre>\n<p>Obvious the last part is ensuring apache has access to the folders. Because it was a tar copy the permissions will already be correct.<\/p>\n<h3>Fix the configs<\/h3>\n<p>Firstly we need to fix the DB details etc. These are contained in the php config file, in my case:<\/p>\n<pre class=\"crayon:false; \">\/srv\/www\/wordpress\/wp-config.php<\/pre>\n<p>Edit this file and change any DB details as required.<\/p>\n<p>Next on the list is the Apache config file. One of the side tasks things my backup script does is to backup the Apache folders under &#8220;\/etc\/httpd&#8221; which brings httpd.conf along with the ssl.conf and any certificates.<\/p>\n<p>When I upgrade Apache or JBoss or anything like that I find the best thing is do a compare between the out-of-the-box for the new instance against the original config &#8211; using a tool like BeyondCompare or kdiff. In this case the versions are both on Apache 2.4.x and comparing the configs I can still keep using the old one, so great.<\/p>\n<p>So just copy of the following to the new Apache folder:<\/p>\n<pre class=\"crayon:false;\">etc\\httpd\\conf.d\\zoyinc.com.pem\r\netc\\httpd\\conf.d\\zoyinc.com.cacert.pem\r\netc\\httpd\\conf.d\\zoyinc.com.v2.key\r\netc\\httpd\\conf.d\\zoyinc.com.v2.crt\r\netc\\httpd\\conf.d\\ssl.conf\r\netc\\httpd\\conf\\httpd.conf<\/pre>\n<p>Its really important to have everything in the same directories as they were in the old server otherwise you will need to carefully go over all the configs to see if there is anything you need to change.<\/p>\n<p>Also don&#8217;t simply take all files across because that won&#8217;t work either.<\/p>\n<pre class=\"crayon:false;\">systemctl stop httpd.service\r\ncd \/var\/tmp\r\ntar xvzf HTTPD_Conf_Dir_on_VMValiant.tar.gz\r\n\r\nmv \/etc\/httpd\/conf.d\/zoyinc.com.pem \/etc\/httpd\/conf.d\/zoyinc.com.pem.ootb\r\nmv \/etc\/httpd\/conf.d\/zoyinc.com.cacert.pem \/etc\/httpd\/conf.d\/zoyinc.com.cacert.pem.ootb\r\nmv \/etc\/httpd\/conf.d\/zoyinc.com.v2.key \/etc\/httpd\/conf.d\/zoyinc.com.v2.key.ootb\r\nmv \/etc\/httpd\/conf.d\/zoyinc.com.v2.crt \/etc\/httpd\/conf.d\/zoyinc.com.v2.crt.ootb\r\nmv \/etc\/httpd\/conf.d\/ssl.conf \/etc\/httpd\/conf.d\/ssl.conf.ootb\r\nmv \/etc\/httpd\/conf\/httpd.conf\u00a0 \/etc\/httpd\/conf\/httpd.conf.ootb\r\n\r\ncp etc\/httpd\/conf.d\/zoyinc.com.pem \/etc\/httpd\/conf.d\/zoyinc.com.pem\r\ncp etc\/httpd\/conf.d\/zoyinc.com.cacert.pem \/etc\/httpd\/conf.d\/zoyinc.com.cacert.pem\r\ncp etc\/httpd\/conf.d\/zoyinc.com.v2.key \/etc\/httpd\/conf.d\/zoyinc.com.v2.key\r\ncp etc\/httpd\/conf.d\/zoyinc.com.v2.crt \/etc\/httpd\/conf.d\/zoyinc.com.v2.crt\r\ncp etc\/httpd\/conf.d\/ssl.conf \/etc\/httpd\/conf.d\/ssl.conf\r\ncp etc\/httpd\/conf\/httpd.conf\u00a0 \/etc\/httpd\/conf\/httpd.conf<\/pre>\n<h3>Restart Apache<\/h3>\n<p>Simply run:<\/p>\n<pre class=\"crayon:false;\">systemctl start httpd.service<\/pre>\n<p>To start Apache and you are done<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I run WordPress on Apache 2.2 on RedHat. A WordPress instance, is just a bunch of PHP files, various image files and uploaded files and a database &#8211; that&#8217;s pretty much it. So to backup and restore you need to back up one directory tree and a DB. This post details what I did to [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":3696,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[537,536,10],"tags":[327,542,540,235,539,538,541,497,297],"class_list":["post-3675","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mariadb","category-mysql","category-wordpress","tag-backup","tag-export","tag-file-system","tag-import","tag-mariadb","tag-mysql","tag-recovery","tag-restore","tag-wordpress"],"_links":{"self":[{"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=\/wp\/v2\/posts\/3675","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3675"}],"version-history":[{"count":14,"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=\/wp\/v2\/posts\/3675\/revisions"}],"predecessor-version":[{"id":4758,"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=\/wp\/v2\/posts\/3675\/revisions\/4758"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=\/wp\/v2\/media\/3696"}],"wp:attachment":[{"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3675"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3675"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3675"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}