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())
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)
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,
#!//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 ***'