如何解决如何在 Azure 虚拟机中备份 MySQL 数据库并将备份发送到 Blob 存储,使用 Azure 函数和 Python
我在 Azure VM 中有一个 MySQL 数据库,我正在寻找一种用 Python 编写 Azure 函数的方法,该函数将 mysqldump shell 命令的内容发送到 blob 存储容器。我每天使用计时器触发器备份数据库几次。
我的初始测试使用 os.system()
运行 mysqldump shell 命令,并将 gzip 压缩版本存储在临时存储中然后上传,这在 vscode 中本地工作,但这在生产中不起作用。在我的日志中,它只是说找不到文件,我已经尝试了几个不同的位置来在生产中测试该文件。
我正在寻找一种更好的方法来执行这些更新,最好将 gzip 文件的内容存储到某个 python 对象中,然后使用 blob 输出绑定进行存储。
解决方法
你可以在你的VM中使用python schedule
来做我认为的一切,没有必要使用Azure功能,它是你的VM在云端的外部环境,它不能访问任何东西在你的虚拟机里面。尝试使用以下代码按计划直接从您的 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()
结果:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。