Esempio n. 1
0
    def snapSystem(self):
	try:
	    snap = ec2.create_snapshot(self.volume,'Snapshot of %s - %s - %s' % (self.instanceName(), self.volumeDevice(), dateString))
	    snap.add_tags({'backup': 'True'})
        except:
            logger.error('Failed to start Snapshotting for %s' % (self.volume))
        return snap.id
Esempio n. 2
0
def create_snapshot(module, ec2, state=None, description=None, wait=None,
                    wait_timeout=None, volume_id=None, instance_id=None,
                    snapshot_id=None, device_name=None, snapshot_tags=None,
                    last_snapshot_min_age=None):
    snapshot = None
    changed = False

    required = [volume_id, snapshot_id, instance_id]
    if required.count(None) != len(required) - 1: # only 1 must be set
        module.fail_json(msg='One and only one of volume_id or instance_id or snapshot_id must be specified')
    if instance_id and not device_name or device_name and not instance_id:
        module.fail_json(msg='Instance ID and device name must both be specified')

    if instance_id:
        try:
            volumes = ec2.get_all_volumes(filters={'attachment.instance-id': instance_id, 'attachment.device': device_name})
        except boto.exception.BotoServerError as e:
            module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))

        if not volumes:
            module.fail_json(msg="Could not find volume with name %s attached to instance %s" % (device_name, instance_id))

        volume_id = volumes[0].id

    if state == 'absent':
        if not snapshot_id:
            module.fail_json(msg = 'snapshot_id must be set when state is absent')
        try:
            ec2.delete_snapshot(snapshot_id)
        except boto.exception.BotoServerError as e:
            # exception is raised if snapshot does not exist
            if e.error_code == 'InvalidSnapshot.NotFound':
                module.exit_json(changed=False)
            else:
                module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))

        # successful delete
        module.exit_json(changed=True)

    if last_snapshot_min_age > 0:
        try:
            current_snapshots = ec2.get_all_snapshots(filters={'volume_id': volume_id})
        except boto.exception.BotoServerError as e:
            module.fail_json(msg="%s: %s" % (e.error_code, e.error_message))

        last_snapshot_min_age = last_snapshot_min_age * 60 # Convert to seconds
        snapshot = _get_most_recent_snapshot(current_snapshots,
                                             max_snapshot_age_secs=last_snapshot_min_age)
    try:
        # Create a new snapshot if we didn't find an existing one to use
        if snapshot is None:
            snapshot = ec2.create_snapshot(volume_id, description=description)
            changed = True
        if wait:
            if not _create_with_wait(snapshot, wait_timeout):
                module.fail_json(msg='Timed out while creating snapshot.')
        if snapshot_tags:
            for k, v in snapshot_tags.items():
                snapshot.add_tag(k, v)
    except boto.exception.BotoServerError as e:
        module.fail_json(msg="%s: %s" % (e.error_code, e.error_message))

    module.exit_json(changed=changed,
                     snapshot_id=snapshot.id,
                     volume_id=snapshot.volume_id,
                     volume_size=snapshot.volume_size,
                     tags=snapshot.tags.copy())
Esempio n. 3
0
        # successful delete
        module.exit_json(changed=True)

    if last_snapshot_min_age > 0:
        try:
            current_snapshots = ec2.get_all_snapshots(filters={'volume_id': volume_id})
        except boto.exception.BotoServerError, e:
            module.fail_json(msg="%s: %s" % (e.error_code, e.error_message))

        last_snapshot_min_age = last_snapshot_min_age * 60 # Convert to seconds
        snapshot = _get_most_recent_snapshot(current_snapshots,
                                             max_snapshot_age_secs=last_snapshot_min_age)
    try:
        # Create a new snapshot if we didn't find an existing one to use
        if snapshot is None:
            snapshot = ec2.create_snapshot(volume_id, description=description)
            changed = True
        if wait:
            if not _create_with_wait(snapshot, wait_timeout):
                module.fail_json(msg='Timed out while creating snapshot.')
        if snapshot_tags:
            for k, v in snapshot_tags.items():
                snapshot.add_tag(k, v)
    except boto.exception.BotoServerError, e:
        module.fail_json(msg="%s: %s" % (e.error_code, e.error_message))

    module.exit_json(changed=changed,
                     snapshot_id=snapshot.id,
                     volume_id=snapshot.volume_id,
                     volume_size=snapshot.volume_size,
                     tags=snapshot.tags.copy())
Esempio n. 4
0
    if instance_id and not device_name or device_name and not instance_id:
        module.fail_json('Instance ID and device name must both be specified')

    ec2 = ec2_connect(module)

    if instance_id:
        try:
            volumes = ec2.get_all_volumes(filters={'attachment.instance-id': instance_id, 'attachment.device': device_name})
            if not volumes:
                module.fail_json(msg="Could not find volume with name %s attached to instance %s" % (device_name, instance_id))
            volume_id = volumes[0].id
        except boto.exception.BotoServerError, e:
            module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))

    try:
        snapshot = ec2.create_snapshot(volume_id, description=description)
        time_waited = 0
        if wait:
            snapshot.update()
            while snapshot.status != 'completed':
                time.sleep(3)
                snapshot.update()
                time_waited += 3
                if wait_timeout and time_waited > wait_timeout:
                    module.fail_json('Timed out while creating snapshot.')
        for k, v in snapshot_tags.items():
            snapshot.add_tag(k, v)
    except boto.exception.BotoServerError, e:
        module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))

    module.exit_json(changed=True, snapshot_id=snapshot.id, volume_id=snapshot.volume_id,
Esempio n. 5
0
def create_snapshot(module,
                    ec2,
                    state=None,
                    description=None,
                    wait=None,
                    wait_timeout=None,
                    volume_id=None,
                    instance_id=None,
                    snapshot_id=None,
                    device_name=None,
                    snapshot_tags=None,
                    last_snapshot_min_age=None):
    snapshot = None
    changed = False

    required = [volume_id, snapshot_id, instance_id]
    if required.count(None) != len(required) - 1:  # only 1 must be set
        module.fail_json(
            msg=
            'One and only one of volume_id or instance_id or snapshot_id must be specified'
        )
    if instance_id and not device_name or device_name and not instance_id:
        module.fail_json(
            msg='Instance ID and device name must both be specified')

    if instance_id:
        try:
            volumes = ec2.get_all_volumes(
                filters={
                    'attachment.instance-id': instance_id,
                    'attachment.device': device_name
                })
        except boto.exception.BotoServerError as e:
            module.fail_json(msg="%s: %s" % (e.error_code, e.error_message))

        if not volumes:
            module.fail_json(
                msg="Could not find volume with name %s attached to instance %s"
                % (device_name, instance_id))

        volume_id = volumes[0].id

    if state == 'absent':
        if not snapshot_id:
            module.fail_json(
                msg='snapshot_id must be set when state is absent')
        try:
            ec2.delete_snapshot(snapshot_id)
        except boto.exception.BotoServerError as e:
            # exception is raised if snapshot does not exist
            if e.error_code == 'InvalidSnapshot.NotFound':
                module.exit_json(changed=False)
            else:
                module.fail_json(msg="%s: %s" %
                                 (e.error_code, e.error_message))

        # successful delete
        module.exit_json(changed=True)

    if last_snapshot_min_age > 0:
        try:
            current_snapshots = ec2.get_all_snapshots(
                filters={'volume_id': volume_id})
        except boto.exception.BotoServerError as e:
            module.fail_json(msg="%s: %s" % (e.error_code, e.error_message))

        last_snapshot_min_age = last_snapshot_min_age * 60  # Convert to seconds
        snapshot = _get_most_recent_snapshot(
            current_snapshots, max_snapshot_age_secs=last_snapshot_min_age)
    try:
        # Create a new snapshot if we didn't find an existing one to use
        if snapshot is None:
            snapshot = ec2.create_snapshot(volume_id, description=description)
            changed = True
        if wait:
            if not _create_with_wait(snapshot, wait_timeout):
                module.fail_json(msg='Timed out while creating snapshot.')
        if snapshot_tags:
            for k, v in snapshot_tags.items():
                snapshot.add_tag(k, v)
    except boto.exception.BotoServerError as e:
        module.fail_json(msg="%s: %s" % (e.error_code, e.error_message))

    module.exit_json(changed=changed,
                     snapshot_id=snapshot.id,
                     volume_id=snapshot.volume_id,
                     volume_size=snapshot.volume_size,
                     tags=snapshot.tags.copy())
Esempio n. 6
0
#!//usr/bin/python

import boto.ec2

print '*** Inicio de Backup com ***'

# Conectando na regiao default
# ec2 = boto.connect_ec2()
#
# Pegando a regiao dinamicamente e passando por parametro
# r = boto.ec2.get_region('ap-southeast-1')
# conn = boto.connect_ec2( region=r)

ec2 = boto.ec2.connect_to_region('sa-east-1')

volumes = ec2.get_all_volumes()

for volume in volumes:
    print volume.id
    ec2.create_snapshot(volume.id, 'backup script')

print '*** Fim de Backup ***'
def create_ebs_snapshot(ebs_volume):
    ec2_instance = str(ebs_volume.attach_data.instance_id)
    print "Snap-shotting ebs_volume : " + ebs_volume.id + " description : [ " + ec2_instance + " ] "
    ec2.create_snapshot(ebs_volume.id, ec2_instance)
#!//usr/bin/python

import boto.ec2

print '*** Inicio de Backup com ***'

# Conectando na regiao default
# ec2 = boto.connect_ec2()
#
# Pegando a regiao dinamicamente e passando por parametro
# r = boto.ec2.get_region('ap-southeast-1')
# conn = boto.connect_ec2( region=r)

ec2 = boto.ec2.connect_to_region('sa-east-1')

volumes = ec2.get_all_volumes()

for volume in volumes:
	print volume.id
	ec2.create_snapshot(volume.id, 'backup script')


print '*** Fim de Backup ***'
Esempio n. 9
0
def create_snapshot(module,
                    ec2,
                    state=None,
                    description=None,
                    wait=None,
                    wait_timeout=None,
                    volume_id=None,
                    instance_id=None,
                    snapshot_id=None,
                    device_name=None,
                    snapshot_tags=None,
                    last_snapshot_min_age=None,
                    create_volume_permissions=None):
    snapshot = None
    changed = False

    if instance_id:
        try:
            volumes = ec2.get_all_volumes(
                filters={
                    'attachment.instance-id': instance_id,
                    'attachment.device': device_name
                })
        except boto.exception.BotoServerError as e:
            module.fail_json(msg="%s: %s" % (e.error_code, e.error_message))

        if not volumes:
            module.fail_json(
                msg="Could not find volume with name %s attached to instance %s"
                % (device_name, instance_id))

        volume_id = volumes[0].id

    if state == 'absent':
        try:
            ec2.delete_snapshot(snapshot_id)
        except boto.exception.BotoServerError as e:
            # exception is raised if snapshot does not exist
            if e.error_code == 'InvalidSnapshot.NotFound':
                module.exit_json(changed=False)
            else:
                module.fail_json(msg="%s: %s" %
                                 (e.error_code, e.error_message))

        # successful delete
        module.exit_json(changed=True)

    if last_snapshot_min_age > 0:
        try:
            current_snapshots = ec2.get_all_snapshots(
                filters={'volume_id': volume_id})
        except boto.exception.BotoServerError as e:
            module.fail_json(msg="%s: %s" % (e.error_code, e.error_message))

        last_snapshot_min_age = last_snapshot_min_age * 60  # Convert to seconds
        snapshot = _get_most_recent_snapshot(
            current_snapshots, max_snapshot_age_secs=last_snapshot_min_age)
    try:
        if snapshot_id:
            snapshot = ec2.get_all_snapshots(snapshot_ids=[snapshot_id])[0]
        # Create a new snapshot if we didn't find an existing one to use
        if snapshot is None:
            snapshot = ec2.create_snapshot(volume_id, description=description)
            changed = True
        if wait:
            if not _create_with_wait(snapshot, wait_timeout):
                module.fail_json(msg='Timed out while creating snapshot.')
        if snapshot_tags:
            tags_to_add, tags_to_remove = existing_to_desired(
                snapshot.tags.items(), snapshot_tags.items())
            for (k, v) in tags_to_add:
                snapshot.add_tag(k, v)
                changed = True
            for (k, v) in tags_to_remove:
                snapshot.remove_tag(k, value=v)
                changed = True

        permissions = ec2.get_snapshot_attribute(snapshot.id)

        users_to_add, users_to_remove = existing_to_desired(
            permissions.attrs.get('user_ids', []), [
                str(user_id)
                for user_id in create_volume_permissions.get('user_ids', [])
            ])
        groups_to_add, groups_to_remove = existing_to_desired(
            permissions.attrs.get('groups', []),
            create_volume_permissions.get('groups', []))

        if users_to_add or groups_to_add:
            ec2.modify_snapshot_attribute(snapshot.id,
                                          user_ids=users_to_add,
                                          groups=groups_to_add)
            permissions = ec2.get_snapshot_attribute(snapshot.id)
            changed = True
        if users_to_remove or groups_to_remove:
            ec2.modify_snapshot_attribute(snapshot.id,
                                          operation='remove',
                                          user_ids=users_to_remove,
                                          groups=groups_to_remove)
            permissions = ec2.get_snapshot_attribute(snapshot.id)
            changed = True
    except boto.exception.BotoServerError as e:
        module.fail_json(msg="%s: %s" % (e.error_code, e.error_message))

    module.exit_json(changed=changed,
                     snapshot_id=snapshot.id,
                     volume_id=snapshot.volume_id,
                     volume_size=snapshot.volume_size,
                     tags=snapshot.tags.copy(),
                     permissions=permissions.attrs)