Skip to content

Commit 3a83853

Browse files
philrhurstPhilip Hurstbenjaminjb
authored
Pgbackrest check and pgcontroldata (#104)
* add pgbackrest check to gatherPgBackRestInfo function * bashCommand function is a simple function to execute a one-line bash command in a container. * capture pg_controldata for each DB host --------- Co-authored-by: Philip Hurst <[email protected]> Co-authored-by: Benjamin Blattberg <[email protected]>
1 parent 78e0830 commit 3a83853

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

internal/cmd/exec.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,23 @@ func (exec Executor) pgBackRestInfo(output, repoNum string) (string, string, err
3939
return stdout.String(), stderr.String(), err
4040
}
4141

42+
// bashCommand defines a one-line bash command to exec in a container
43+
func (exec Executor) bashCommand(command string) (string, string, error) {
44+
var stdout, stderr bytes.Buffer
45+
err := exec(nil, &stdout, &stderr, "bash", "-ceu", "--", command)
46+
return stdout.String(), stderr.String(), err
47+
}
48+
49+
// pgBackRestCheck defines a pgBackRest check command
50+
// Force log-level-console=detail to override if set elsewhere
51+
func (exec Executor) pgBackRestCheck() (string, string, error) {
52+
var stdout, stderr bytes.Buffer
53+
command := "pgbackrest check --log-level-console=detail"
54+
err := exec(nil, &stdout, &stderr, "bash", "-ceu", "--", command)
55+
56+
return stdout.String(), stderr.String(), err
57+
}
58+
4259
// postgresqlListLogFiles returns the full path of numLogs log files.
4360
func (exec Executor) listPGLogFiles(numLogs int) (string, string, error) {
4461
var stdout, stderr bytes.Buffer

internal/cmd/export.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,45 @@ func gatherPostgresLogsAndConfigs(ctx context.Context,
11071107
}
11081108
}
11091109

1110+
// We will execute several bash commands in the DB container
1111+
// text is command to execute and desc is a short description
1112+
type Command struct {
1113+
path string
1114+
description string
1115+
}
1116+
1117+
commands := []Command{
1118+
{path: "pg_controldata", description: "pg_controldata"},
1119+
}
1120+
1121+
var buf bytes.Buffer
1122+
1123+
for _, command := range commands {
1124+
stdout, stderr, err := Executor(exec).bashCommand(command.path)
1125+
if err != nil {
1126+
if apierrors.IsForbidden(err) {
1127+
writeInfo(cmd, err.Error())
1128+
return nil
1129+
}
1130+
writeDebug(cmd, fmt.Sprintf("Error executing %s\n", command.path))
1131+
writeDebug(cmd, fmt.Sprintf("%s\n", err.Error()))
1132+
writeDebug(cmd, "This is acceptable in some configurations.\n")
1133+
continue
1134+
}
1135+
buf.Write([]byte(fmt.Sprintf("%s\n", command.description)))
1136+
buf.Write([]byte(stdout))
1137+
if stderr != "" {
1138+
buf.Write([]byte(stderr))
1139+
}
1140+
buf.Write([]byte("\n\n"))
1141+
}
1142+
1143+
// Write the buffer to a file
1144+
path := clusterName + fmt.Sprintf("/pods/%s/%s", pod.Name, "postgres-info")
1145+
if err := writeTar(tw, buf.Bytes(), path, cmd); err != nil {
1146+
return err
1147+
}
1148+
11101149
}
11111150
return nil
11121151
}
@@ -1518,6 +1557,21 @@ func gatherPgBackRestInfo(ctx context.Context,
15181557
buf.Write([]byte(stderr))
15191558
}
15201559

1560+
buf.Write([]byte("pgbackrest check\n"))
1561+
stdout, stderr, err = Executor(exec).pgBackRestCheck()
1562+
if err != nil {
1563+
if apierrors.IsForbidden(err) {
1564+
writeInfo(cmd, err.Error())
1565+
return nil
1566+
}
1567+
return err
1568+
}
1569+
1570+
buf.Write([]byte(stdout))
1571+
if stderr != "" {
1572+
buf.Write([]byte(stderr))
1573+
}
1574+
15211575
path := clusterName + "/pgbackrest-info"
15221576
return writeTar(tw, buf.Bytes(), path, cmd)
15231577
}

0 commit comments

Comments
 (0)