c# - 如何在 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");
}
错误日志:-
- bcpSystem.ComponentModel.Win32Exception(2):系统找不到指定的文件
- 在 System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
- 在 System.Diagnostics.Process.Start()
子问题:-
- bcp 是否能够直接将数据复制到 azure blob 存储中?
提前致谢!!
解决方案
我的建议是使用 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")
}
本文将为您提供更多详细信息。
推荐阅读
- python - 使用 desktop.ini 更改文件夹图标的问题
- c# - 从 API 端点访问通用对象
- amazon-web-services - EKS 集群终端节点访问
- javascript - 不理解 javascript 片段
- python - 如何加快文本翻译速度?
- pytorch - 在pytorch中将2D张量与3D张量相乘
- sql-server - 在 SQL Server 中检索“制造日期”前 14 天的数据,而不考虑年份
- apache-beam - 何时对按事件时间进行分组使用窗口化?
- azure - 在 APIM AZURE 上使用 RS256 创建 JWT
- loops - 如何在空手道中使用循环来创建 xml 请求正文?