Skip to content

How to mirror a WordPress site between a web server and a desktop testbed server

How to Migrate your WordPress blog in 5 easy steps (or back it up in 2!).

  1. Archive all files from the WordPress installation:

    [bash]tar -czf /home/yoursite_files.tar.gz yoursite[/bash]

    Doing this compresses all the files in the “yoursite” directory into one file, “yousite_files.tar.gz”. This makes it easy and fast to download the entire site as one file instead of downloading each file individually.

  2. Export the WordPress database:

    [bash]mysqldump –add-drop-table -h localhost -u dbuser -pPassword wordpressdb | gzip >wordpressdb.gz[/bash]

    This command dumps your MySQL database to a text file. The pipe command, “|”, tells the OS to then channel that data into a gzipped file, “wordpressdb.gz”.

    At this point, you are done if you just wanted a backup of your WordPress site. If you ever want to restore the backup, follow steps 3 and 4. If you want to migrate or mirror your WordPress site at a different domain, do the following:

  3. Extract all files from WordPress installation onto mirror server into the desired directory.

    These are the files in “yoursite_files.tar.gz”. Use your favorite extracting program to do so. I personally use 7zip on my Windows workstation.

  4. Import WordPress DB on other computer:

    1. If this is the first time you are backing up your database, make sure to create a database and user in MySQL beforehand. This should be the same WordPress database user name and password as on your web server. Example sql:
      [sql]
      CREATE DATABASE website_db_wp;
      USE website_db_wp;
      GRANT ALL PRIVILEGES ON website_db_wp.* TO ‘webuser’@’localhost’ IDENTIFIED BY ‘password’;
      [/sql]
    2. Extract the gzipped SQL file, “wordpressdb.gz”.
    3. Import the database using the SQL file:
      • Using phpMyAdmin -> Use wordpressdb and select “import”
      • cmd line -> [bash]mysql -u user -pPassword wordpressdb < wordpressdb.sql[/bash]
  5. Change WordPress DB Setup for the new domain name:

    [sql]UPDATE wp_options SET option_value=’http://yourdomainname’ WHERE option_name IN (‘siteurl’,’home’)[/sql]

    The only thing you need to change in the above SQL statement is ‘http://yourdomainname’

    If the query is successful you should see something like:

    Query OK, 2 rows affected

    Note: I recommend using your domain name without the top-level domain (TLD), so that you don’t run into issues when attempting to login as admin. Also, you will need to set up a virtual host in Apache. Edit httpd-vhosts.conf. If you are using XAMPP, this is in .\xampp\apache\conf. First, make sure to un-comment at the top:

    [bash]
    #
    # Use name-based virtual hosting.
    #
    ##NameVirtualHost *:80
    NameVirtualHost *:80[/bash]

    Now add a virtual host; you need this at the bare minimum:

    [bash]
    <VirtualHost *:80>
    ServerName website1
    DocumentRoot "C:/web/site1/path"
    </VirtualHost>
    [/bash]

    For each virtual host you add, you must also add the “ServerName” above to the list of aliases for 127.0.0.1 in your “hosts” file. If you’re using windows, it is under C:\Windows\System32\drivers\etc\. E.g.:

    [bash]
    127.0.0.1 localhost website1
    [/bash]

    Now, when you type “http://website1/” into your browser, it will point to the “C:/web/site1/path” directory (due to the change you made in “hosts”) and Apache will serve that directory (due to the virtual server added to “httpd-vhosts.conf”).

That’s it! You have backed up, restored, or migrated your WordPress blog.

More Resources:

Published inDevelopment

2 Comments

  1. Im not sure about the command line, but in phpmyadmin to execute this command, it must have double quotes:

    UPDATE wp_options SET option_value=”http://yourdomainname” WHERE option_name IN (“siteurl”,”home”)

    Mine executed fine with this, but has some redirect problems in logging into wp-admin.

    a

    • I was having redirect problems logging in too. That’s why I set option_value=”http://joebashe” on my local computer rather than “http://joebashe.com”. By doing this, it will redirect to your local server (assuming you have set up a virtual host in apache config.)

Leave a Reply