Beispiel #1
0
func (c *controllerAPI) createAndStreamBackup(ctx context.Context, w http.ResponseWriter, req *http.Request) {
	w.Header().Set("Content-Type", "application/tar")
	filename := "flynn-backup-" + time.Now().UTC().Format("2006-01-02_150405") + ".tar"
	w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", filename))

	handleError := func(err error) {
		if l, ok := ctxhelper.LoggerFromContext(ctx); ok {
			l.Error(err.Error())
			w.WriteHeader(500)
		}
	}

	client, err := controller.NewClient("", c.config.keys[0])
	if err != nil {
		handleError(err)
		return
	}

	b := &ct.ClusterBackup{
		Status: ct.ClusterBackupStatusRunning,
	}
	if err := c.backupRepo.Add(b); err != nil {
		handleError(err)
		return
	}

	h := sha512.New()
	hw := io.MultiWriter(h, w)
	sw := newSizeWriter(hw)

	if err := backup.Run(client, sw, nil); err != nil {
		b.Status = ct.ClusterBackupStatusError
		b.Error = err.Error()
		now := time.Now()
		b.CompletedAt = &now
		if err := c.backupRepo.Update(b); err != nil {
			handleError(err)
			return
		}
		handleError(err)
		return
	}

	b.Status = ct.ClusterBackupStatusComplete
	b.SHA512 = hex.EncodeToString(h.Sum(nil))
	b.Size = int64(sw.Size())
	now := time.Now()
	b.CompletedAt = &now
	if err := c.backupRepo.Update(b); err != nil {
		handleError(err)
	}
}
Beispiel #2
0
func (c *controllerAPI) GetBackup(ctx context.Context, w http.ResponseWriter, req *http.Request) {
	w.Header().Set("Content-Type", "application/tar")
	filename := "flynn-backup-" + time.Now().UTC().Format("2006-01-02_150405") + ".tar"
	w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", filename))
	client, err := controller.NewClient("", c.config.keys[0])
	if err != nil {
		respondWithError(w, err)
		return
	}
	if err := backup.Run(client, w); err != nil {
		respondWithError(w, err)
		return
	}
}
Beispiel #3
0
func runClusterBackup(args *docopt.Args) error {
	client, err := getClusterClient()
	if err != nil {
		return err
	}

	var bar *pb.ProgressBar
	if term.IsTerminal(os.Stderr.Fd()) {
		bar = pb.New(0)
		bar.SetUnits(pb.U_BYTES)
		bar.ShowBar = false
		bar.ShowSpeed = true
		bar.Output = os.Stderr
		bar.Start()
	}

	var dest io.Writer = os.Stdout
	if filename := args.String["--file"]; filename != "" {
		f, err := os.Create(filename)
		if err != nil {
			return err
		}
		defer f.Close()
		dest = f
	}

	fmt.Fprintln(os.Stderr, "Creating cluster backup...")

	if bar != nil {
		dest = io.MultiWriter(dest, bar)
	}
	if err := backup.Run(client, dest); err != nil {
		return err
	}
	if bar != nil {
		bar.Finish()
	}
	fmt.Fprintln(os.Stderr, "Backup complete.")

	return nil
}