如何使用 boto 从正在运行的实例创建 EC2 映像?

发布于 2024-10-21 07:48:05 字数 1701 浏览 0 评论 0原文

我正在尝试为我的 EC2 实例创建一个简单的 python 备份脚本。此脚本的目的是创建当前计算机的每日/每周快照(请参阅 这个关于 ServerFault 的问题)。我正在使用 EC2 API 的 boto python 包,并且想要创建一个 EBS来自给定实例的 AMI(例如 ElasticFox 的“创建映像”操作)

# This script will look up all your running EC2 images, find the current one, and back it up by creating an AMI 

# Configuration
accessKeyId = "..."
accessKeySecret = "..."
target = "..."

def resolveIp(target):
    import socket
    ip = repr(socket.gethostbyname_ex(target)[2][0])
    return ip

def find_target(target, connection) :
    ip = resolveIp(target)
    print "Finding instance for " + target + " (IP " + ip + ")"
    reservations = connection.get_all_instances();
    for reservation in reservations:
        instances = reservation.instances
        if len(instances) != 1:
            print "Skipping reservation " + reservation
            continue
        instance = instances[0]
        instanceIp = resolveIp(instance.dns_name)
        if instanceIp == ip:
            return instance

    raise Exception("Can't find instance with IP " + ip)

from boto.ec2.connection import EC2Connection

print "Connecting to EC2"
conn = EC2Connection(accessKeyId, accessKeySecret)
print "Connected to EC2"

instance = find_target(target, conn)
print "Backing up instance '{}'".format(instance)

# Now, I'd like to create a new image out of this instance
# Can you help?

(也报告为 boto 项目页面上的问题,因为我没有找到邮件列表)

I'm trying to create a simple python backup script for my EC2 instances. This script's purpose is to create daily/weekly snapshots of the current machine (see this question on ServerFault). I'm using the boto python package for EC2 API, and would like to create an EBS AMI from a given instance (like ElasticFox's "Create Image" action)

# This script will look up all your running EC2 images, find the current one, and back it up by creating an AMI 

# Configuration
accessKeyId = "..."
accessKeySecret = "..."
target = "..."

def resolveIp(target):
    import socket
    ip = repr(socket.gethostbyname_ex(target)[2][0])
    return ip

def find_target(target, connection) :
    ip = resolveIp(target)
    print "Finding instance for " + target + " (IP " + ip + ")"
    reservations = connection.get_all_instances();
    for reservation in reservations:
        instances = reservation.instances
        if len(instances) != 1:
            print "Skipping reservation " + reservation
            continue
        instance = instances[0]
        instanceIp = resolveIp(instance.dns_name)
        if instanceIp == ip:
            return instance

    raise Exception("Can't find instance with IP " + ip)

from boto.ec2.connection import EC2Connection

print "Connecting to EC2"
conn = EC2Connection(accessKeyId, accessKeySecret)
print "Connected to EC2"

instance = find_target(target, conn)
print "Backing up instance '{}'".format(instance)

# Now, I'd like to create a new image out of this instance
# Can you help?

(Also reported as an issue on the boto project page, since I didn't find a mailing list)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仲春光 2024-10-28 07:48:05

您需要 EC2Connection 对象的“create_image”方法。请参阅此处的文档。您还可以在 boto-users Google 网上论坛提问。

You want the "create_image" method of the EC2Connection object. See the docs here. You can also ask questions on the boto-users Google Group.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文