my ( $self, $bag_of_dbs ) = @_;
my $size_as_float = $bag_of_dbs->total_size;
$self->log->debug( "Making SNAPSHOT " . $bag_of_dbs->tag . " Current status=" . $bag_of_dbs->status );
# round up volumesize to nearest Gb +1
# rounding to nearest Gig doesn't seem to enough
my $round_up_size = int( $size_as_float + 2 );
#$self->clear_ec2;
my $volume = $self->ec2->create_volume( Size => $round_up_size, AvailabilityZone => $self->this_zone );
if ( $volume->can('errors') ) {
$self->log->error( "[Error Creating Volume] " . $self->pp_ec2_errors( $volume->errors ) );
return;
}
else {
$self->log->info( "Created ", $volume->volume_id, " ", $volume->size, " Gb" );
}
my $do_attach = $self->ec2->attach_volume(
VolumeId => $volume->volume_id,
InstanceId => $self->this_instance_id,
Device => $self->device
);
$self->log->info( "Attaching " . $volume->volume_id . " Device " . $self->device );
if ( $do_attach->can('errors') ) {
$self->log->error( "[Error attaching Volume] " . $self->pp_ec2_errors( $do_attach->errors ) );
$self->log->info( "Deleting " . $volume->volume_id );
$self->ec2->delete_volume( VolumeId => $volume->volume_id );
return;
}
else {
my $wait_time = 0;
my $attach_status = '';
while ( $attach_status ne 'attached' && $wait_time < 30 ) {
$self->log->info("Waiting 10 seconds for volume to become available");
sleep 10;
$wait_time += 10;
$attach_status =
$self->ec2->describe_volumes( VolumeId => $volume->volume_id )->[0]->attachments->[0]->{status};
$self->log->info("Volume $attach_status");
}
unless ( $attach_status eq 'attached' ) {
$self->log->error("ERROR ATTACHING VOLUME AFTER 30 seconds... DETACHING AND DELETING");
$self->clean_up_by_volume($volume);
# todo, check for errors in the delete call
$self->log->fatal("COULD NOT ATTACH VOLUME. THIS IS BAD. EXITING") && die;
}
}
# Make filesystem on the new volume
$self->log->info( "Making filesystem on " . $self->device );
my $mkfs_path = can_run('mkfs.xfs') or $self->log->warn('mkfs.xfs not installed!');
my $mkfs_cmd = [ 'sudo', $mkfs_path, $self->device ];
$self->log->info("Cleaning up after failed mkfs");
$self->clean_up_by_volume($volume);
return;
}
# Make the directory
my $mkdir_cmd = [ 'sudo', 'mkdir', '-p', $self->myd_destination_folder ];
unless ( $self->run_command($mkdir_cmd) ) {
$self->log->info("Cleaning up after failed mkdir");
$self->clean_up_by_volume($volume);
return;
}
# Mount new volume to it
$self->log->info( "Mounting " . $self->device );
my $mount_path = can_run('mount') or $self->log->warn('mount not installed!');
my $mount_cmd = [ 'sudo', $mount_path, $self->device, $self->myd_destination_folder ];
$self->log->info("Cleaning up after failed mount");
$self->clean_up_by_volume($volume);
return;
}
# Copy the each MYD dir
foreach my $myd_dir ( $bag_of_dbs->all_dbs ) {
my $copy_cmd = [ 'sudo', 'cp', '-r', $myd_dir->myd_path, $self->myd_destination_folder ];
$self->log->debug( join " ", @$copy_cmd );
unless ( $self->run_command($copy_cmd) ) {
$self->log->info("Cleaning up after failed copy");
my $umount_path = can_run('umount') or $self->log->warn('umount not installed!');
my $umount_cmd = [ 'sudo', $umount_path, $self->device ];
$self->log->fatal( "CANNOT UMOUNT " . $self->device . "EXITING" ) && die;
}
$self->clean_up_by_volume($volume);
return;
}
$myd_dir->is_copied(1);
# $snapshot_description .= $myd_dir->name . " ";
}
$self->log->info( "umounting " . $self->device );
my $umount_path = can_run('umount') or $self->log->warn('umount not installed!');
my $umount_cmd = [ 'sudo', $umount_path, $self->device ];
my $wait_time = 0;
$self->ec2->detach_volume( VolumeId => $volume->volume_id );
while ( defined eval { $self->ec2->describe_volumes( VolumeId => $volume->volume_id )->[0]->attachments }
&& $wait_time < 60 )
{
$self->log->info("Waiting 10 seconds for volume to detach");
sleep 10;
$wait_time += 10;
}
my $snapshot_description = $bag_of_dbs->{tag};
$self->log->info("Creating Snapshot");
my $snapshot = $self->ec2->create_snapshot( VolumeId => $volume->volume_id, Description => $snapshot_description );
if ( $snapshot->can('errors') ) {
$self->log->error( "[Snapshot Creation Error] " . $self->pp_ec2_errors( $snapshot->errors ) );
return;
}
else {
$self->log->info( "Created Snapshot ", $snapshot->snapshot_id, " ", $volume->size, " Gb" );
$self->log->info( "Deleting " . $volume->volume_id );
$self->ec2->delete_volume( VolumeId => $volume->volume_id );
$self->log->info("Tagging the Snapshot");
my $tag_path = can_run('ec2-create-tags') or $self->log->warn('ec2-describe-tags not found');
my $tag_cmd = [ $tag_path, $snapshot->snapshot_id, '-t', "Name=$snapshot_description" ];
# $self->log->info("Detaching Temp Volume");
# $self->ec2->detach_volume(VolumeId => $volume->volume_id);
# $self->log->info("Deleting Temp Volume");
# $self->ec2->delete_volume(VolumeId => $volume->volume_id);
}
}