gpt4 book ai didi

mysql - 如何在 Azure 虚拟机中备份 MySQL 数据库,并使用 Azure 函数和 Python 将备份发送到 Blob 存储

转载 作者:行者123 更新时间:2023-12-03 03:50:24 27 4
gpt4 key购买 nike

我在 Azure VM 内有一个 MySQL 数据库,我正在寻找一种用 Python 编写 Azure 函数的方法,该函数将 mysqldump shell 命令的内容发送到 Blob 存储容器。我使用计时器触发器每天备份数据库几次。

我使用的初始测试 os.system()运行 mysqldump shell 命令,我将 gzip 压缩版本存储在临时存储中,然后上传,这在 vscode 中本地工作,但这在生产中不起作用。在我的日志中,它只是说找不到文件,并且我已经尝试了几个不同的位置来在生产中测试该文件。

我正在寻找一种更好的方法来执行这些更新,最好将 gzip 压缩文件的内容存储到某个 python 对象中,然后使用 blob 输出绑定(bind)进行存储。

最佳答案

你可以在你的VM中使用python schedule来完成我认为的一切,不需要使用Azure功能,它是你的VM在云上的外部环境,它可以'无法访问虚拟机内的任何内容。尝试使用下面的代码按计划直接从您的虚拟机上传 .zip 文件:

from azure.storage.blob import BlobClient
import time
import schedule

def uploadZip():
storage_connection_string='<your storage connection string>'
container_name = 'backups'

#ignore the process about generating the .zip file,just store the file to local temp path
templeFilePath = "d:/home/temp/test.zip"

blob_client = BlobClient.from_connection_string(storage_connection_string,container_name,time.strftime('%Y-%m-%d,%H:%M:%S',time.localtime())+".zip")
with open(templeFilePath,'rb') as stream:
blob_client.upload_blob(stream)
print("upload successfully")

#backup each 12 hours
#schedule.every(12).hours.do(uploadZip)
#for a quick test, upload each 5 seconds
schedule.every(5).seconds.do(uploadZip)

while 1:
schedule.run_pending()

结果:

enter image description here enter image description here

关于mysql - 如何在 Azure 虚拟机中备份 MySQL 数据库,并使用 Azure 函数和 Python 将备份发送到 Blob 存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66970093/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com