How to clone a JavaScript backend Mobile Service

The Azure Mobile folks state very clearly that you shouldn’t use a production instance of Azure Mobile Services to test out new functionality, which makes sense. However, this brings up an interesting question from a customer: “How do I create a clone of my existing mobile service to do testing and development?”

A JavaScript backend mobile service has essentially two parts: the node.js site and code that hooks that site to the Mobile Services runtime and Azure SQL Database. The node.js site files (script files, permissions, custom API endpoints) are easy to clone because they are defined in the site itself. Other actions, like creating the mobile service itself and tables within the mobile service, must either be done in the Azure classic portal or by using the command line interface (CLI). So, if you really want to automate the process of cloning and syncing a production and staging site, you will use the CLI and Git.

Configure the Git repo

Since we need to pull down the production site and copy those files to our new staging site, we need to make sure that Git publishing is configured on the mobile service. Complete the following steps with your production site:

  1. Enable source control in your mobile service
  2. Install Git and create the local repository.

Note that you only need to configure Git publishing once for your subscription. At this point, you should have pulled the production site down to the Git repo on your local machine.

Create the cloned mobile service

You need to create a new mobile service (such as todolist-clone) and recreate the same tables that you have in the production site. You can do this either in the portal or by using the CLI, like this:

  1. Create the mobile service clone:

     azure mobile create todolist-clone <server-admin> <server-password>
    
  2. Re-create the same tables (once for each existing table):

     azure mobile table create todolist-clone TodoItem
    

Overwrite the cloned mobile service and publish

Once the new cloned service is configured, you can copy over from the production site and republish:

  1. Use Git to also create a local repository for the cloned site.

    Note that your credentials are the same as the first time—since they are shared across the subscription.

  2. On your local machine, copy files from the \service subdirectory of the original production repo (todolist) to the same local directory in the new repo (todolist-clone).

  3. Open the package.json file in the root of the cloned repo and update the name field to the name of the clone, as below:

     {
       "name": "todolist-clone" ,
      "version": "1.0.0",
      "description": "todolist-cordova - hosted on Windows Azure Mobile Services",
      "main": "server.js",
      "engines": {
        "node": ">= 0.8.19"
      },
      "dependencies": {
       "node-uuid": "~1.4.3"
      },
      "devDependencies": {},
      "scripts": {},
      "author": "unknown",
      "licenses": [],
      "keywords":[]
    }
    
  4. Save the changes and execute the following commands in the new repo to add/commit/publish changes:

     git add .
    
     git commit -a -m "cloned the site"
    
     git push origin master
    

At this point, the mobile service will be restarted and you should see your production APIs, script files, and permissions in the new cloned mobile service. Note that you won’t see your existing data, since the cloned service uses tables in a new DB schema that matches the service name (in this case todolist_clone).

Hope that this is helpful….

Cheers!

Glenn Gailey