37baabe134
* EE-384: add endpoint to set auto backup (#224) * EE-383: add endpoint to fetch backup settings (#231) * add get backup settings handler * add api docs desc * EE-382: restore from s3 (#233) * EE-381: add GET backup status handler (#234) * EE-385: Add S3 backup execute handler (#237) * add s3 backup execute handler * refactories inside `./api/backup/backup_scheduler.go` and `./api/backup/backup_scheduler.go` * fix tests * EE-375: added backup to S3 form * EE-376: added restore from S3 form * EE-377: Update Home screen to display last backup run status * update backup service with back end endpoints. * restart admin monitor during s3 restores * use go 1.13 * go 1.13 compatibility * EE-375: added cron-validator lib * EE-375: using enum to compare form types * EE-375: validate cron rule field * try fix windows build * EE-375 EE-376 backup and restore forms validation changes * fix(autobackup): update autobackup settings validation rules (#260) * fix(autobackup): automate backup to s3 fe update (#261) * EE-292: fixed typo in property. * EE-292: updated auto backup front end validation. * EE-292: updated lib to validate cron rule in front end * fix dependencies * bumped libcompose version Co-authored-by: Hui <arris_li@hotmail.com> Co-authored-by: Felix Han <felix.han@portainer.io> Co-authored-by: fhanportainer <79428273+fhanportainer@users.noreply.github.com>
61 lines
1.6 KiB
Go
61 lines
1.6 KiB
Go
package s3
|
|
|
|
import (
|
|
"io"
|
|
"log"
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
"github.com/aws/aws-sdk-go/aws/credentials"
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
|
"github.com/aws/aws-sdk-go/service/s3"
|
|
"github.com/aws/aws-sdk-go/service/s3/s3manager"
|
|
"github.com/pkg/errors"
|
|
portainer "github.com/portainer/portainer/api"
|
|
)
|
|
|
|
func NewSession(region string, accessKeyID string, secretAccessKey string) (*session.Session, error) {
|
|
sess, err := session.NewSessionWithOptions(session.Options{
|
|
Config: aws.Config{
|
|
Region: aws.String(region),
|
|
Credentials: credentials.NewStaticCredentials(accessKeyID, secretAccessKey, "")},
|
|
})
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "failed to create AWS S3 session")
|
|
}
|
|
|
|
return sess, nil
|
|
}
|
|
|
|
func Upload(sess *session.Session, r io.Reader, bucketname string, filename string) error {
|
|
s3Uploader := s3manager.NewUploader(sess)
|
|
|
|
out, err := s3Uploader.Upload(&s3manager.UploadInput{
|
|
Bucket: aws.String(bucketname),
|
|
Key: aws.String(filename),
|
|
Body: r,
|
|
})
|
|
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to upload the backup")
|
|
}
|
|
|
|
log.Printf("[DEBUG] upload backup to: %s \n", out.Location)
|
|
return nil
|
|
}
|
|
|
|
func Download(sess *session.Session, w io.WriterAt, settings portainer.S3Location) error {
|
|
downloader := s3manager.NewDownloader(sess)
|
|
|
|
_, err := downloader.Download(w, &s3.GetObjectInput{
|
|
Bucket: aws.String(settings.BucketName),
|
|
Key: aws.String(settings.Filename),
|
|
})
|
|
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to download the backup")
|
|
}
|
|
|
|
log.Printf("[DEBUG] downloaded backup from: https://%s.s3.%s.amazonaws.com/%s \n", settings.BucketName, settings.Region, settings.Filename)
|
|
return nil
|
|
}
|