简体   繁体   English

通过s3cmd和Python自动备份脚本-错误处理?

[英]Automated backup script via s3cmd and Python — error handling?

I have a script that executes an s3cmd upload on a schedule. 我有一个脚本,可以按计划执行s3cmd上传。 I have yet to encounter any errors in the upload process, but I anticipate I may run into one sometime in the future and would like to send an SNS notification if ever this happens. 我尚未在上传过程中遇到任何错误,但是我希望将来可能会遇到一个错误,并且如果发生这种情况,我想发送SNS通知。 I know how to send the notification using the Boto3 AWS SDK, however my problem is knowing when to send it. 我知道如何使用Boto3 AWS开发工具包发送通知,但是我的问题是知道何时发送通知。

The script is written in Python, and sends BASH commands for the s3cmd upload using the subprocess module: 该脚本是用Python编写的,并使用subprocess模块发送用于s3cmd上传的BASH命令:

>> subprocess.check_output(['bash', '-c', "s3cmd put /path/to/file s3://bucket-name"])

I just want some form of indication when any errors are encountered. 当遇到任何错误时,我只想要某种形式的指示。 What is the best approach to do this? 最好的方法是什么?

You should use the python SDK from aws to achieve this: boto3. 您应该使用来自aws的python SDK来实现此目的:boto3。 Then simply add a try/catch block to publish to an SNS if you wish. 然后,如果需要,只需添加一个try / catch块即可发布到SNS。

with open('test.jpg', 'rb') as data:
    s3.Bucket('my-bucket').put_object(Key='test.jpg', Body=data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM