func createSnapshot(svc *ec2.EC2, volumeId string) {
	description := descriptionPrefix + time.Now().Local().String()
	svc.CreateSnapshot(&ec2.CreateSnapshotInput{
		VolumeId:    &volumeId,
		Description: &description,
	})
}
예제 #2
0
func CreateSnapshot(v *string, i string, c *ec2.EC2) *string {
	params := &ec2.CreateSnapshotInput{
		VolumeId:    aws.String(*v), // Required
		Description: aws.String("Snapshot for :" + i),
		DryRun:      aws.Bool(false),
	}
	SnapshotStatus, err := c.CreateSnapshot(params)
	if err != nil {
		fmt.Println("Error Occured :", err.Error())
		os.Exit(1)
	}
	return SnapshotStatus.SnapshotId
}
예제 #3
0
파일: main.go 프로젝트: porjo/auto-snapshot
func CreateSnapshots(svc *ec2.EC2) error {
	volumes, err := GetBackupVolumes(svc)
	if err != nil {
		return err
	}

	if len(volumes) == 0 {
		log.Printf("No volumes found matching tags: %s\n", tags)
	}

	for i, volume := range volumes {
		csi := ec2.CreateSnapshotInput{}
		csi.VolumeId = volume.VolumeId
		volname, _ := getTagValue("Name", volume.Tags)
		var description string
		if volname == "" {
			description = fmt.Sprintf("%s: %s", *tagPrefix, *volume.VolumeId)
		} else {
			description = fmt.Sprintf("%s: %s (%s)", *tagPrefix, volname, *volume.VolumeId)
		}
		csi.Description = &description

		var err error
		var snap *ec2.Snapshot
		retries := 0
		for {
			snap, err = svc.CreateSnapshot(&csi)
			if err != nil {
				if aerr, ok := err.(awserr.Error); ok {
					retries++
					if retries > MaxSnapshotRetries {
						return fmt.Errorf("Maximum snapshot retries reached for volume %s", *volume.VolumeId)
					}
					if aerr.Code() == "SnapshotCreationPerVolumeRateExceeded" {
						sleep := time.Duration(MinSnapshotInterval+math.Pow(float64(5), float64(retries))) * time.Second
						log.Printf("SnapshotCreationPerVolumeRateExceeded, sleeping for %f\n", sleep.Seconds())
						time.Sleep(sleep)
					}
				} else {
					return err
				}
			} else {
				break
			}
		}

		log.Printf("snapshotting volume, Name: '%s', VolumeId: %s, Size: %d GiB\n", volname, *volume.VolumeId, *volume.Size)

		err = CreateSnapshotTags(svc, *snap.SnapshotId, volname, *volume.VolumeId)
		if err != nil {
			return err
		}

		// wait before kicking off next snapshot
		if i < (len(volumes) - 1) {
			time.Sleep(time.Duration(MinSnapshotInterval) * time.Second)
		}
	}

	return nil
}