Jeff Sanders Technical Blog

I am a Microsoft employee that has worked on all aspects of the Web Stack for a long time. I hope these blogs are useful to you! Use this information at your own risk.


<< Go Back

Powershell Script To Backup Windows Azure Mobile Services Scripts

- 25 Feb 2014

Listing for a PowerShell Script to backup Windows Azure Mobile Services scripts and configuration information.  I will get something more complete up on the Script Repository this next month!  This also assumes you have set your subscription active using Windows Azure PowerShell.  See How to install and configure Windows Azure PowerShell for more information.

function main()
{
    $startDir = Get-Location;
    $MobileServices =  ListMobileServices ;
    $MobileServices | foreach{
        # for every service create a directory
        $MobileServiceName = $_.name;
        $MobileServiceName;
        $MobileServiceDir = CreateMobileServiceDirectory($MobileServiceName);
        # and move to that directory
        Set-Location $MobileServiceDir;
        $ScriptList = ListScripts $MobileServiceName;
        $ScriptList;
        if ($ScriptList.table)
        {
            $tblScriptLoc = CreateMobileServiceDirectory("scripts");
            Set-Location $tblScriptLoc;
            $ScriptList.table | foreach{
                $ScriptName = "table/{0}.{1}" -f $_.table, $_.operation;
                azure mobile script download $MobileServiceName $ScriptName;
                }
                #end foreach table
            Set-Location $MobileServiceDir;
        }
        if($ScriptList.scheduler)
        {
            $tblScriptLoc = CreateMobileServiceDirectory("scripts");
            Set-Location $tblScriptLoc;
            $ScriptList.scheduler | foreach{
                $ScriptName = "scheduler/{0}" -f $_.name;
                azure mobile script download $MobileServiceName $ScriptName;
                }#end foreach scheduler
            Set-Location $MobileServiceDir;
        }
        if($ScriptList.api)
        {
            $tblScriptLoc = CreateMobileServiceDirectory("scripts");
            Set-Location $tblScriptLoc;
            $ScriptList.api | foreach{
                $ScriptName = "api/{0}" -f $_.name;
                azure mobile script download $MobileServiceName $ScriptName;
                }
                #end foreach api
            Set-Location $MobileServiceDir;
        }
        if($ScriptList.shared)
        {
            $tblScriptLoc = CreateMobileServiceDirectory("scripts");
            Set-Location $tblScriptLoc;
            $ScriptList.shared | foreach{
                $ScriptName = "shared/{0}" -f $_.name;
                azure mobile script download $MobileServiceName $ScriptName;
                }
                #end foreach shared
            Set-Location $MobileServiceDir;
        }
        #dump config to a json file:
        $tmp=""; azure mobile config list --json $MobileServiceName |  foreach{  $tmp += $_ };  New-Item "config.json" -ItemType file -Value $tmp;
        $tmp=""; azure mobile show --json $MobileServiceName |  foreach{  $tmp += $_ };  New-Item "details.json" -ItemType file -Value $tmp;
        $tmp=""; azure mobile job list --json $MobileServiceName |  foreach{  $tmp += $_ };  New-Item "job.json" -ItemType file -Value $tmp;
    Set-Location $startDir.Path;
    };
    #end foreach Service
}
function CreateMobileServiceDirectory()
{
    param(
        [Parameter(Mandatory=$true)]
        $ServiceName
        )
    $loc = Get-Location;
    [string]$fullPath = "{0}\{1}" -f $loc.Path, $ServiceName;
    if(Test-Path $fullPath)
    {
        if ( Test-Path $fullPath -pathType Container)
        {
        }
        else
        {
            #error
        }
    }
    else
    {
        $newObj= New-Item $fullPath -ItemType directory;
    }
    return $fullPath;
}
function ListTables()
{
    param(
        [Parameter(Mandatory=$true)]
        $ServiceName
        )
    $TableTemp = "";
    $TMPP=azure mobile table list --json $ServiceName |  foreach{
        $TableTemp += $_;
        };
    return ConvertFrom-Json($TableTemp);
}
function ListScripts()
{
    param(
        [Parameter(Mandatory=$true)]
        $ServiceName
    )
    $TableTemp = "";
    $TMPP=azure mobile script list --json $ServiceName |  foreach{
        $TableTemp += $_;
        };
    return ConvertFrom-Json($TableTemp);
}
function ListMobileServices()
{
    $TempJsonString= "";
    # Get a list of Services as Json objects
    # since it is a bunch of Line feeds have to concatonate the string
    azure mobile list --json |  foreach{ $TempJsonString += $_  };
    return ConvertFrom-Json($TempJsonString);
}
#call Main!
main; 



<< Go Back