← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~cjwatson/launchpad:charm-postgresql-extras-push-backups-primary-only into launchpad:master

 

Colin Watson has proposed merging ~cjwatson/launchpad:charm-postgresql-extras-push-backups-primary-only into launchpad:master.

Commit message:
charm: Only run push-backups on primary database units

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~cjwatson/launchpad/+git/launchpad/+merge/456278

We don't want to be pushing backups from more than one unit.
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad:charm-postgresql-extras-push-backups-primary-only into launchpad:master.
diff --git a/charm/launchpad-postgresql-extras/files/push-backups b/charm/launchpad-postgresql-extras/files/push-backups
index 8a48574..9e2989d 100755
--- a/charm/launchpad-postgresql-extras/files/push-backups
+++ b/charm/launchpad-postgresql-extras/files/push-backups
@@ -9,6 +9,18 @@ from argparse import ArgumentParser
 from pathlib import Path
 
 
+def is_primary():
+    return (
+        subprocess.run(
+            ["psql", "-At", "-c", "SELECT pg_is_in_recovery();"],
+            capture_output=True,
+            check=True,
+            text=True,
+        ).stdout.strip()
+        == "f"
+    )
+
+
 def find_latest_dumps(backups_path, database, count):
     yield from sorted(backups_path.glob(f"{glob.escape(database)}.*.dump"))[
         -count:
@@ -49,6 +61,9 @@ def main():
     parser.add_argument("databases", metavar="database", nargs="+")
     args = parser.parse_args()
 
+    if not is_primary():
+        return
+
     command = ["rsync"]
     if args.bwlimit is not None:
         command.append(f"--bwlimit={args.bwlimit}")