如何在Azure函数中使用bcp.exe

时间:2018-10-19 03:47:57

标签: c# azure-sql-database azure-functions azure-storage-blobs bcp

我的目标是使用azure函数将数据从azure sql数据库备份到azure blob存储中。

我能够在本地计算机和Azure门户(功能应用程序)中使用bcp。 但是在发布到azure函数应用后,我无法打电话

SampleCode:-

[FunctionName("Function1")]
    public static IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequest req, TraceWriter log)
    {
        string filePath = "filePatch";
        string FileName = "filaName";
        string arg = @"database.scheme.table out table.txt -c -S tcp:testingServer -UUserName -PPassword";

        System.Diagnostics.Process process = new System.Diagnostics.Process();
        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
        startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        FileName = "bcp";
        startInfo.FileName = FileName;
        startInfo.Arguments = arg;
        process.StartInfo = startInfo;
        process.Start();

        process.WaitForExit();

        //upload to blob storage
        var storageAccount = CloudStorageAccount.Parse("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        var blobClient = storageAccount.CreateCloudBlobClient();
        var container = blobClient.GetContainerReference("container");
        var blob = container.GetBlockBlobReference("table");

        blob.UploadFromFileAsync(filePath);
        return (ActionResult)new OkObjectResult("Return result");
    }

错误日志:-

  • bcpSystem.ComponentModel.Win32Exception(2):系统找不到指定的文件
  • 在System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
  • 在System.Diagnostics.Process.Start()中

子问题:-

  1. bcp是否可以将数据直接复制到azure blob存储中?

提前谢谢!

1 个答案:

答案 0 :(得分:1)

我的建议是使用PowerShell而不是bcp来执行Azure SQL数据库的备份,并使用计划的Azure函数来执行将执行备份的PowerShell脚本。

以下示例PowerShell脚本:

$subcriptionId = "******"
$resourceGroupName = "project-tra-rg"

#Sql server and target database
$ServerName = "tra-sql-srv"
$DatabaseName = "tra-db"

#target storage informations
$StorageKeytype = "StorageAccessKey"
$StorageKey = "******"
$storageUriTarget = "https://trastorageac.blob.core.windows.net/backup"

#sql credentials
$pwdClear = "******"
$userName = "admin-tra"
$pwd = ConvertTo-SecureString $pwdClear -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential ($userName, $pwd)

#Backup naming logic
$bacpactName = "{0:yyyy-MM-dd}.bacpac" -f (get-date)
$uriTarget = $storageUriTarget + '/' + $DatabaseName + '-' + $bacpactName

Login-AzureRmAccount -SubscriptionId $subcriptionId

$exportRequest = New-AzureRmSqlDatabaseExport -ResourceGroupName $ResourceGroupName -ServerName $ServerName -DatabaseName $DatabaseName -StorageKeytype $StorageKeytype -StorageKey $StorageKey -StorageUri $uriTarget -AdministratorLogin $creds.UserName -AdministratorLoginPassword $creds.Password

while ($exportRequest.Status -eq "InProgress")
{
   $exportRequest = Get-AzureRmSqlDatabaseImportExportStatus -
   OperationStatusLink $exportRequest.OperationStatusLink
   [Console]::Write(" Export in progress..")
   Start-Sleep -s 10
}

if ($exportRequest.Status -eq "Succeeded")
{
   [Console]::Write(" Export done")
}

This文章将为您提供更多详细信息。