diff --git a/internal/pgbackrest/postgres.go b/internal/pgbackrest/postgres.go index 2afcd508ff..c8236a3f20 100644 --- a/internal/pgbackrest/postgres.go +++ b/internal/pgbackrest/postgres.go @@ -28,11 +28,12 @@ func PostgreSQL( // - https://pgbackrest.org/user-guide.html#quickstart/configure-archiving // - https://pgbackrest.org/command.html#command-archive-push // - https://www.postgresql.org/docs/current/runtime-config-wal.html - fixTimezone := `sed -E 's/([0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{6}) (UTC|[\+\-][0-9]{2})/\1\2/' | sed 's/UTC/Z/'` - extractCommitTime := `awk '{print $(NF-2) "T" $(NF-1) " " $(NF)}' | ` + fixTimezone + `` - validateCommitTime := `grep -E '^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{6}(Z|[\+\-][0-9]{2}` + + fixTimezone := `sed -E "s/([0-9]{4}-[0-9]{2}-[0-9]{2}) ([0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{6}) (UTC|[\\+\\-][0-9]{2})/\1T\2\3/" | sed "s/UTC/Z/"` + extractCommitTime := `grep -oP "COMMIT \K[^;]+" | ` + fixTimezone + `` + validateCommitTime := `grep -E "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{6}(Z|[\+\-][0-9]{2})$"` archive := `pgbackrest --stanza=` + DefaultStanzaName + ` archive-push "%p"` - archive += ` && timestamp=$(pg_waldump "%p" | grep COMMIT | ` + extractCommitTime + ` | tail -n 1 | ` + validateCommitTime + `)$');` + archive += ` && timestamp=$(pg_waldump "%p" | ` + extractCommitTime + ` | tail -n 1 | ` + validateCommitTime + `);` archive += ` if [ ! -z ${timestamp} ]; then echo ${timestamp} > /pgdata/latest_commit_timestamp.txt; fi` outParameters.Mandatory.Add("archive_mode", "on") diff --git a/internal/pgbackrest/postgres_test.go b/internal/pgbackrest/postgres_test.go index 618b8bff4e..b518e6edd4 100644 --- a/internal/pgbackrest/postgres_test.go +++ b/internal/pgbackrest/postgres_test.go @@ -22,14 +22,14 @@ func TestPostgreSQLParameters(t *testing.T) { assert.DeepEqual(t, parameters.Mandatory.AsMap(), map[string]string{ "archive_mode": "on", "archive_command": strings.Join([]string{ - `pgbackrest --stanza=db archive-push "%p"`, - ` && timestamp=$(pg_waldump "%p" | grep COMMIT | awk '{print $(NF`, - `-2) "T" $(NF-1) " " $(NF)}' | sed -E 's/([0-9]{4}-[0-9]{2}-[0-9]`, - `{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{6}) (UTC|[\+\-][0-9]{2})/\`, - `1\2/' | sed 's/UTC/Z/' | tail -n 1 | grep -E '^[0-9]{4}-[0-9]{2}`, - `-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{6}(Z|[\+\-][0-9]{2})`, - "$'); if [ ! -z ${timestamp} ]; then echo ${timestamp} > /pgdata/l", - "atest_commit_timestamp.txt; fi", + `pgbackrest --stanza=db archive-push "%p" `, + `&& timestamp=$(pg_waldump "%p" | `, + `grep -oP "COMMIT \K[^;]+" | `, + `sed -E "s/([0-9]{4}-[0-9]{2}-[0-9]{2}) ([0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{6}) (UTC|[\\+\\-][0-9]{2})/\1T\2\3/" | `, + `sed "s/UTC/Z/" | `, + "tail -n 1 | ", + `grep -E "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{6}(Z|[\+\-][0-9]{2})$"); `, + "if [ ! -z ${timestamp} ]; then echo ${timestamp} > /pgdata/latest_commit_timestamp.txt; fi", }, ""), "restore_command": `pgbackrest --stanza=db archive-get %f "%p"`, "track_commit_timestamp": "true", @@ -48,14 +48,14 @@ func TestPostgreSQLParameters(t *testing.T) { assert.DeepEqual(t, parameters.Mandatory.AsMap(), map[string]string{ "archive_mode": "on", "archive_command": strings.Join([]string{ - `pgbackrest --stanza=db archive-push "%p"`, - ` && timestamp=$(pg_waldump "%p" | grep COMMIT | awk '{print $(NF`, - `-2) "T" $(NF-1) " " $(NF)}' | sed -E 's/([0-9]{4}-[0-9]{2}-[0-9]`, - `{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{6}) (UTC|[\+\-][0-9]{2})/\`, - `1\2/' | sed 's/UTC/Z/' | tail -n 1 | grep -E '^[0-9]{4}-[0-9]{2}`, - `-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{6}(Z|[\+\-][0-9]{2})`, - "$'); if [ ! -z ${timestamp} ]; then echo ${timestamp} > /pgdata/l", - "atest_commit_timestamp.txt; fi", + `pgbackrest --stanza=db archive-push "%p" `, + `&& timestamp=$(pg_waldump "%p" | `, + `grep -oP "COMMIT \K[^;]+" | `, + `sed -E "s/([0-9]{4}-[0-9]{2}-[0-9]{2}) ([0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{6}) (UTC|[\\+\\-][0-9]{2})/\1T\2\3/" | `, + `sed "s/UTC/Z/" | `, + "tail -n 1 | ", + `grep -E "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{6}(Z|[\+\-][0-9]{2})$"); `, + "if [ ! -z ${timestamp} ]; then echo ${timestamp} > /pgdata/latest_commit_timestamp.txt; fi", }, ""), "restore_command": `pgbackrest --stanza=db archive-get %f "%p" --repo=99`, "track_commit_timestamp": "true",