ClonePgData clones an existing server, given its connection string, to a certain data directory
(ctx context.Context, connectionString, targetPgData, walDir string)
| 38 | // ClonePgData clones an existing server, given its connection string, |
| 39 | // to a certain data directory |
| 40 | func ClonePgData(ctx context.Context, connectionString, targetPgData, walDir string) error { |
| 41 | log.Info("Waiting for server to be available", "connectionString", connectionString) |
| 42 | |
| 43 | db, err := pool.NewDBConnection(connectionString, pool.ConnectionProfilePostgresqlPhysicalReplication) |
| 44 | if err != nil { |
| 45 | return err |
| 46 | } |
| 47 | defer func() { |
| 48 | _ = db.Close() |
| 49 | }() |
| 50 | |
| 51 | err = waitForStreamingConnectionAvailable(ctx, db) |
| 52 | if err != nil { |
| 53 | return fmt.Errorf("source server not available: %v", connectionString) |
| 54 | } |
| 55 | |
| 56 | options := []string{ |
| 57 | "-D", targetPgData, |
| 58 | "-v", |
| 59 | "-w", |
| 60 | "-d", connectionString, |
| 61 | } |
| 62 | |
| 63 | if walDir != "" { |
| 64 | options = append(options, "--waldir", walDir) |
| 65 | } |
| 66 | |
| 67 | pgBaseBackupCmd := exec.Command(pgBaseBackupName, options...) // #nosec |
| 68 | err = execlog.RunStreaming(pgBaseBackupCmd, pgBaseBackupName) |
| 69 | if err != nil { |
| 70 | return fmt.Errorf("error in pg_basebackup, %w", err) |
| 71 | } |
| 72 | |
| 73 | return nil |
| 74 | } |
| 75 | |
| 76 | // Join creates a new instance joined to an existing PostgreSQL cluster |
| 77 | func (info InitInfo) Join(ctx context.Context, cluster *apiv1.Cluster) error { |
no test coverage detected