Пример #1
0
def check_snapshots (ec2, opts):
  print "Checking snapshots exist..."
  instances = utils.get_instance_objs(ec2, opts, filterDict={'tag:AutoSnapshot': 'Yes'})
  if instances == None:
    return None

  # Create list of all volumes attached to instances which are flagged as needing snapshots
  for instance in instances:
    vols_to_snapshot = []
    block_map = instance.block_device_mapping
    for _, block_obj in block_map.iteritems():
      if block_obj.volume_id not in vols_to_snapshot:
        vols_to_snapshot.append(block_obj.volume_id)

  # Create a list of all snapshots that exist
  all_snapshots = ec2.get_all_snapshots(owner='self')
  snapshot_volume_ids = []
  for snapshot in all_snapshots:
    if snapshot.volume_id not in snapshot_volume_ids:
      snapshot_volume_ids.append(snapshot.volume_id)

  if vols_to_snapshot:
    # Compare the two lists
    error_count = 0
    for vol in vols_to_snapshot:
      if vol not in snapshot_volume_ids:
        error_count += 1
        print "Error: Volume %s appears to have no snapshot for it !" % vol
    if error_count == 0:
      print 'All volumes have snapshots that should'
  else:
    print "No volumes seem to have snapshots in this region"
Пример #2
0
def take_snapshots (ec2, opts):
  instances = utils.get_instance_objs(ec2, opts, filterDict={'tag:AutoSnapshot': 'Yes'})    # We only snapshot a volume if the instance is tagged as such
  if instances == None:
    return None

  print "Taking snapshots of volumes for matching machines that are tagged with 'AutoSnapshot' = 'Yes'"

  volume_id_list = []
  for instance in instances:
    for _, device in instance.block_device_mapping.iteritems():
      volume_id_list.append(device.volume_id)         # Gather a list of volume ids for volumes attached to such tagged instances

  volume_objs = ec2.get_all_volumes(volume_ids=volume_id_list)  # Get a list of all volume objects that match our list of ids
  for volume_obj in volume_objs:
    print "Creating snapshot for %s" % volume_obj.id
    description_string = "Automatic %s triggered snapshot of [%s]" % (opts['snapshot_identifier'], volume_obj.id)
    if 'Name' in volume_obj.tags:
      description_string += ' for %s' % (volume_obj.tags['Name'])
    volume_obj.create_snapshot(description_string)