首页 > 解决方案 > 如何在 Azure 函数中使用 bcp.exe

问题描述

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

我能够在本地计算机和 azure 门户(功能应用程序)中使用 bcp。但是在我发布到 azure function app 后我没有打电话

示例代码:-

[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");
    }

错误日志:-

子问题:-

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

提前致谢!!

标签: c#azure-sql-databaseazure-functionsazure-blob-storagebcp

解决方案


我的建议是使用 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")
}

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


推荐阅读