clientv3/snapshot/v3_snapshot.go
func (s *v3Manager) Save(ctx context.Context, cfg clientv3.Config, dbPath string) error {
	if len(cfg.Endpoints) != 1 {
		return fmt.Errorf("snapshot must be requested to one selected node, not multiple %v", cfg.Endpoints)
	}
	cli, err := clientv3.New(cfg)
	if err != nil {
		return err
	}
	defer cli.Close()

	partpath := dbPath + ".part"
	defer os.RemoveAll(partpath)

	var f *os.File
	f, err = os.OpenFile(partpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, fileutil.PrivateFileMode)
	if err != nil {
		return fmt.Errorf("could not open %s (%v)", partpath, err)
	}
	s.lg.Info(
		"created temporary db file",
		zap.String("path", partpath),
	)

	now := time.Now()
	var rd io.ReadCloser
	rd, err = cli.Snapshot(ctx)
	if err != nil {
		return err
	}
	s.lg.Info(
		"fetching snapshot",
		zap.String("endpoint", cfg.Endpoints[0]),
	)
	if _, err = io.Copy(f, rd); err != nil {
		return err
	}
	if err = fileutil.Fsync(f); err != nil {
		return err
	}
	if err = f.Close(); err != nil {
		return err
	}
	s.lg.Info(
		"fetched snapshot",
		zap.String("endpoint", cfg.Endpoints[0]),
		zap.Duration("took", time.Since(now)),
	)

	if err = os.Rename(partpath, dbPath); err != nil {
		return fmt.Errorf("could not rename %s to %s (%v)", partpath, dbPath, err)
	}
	s.lg.Info("saved", zap.String("path", dbPath))
	return nil
}
func (s *v3Manager) Save(ctx context.Context, cfg clientv3.Config, dbPath string) error {
	if len(cfg.Endpoints) != 1 {
		return fmt.Errorf("snapshot must be requested to one selected node, not multiple %v", cfg.Endpoints)
	}
	cli, err := clientv3.New(cfg)
	if err != nil {
		return err
	}
	defer cli.Close()

	partpath := dbPath + ".part"
	defer os.RemoveAll(partpath)

	var f *os.File
	f, err = os.OpenFile(partpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, fileutil.PrivateFileMode)
	if err != nil {
		return fmt.Errorf("could not open %s (%v)", partpath, err)
	}
	s.lg.Info(
		"created temporary db file",
		zap.String("path", partpath),
	)

	now := time.Now()
	var rd io.ReadCloser
	rd, err = cli.Snapshot(ctx)
	if err != nil {
		return err
	}
	s.lg.Info(
		"fetching snapshot",
		zap.String("endpoint", cfg.Endpoints[0]),
	)
	if _, err = io.Copy(f, rd); err != nil {
		return err
	}
	if err = fileutil.Fsync(f); err != nil {
		return err
	}
	if err = f.Close(); err != nil {
		return err
	}
	s.lg.Info(
		"fetched snapshot",
		zap.String("endpoint", cfg.Endpoints[0]),
		zap.Duration("took", time.Since(now)),
	)

	if err = os.Rename(partpath, dbPath); err != nil {
		return fmt.Errorf("could not rename %s to %s (%v)", partpath, dbPath, err)
	}
	s.lg.Info("saved", zap.String("path", dbPath))
	return nil
}
func ( s * v3Manager ) Save ( ctx context . Context , cfg clientv3 . Config , dbPath string ) error { if len ( cfg . Endpoints ) != 1 { return fmt . Errorf ( " " , cfg . Endpoints ) } cli , err := clientv3 . New ( cfg ) if err != nil { return err } defer cli . Close ( ) partpath := dbPath + " " defer os . RemoveAll ( partpath ) var f * os . File f , err = os . OpenFile ( partpath , os . O_WRONLY | os . O_CREATE | os . O_TRUNC , fileutil . PrivateFileMode ) if err != nil { return fmt . Errorf ( " " , partpath , err ) } s . lg . Info ( " " , zap . String ( " " , partpath ) , ) now := time . Now ( ) var rd io . ReadCloser rd , err = cli . Snapshot ( ctx ) if err != nil { return err } s . lg . Info ( " " , zap . String ( " " , cfg . Endpoints [ 0 ] ) , ) if _ , err = io . Copy ( f , rd ) ; err != nil { return err } if err = fileutil . Fsync ( f ) ; err != nil { return err } if err = f . Close ( ) ; err != nil { return err } s . lg . Info ( " " , zap . String ( " " , cfg . Endpoints [ 0 ] ) , zap . Duration ( " " , time . Since ( now ) ) , ) if err = os . Rename ( partpath , dbPath ) ; err != nil { return fmt . Errorf ( " " , partpath , dbPath , err ) } s . lg . Info ( " " , zap . String ( " " , dbPath ) ) return nil }
// Save fetches snapshot from remote etcd server and saves data to target path.
Save fetches snapshot from remote etcd server and saves data to target path .