require __DIR__ . '/vendor/autoload.php';
$console = Console::getInstance();
$opt = new Getopt(array('key|O-s' => 'AWS access key', 'secret|W-s' => 'AWS secret key', 'instance|i-s' => 'Instance to backup', 'region|r-s' => 'Region of instance', 'exclude|e-s' => 'Exclude volumes/devices, comma delimited', 'dry-run|d' => 'Dry run, do not delete anything'));
$opt->parse();
try {
    $key = empty($opt->key) ? getenv('AWS_ACCESS_KEY') : $opt->key;
    $secret = empty($opt->secret) ? getenv('AWS_SECRET_KEY') : $opt->secret;
    $region = empty($opt->region) ? getenv('AWS_REGION') : $opt->region;
    $instanceId = $opt->instance;
    $exclusions = empty($opt->exclude) ? array() : array_map('trim', explode(',', $opt->exclude));
    $dryRun = (bool) $opt->{'dry-run'};
    $client = Ec2Client::factory(array('key' => $key, 'secret' => $secret, 'region' => $region));
    $clientHelper = new Ec2ClientHelper($client);
    if (empty($instanceId)) {
        $hostname = gethostname() . '.';
        $instance = $clientHelper->getInstanceByHostname($hostname);
        $instanceId = $instance['InstanceId'];
    } else {
        $instance = $clientHelper->getInstanceById($instanceId);
    }
    $instanceName = $clientHelper->resolveInstanceName($instance);
    $console->writeLine("Creating snapshots of {$instanceName}");
    $volumes = $clientHelper->getVolumesByInstance($instanceId);
    if (count($volumes) === 0) {
        throw new Exception('No volumes found');
    }
    $toBackup = array();
    foreach ($volumes as $volume) {
        $attachment = $clientHelper->getVolumeAttachmentForInstance($volume['Attachments'], $instanceId);
        $volumeId = $attachment['VolumeId'];
        $device = $attachment['Device'];