Ejemplo n.º 1
0
func (runner *Runner) uploadBackupFile(backupFilePath, method, dstPath string) (backuper.UploadResult, error) {
	var errMsg string
	_, name := filepath.Split(backupFilePath + ".tar.gz.encrypted")
	beginTime := time.Now()

	if method == "" {
		method = "s3"
	}

	switch method {
	case "ftp":
		ftpProvider := (*runner.secretConfig)["FTP"]
		err := hmutil.FTPUpload(
			21,
			ftpProvider["host"],
			ftpProvider["user"],
			ftpProvider["passw"],
			backupFilePath+".tar.gz.encrypted",
			ftpProvider["upload_path"]+name, // fix it
		)

		if err == nil {
			errMsg = ""
		} else {
			errMsg = err.Error()
		}

		return backuper.UploadResult{
			Err:         errMsg,
			Path:        ftpProvider["upload_path"] + name,
			Type:        "ftp",
			Destination: ftpProvider["user"] + "@" + ftpProvider["host"],
			BeginTime:   beginTime,
			EndTime:     time.Now(),
		}, err

	case "ssh":
		sshProvider := (*runner.secretConfig)["SSH"]

		_, err := hmutil.SSHExec(hmutil.SSHUploader(
			22,
			sshProvider["id_rsa"],
			sshProvider["host"],
			sshProvider["user"],
			backupFilePath+".tar.gz.encrypted",
			sshProvider["upload_path"],
		))

		if err == nil {
			errMsg = ""
		} else {
			errMsg = err.Error()
		}

		return backuper.UploadResult{
			Err:         errMsg,
			Path:        sshProvider["upload_path"] + name,
			Type:        "SSH",
			Destination: sshProvider["user"] + "@" + sshProvider["host"],
			BeginTime:   beginTime,
			EndTime:     time.Now(),
		}, err
	case "s3":
		awsProvider := (*runner.secretConfig)["AWS"]
		err := hmutil.UploadToS3(
			s3gof3r.Keys{
				AccessKey: awsProvider["AWS_ACCESS_KEY_ID"],
				SecretKey: awsProvider["AWS_SECRET_ACCESS_KEY"],
			},
			"headmade",
			backupFilePath+".tar.gz.encrypted",
		)

		if err == nil {
			errMsg = ""
		} else {
			errMsg = err.Error()
		}

		return backuper.UploadResult{
			Err:         errMsg,
			Path:        name,
			Type:        "S3",
			Destination: "headmade",
			BeginTime:   beginTime,
			EndTime:     time.Now(),
		}, err
	default:
		return backuper.UploadResult{}, errors.New("Unknown method")
	}

	// cmd := fmt.Sprintf(
	// 	"AWS_ACCESS_KEY_ID=%s AWS_SECRET_ACCESS_KEY=%s gobackuper_gof3r put -p %s -b %s -k %s",
	// 	awsProvider["AWS_ACCESS_KEY_ID"],
	// 	awsProvider["AWS_SECRET_ACCESS_KEY"],
	// 	backupFilePath + ".tar.gz.encrypted",
	// 	bucket,
	// 	dstPath + ".tar.gz.encrypted",
	// 	// runner.formatDstPath(dstPath),
	// )

	// return hmutil.System(cmd)
	// return nil, hmutil.UploadToS3(
	// 	s3gof3r.Keys{
	// 		AccessKey: awsProvider["AWS_ACCESS_KEY_ID"],
	// 		SecretKey: awsProvider["AWS_SECRET_ACCESS_KEY"],
	// 	},
	// 	"headmade",
	// 	backupFilePath + ".tar.gz.encrypted",
	// )
}
Ejemplo n.º 2
0
func retrieveAction(c *cli.Context) {
	conf, err := config.New()
	if err != nil {
		log.Fatal(err)
	}

	if !conf.Local {
		cl, err := client.NewClient(BackendAddr())
		if err != nil {
			log.Fatal(err)
		}

		var backup backuper.BackupResult
		id, err := strconv.Atoi(c.Args()[0])
		if err != nil {
			log.Fatal(err)
		}

		if err = cl.GetBackup(&backup, id); err != nil {
			log.Fatal(err)
		}

		fmt.Println(backup.Upload)

		_, name := filepath.Split(backup.Upload.Path)
		name = strings.Split(name, ".encrypted")[0]
		switch backup.Upload.Type {
		case "ssh":
			addr := strings.Split(backup.Upload.Destination, "@")
			login, host := addr[0], addr[1]
			ssh := hmutil.SSHDownloader(
				22,
				conf.Secret["SSH"]["id_rsa"],
				host,
				login,
				backup.Upload.Path,
				conf.Secret["SSH"]["download_path"],
			)
			result, err := hmutil.SSHExec(ssh)

			if err != nil {
				log.Fatal(err)
			}

			decoded_result, err := hmutil.Decode(&result, []byte(conf.Secret["encryption"]["pass"]))

			if err != nil {
				log.Fatal(err)
			}

			if err = hmutil.WriteToFile(conf.Secret["SSH"]["download_path"]+name, decoded_result); err != nil {
				log.Fatal(err)
			}
		case "ftp":
			addr := strings.Split(backup.Upload.Destination, "@")
			login, host := addr[0], addr[1]
			result, err := hmutil.FTPDownload(21, host, login, conf.Secret["FTP"]["passw"], backup.Upload.Path)

			if err != nil {
				log.Fatal(err)
			}

			decoded_result, err := hmutil.Decode(&result, []byte(conf.Secret["encryption"]["pass"]))

			if err != nil {
				log.Fatal(err)
			}

			if err = hmutil.WriteToFile(conf.Secret["FTP"]["download_path"]+name, decoded_result); err != nil {
				log.Fatal(err)
			}
		case "S3":
			result, err := hmutil.DownloadFromS3(
				s3gof3r.Keys{
					AccessKey: conf.Secret["AWS"]["AWS_ACCESS_KEY_ID"],
					SecretKey: conf.Secret["AWS"]["AWS_SECRET_ACCESS_KEY"],
				},
				backup.Upload.Destination,
				backup.Upload.Path,
			)

			if err != nil {
				log.Fatal(err)
			}

			decoded_result, err := hmutil.Decode(&result, []byte(conf.Secret["encryption"]["pass"]))

			if err != nil {
				log.Fatal(err)
			}

			if err = hmutil.WriteToFile(conf.Secret["AWS"]["download_path"]+name, decoded_result); err != nil {
				log.Fatal(err)
			}
		}
	}
}