Migrate your site on Dashboard

This tutorial will guide you in moving your production site on Dashboard.

This tutorial assumes you've created a site on Dashboard and will guide you in how to migrate the contents of your production site into your newly created Dashboard site. Any WordPress site is usually split in three main components: the code (themes and plugins), the database and the media files, so we'll take each of these one by one.

To follow this tutorial, you'll need a site to move, more exactly archives of its code, database and media files. In this tutorial, the site to be moved is in a repository called mysite and it will be moved into a repository called my-dashboard-site.

You'll also need to have installed the mysql command line , kubectl and Google SDK .

Step 1. Import the code

1. Create a repository on GitHub

You'll need to store your code on your own GitHub repository and you can use our Stack example repo as a starting point. For example, you can create a new repository (my-dashboard-site) and copy the contents from stack-example-wordpress into your new repo.

2. Setup Google Cloud Build

You can also set up any other CI that updates the site when pushing on master, but in this tutorial we'll follow the setup with Google Cloud Build. Here are Google's detailed steps on how to run builds on GitHub , which we'll also take one by one here.

Google Cloud Build will allow you to connect your GitHub repository with your GCP project and set up continuous integration for your repository.

The steps below provide instructions for installing the app only for one repository, in this case my-dashboard-site. Be aware to replace this with the name of your own repository on GitHub.

  1. The first thing you need to do is enable the Cloud Build API in the GCP project where your Dashboard site is.

  2. Go to the GitHub marketplace page for the Google Cloud Build app , scroll down and click Setup with Google Cloud Build at the bottom of the page.

    Setup with Google Cloud Build

    If you've already done this, click on Edit your plan.

    Configure Google Cloud Build - Edit your plan

  3. If prompted, sign in to GitHub.

  4. In the Edit your plan page, select or update your billing information and click grant this app access. If you have previously purchased the GitHub Cloud Build App and are reinstalling it, skip this step.

    Click on Grant this app access

  5. Select the Only select repositories option, then use the Select repositories drop-down to only enable your repository (my-dashboard-site) for access via the Cloud Build app. You will also be able to enable additional repositories later.

    Select the repositories for which to enable access

  6. Click Install.

  7. Sign in to GCP then click Authorize Google Cloud Build by GoogleCloudBuild.

    Authorize the Google Cloud Build

  8. Select your GCP project where you've created your cluster , check the consent checkbox and click Next.

    Connect your selected GitHub repositories to your GCP projects

  9. In the Select repository page that appears, select the checkbox next to your target repository, in this case my-dashboard-site, then click on Connect repository.

    Select the GitHub repositories to connect to your GCP project

  10. If you wish to create one or more initial default trigger(s) that will trigger builds on any branch push, use the Create a push trigger page that appears to those triggers. Select the boxes next to each target repository and click Create push triggers. Otherwise, skip this step.

    Create push trigger

  11. You now have successfully installed the Google Cloud Build app, connected your repository to your GCP project, and created push triggers that will launch your build(s).

    You have successfully installed the Google Cloud Build app

  12. Additionally, you can go to Google Cloud Platform - Cloud Build - Triggers to edit your trigger to only trigger builds when you push on the master branch.

Edit the default trigger

Edit the default trigger to only trigger builds on master

3. Setting service account permissions

You need to enable your service account to manage the Kubernetes Engine resources, you can read more about setting service account permissions on Google's documentation.

Go to Google Cloud Platform - Cloud Build - Settings and enable the Kubernetes Engine service:

Enable Kubernetes Engine for Cloud Build

4. Create a cloudbuild.yaml file

To use the Google Cloud Build app, your repository must contain a cloudbuild.yaml file to configure your build. Create a file named cloudbuild.yaml in your project root directory.

You can read more about creating a cloudbuild.yaml file on Google's documentation, but for now you can use this example as a starting point:

steps:
- name: 'gcr.io/cloud-builders/docker'
  args: [ 'build', '-t', 'gcr.io/$PROJECT_ID/${_SITE_NAME}:$SHORT_SHA', '.' ]
- name: 'gcr.io/cloud-builders/kubectl'
  args: ['patch', '-n', '${_PROJECT_NAME}', 'wordpress', '${_SITE_NAME}', '--type=json', '-p', '[{"op": "replace", "path": "/spec/image", "value": "gcr.io/$PROJECT_ID/${_SITE_NAME}:$SHORT_SHA"}]']
  env:
  - 'CLOUDSDK_COMPUTE_ZONE=<your cluster zone>'
  - 'CLOUDSDK_CONTAINER_CLUSTER=<your cluster name>'
images:
  - 'gcr.io/$PROJECT_ID/${_SITE_NAME}:$SHORT_SHA'
substitutions:
  _SITE_NAME : <your site name>
  _PROJECT_NAME : <your project name>

What you need to replace:

  1. <your cluster zone> and <your cluster name> with the zone where your cluster was created and its name, go to Google Cloud Platform -> Kubernetes Engine -> Clusters to see them:

    Get your cluster's details

  2. <your site name> and <your project name>, you can see these in the Presslabs dashboard, in the Runtime section of your site:

    Get your site's runtime details

5. Add themes and plugins to the repo

In your Dashboard repository (my-dashboard-site), copy the wp-content/themes and wp-content/plugins folders from the site you want to move (mysite).

After you push your changes, go to Google Cloud Platform - Cloud Build to see if your build was successful.

Step 2. Import the database

You will need a database backup to import into your Dashboard site, e.g. mysite-mysql-backup.sql.

  1. Do a search-replace in your backup for any fields that have set as default 0000-00-00 00:00:00. Because MySQL runs with NO_ZERO_DATE you'll get errors if you leave them as 0000-00-00 00:00:00.

    You can run for example:

    sed -i 's/0000-00-00 00:00:00/1970-01-01 00:00:00/g' mysite-mysql-backup.sql
    
  2. Go to your Presslabs Dashboard, on the Getting Started section and run the commands from here on your local machine, to connect to the database.

    Getting Started - connect to the database

    First, you set up port forwarding in order to be able to connect from your machine.

    kubectl -n proj-ut7slg port-forward default-mysql-0 3307:3306
    

    Leave this running and open a new terminal tab, than get the username, password and database name and export them as environment variables:

    export MYSQL_USER="$(kubectl -n proj-ut7slg get secret mysite-presslabs-m-j2dar-mysql -o jsonpath='{.data.USER}' | base64 --decode)"
    export MYSQL_PWD="$(kubectl -n proj-ut7slg get secret mysite-presslabs-m-j2dar-mysql -o jsonpath='{.data.PASSWORD}' | base64 --decode)"
    export MYSQL_DATABASE="$(kubectl -n proj-ut7slg get secret mysite-presslabs-m-j2dar-mysql -o jsonpath='{.data.DATABASE}' | base64 --decode)"
    

    Finally, connect to the database using the mysql command-line, without the need to type the password, used by default from the environment variable.

    mysql -u ${MYSQL_USER} -h 127.0.0.1 -P 3307 ${MYSQL_DATABASE}
    
  3. Import the database

    > source ~/Desktop/mysite-mysql-backup-2019-11-26-1243-0CCM76FFY4APG0P.sql
    

Step 3. Import media files

Go to Getting Started -> Access the media files to see your storage details:

Access the media files

To import the media files, you can use the command you find here, under Using the CLI:

gsutil -m cp -r ./wp-content/uploads gs://my-first-project-gqx81/mysite-presslabs-m-3xvq6/wp-content/

Just copy it and run it on your local machine, depending on where you have your uploads archive. After this, you can open your storage browser to see your newly imported media files.

When you import the media files, the files will have set as owner the user that uploads them, which means WordPress won't have reading/writing rights. You need to run the following command to change this (replace with your own data):

gsutil -m acl ch -r -u my-first-project-gqx81@testing-reactor.iam.gserviceaccount.com:O gs://my-first-project-gqx81/mysite-presslabs-m-3xvq6/wp-content/uploads/

You need to replace:

  1. my-first-project-gqx81@testing-reactor.iam.gserviceaccount.com with your own email

    To find it out, copy your K8s project namespace, than run on your local machine:

    kubectl -n <K8s project namespace> describe iamserviceaccount
    

    Look for the email field and copy it, in this case is my-first-project-gqx81@testing-reactor.iam.gserviceaccount.com.

  2. gs://my-first-project-gqx81/mysite-presslabs-m-3xvq6/wp-content/uploads/ with your own storage bucket, you can see it in the Access Media file section above and also on the Presslabs Dashboard -> Overview page

See Also

Connect to your cluster and access Kubernetes pods

Create your first site

This tutorial will guide you to create your first organization, how you can group sites in projects [...]

Delete sites, projects and organizations

All you need to know about deleting sites, projects and organizations and what this implies.