simplify the master<->slave first synchronisation

This commit is contained in:
asyncnomi 2025-08-02 23:25:16 +02:00
parent fd3c1e8537
commit 84c8e901de

View file

@ -62,24 +62,12 @@ in
}; };
}; };
# This preStart script sync the slaves to the master # This preStart script sync the slaves to the master
# systemd.services.<name>.preStart has a mergeable type # systemd.services.<name>.preStart has a mergeable type,
systemd.services.postgresql.preStart = lib.mkIf (builtins.elem myName mapping.db.slaves) '' # this code will be prepend to the default one
if test -e ${cfg.dataDir}/.first_startup; then systemd.services.postgresql.preStart = lib.mkIf (builtins.elem myName mapping.db.slaves) (lib.mkBefore ''
if ! test -e ${cfg.dataDir}/PG_VERSION; then
echo "Setting up PostgreSQL slave replication..." echo "Setting up PostgreSQL slave replication..."
# This is a sl that's defined by the default preStart script
# We need an empty dataDir for the pg_basebackup
# And there is no easy ways that I'm aware of
# to get the hash of that file without recomputing it
PSQL_CONF_PATH="${cfg.dataDir}/postgresql.conf"
PSQL_CONF_TARGET=$(readlink "$PSQL_CONF_PATH")
# Remove data dir
if [ -d "${cfg.dataDir}" ]; then
echo "Deleting postgres data dir: ${cfg.dataDir}"
rm -rf "${cfg.dataDir}"
fi
# Perform base backup from master # Perform base backup from master
echo "Starting base backup from master: ${masterIP}" echo "Starting base backup from master: ${masterIP}"
${cfg.package}/bin/pg_basebackup \ ${cfg.package}/bin/pg_basebackup \
@ -91,12 +79,9 @@ in
-Xs \ -Xs \
-R -R
# Symlink back the psql configFile
ln -sf "$PSQL_CONF_TARGET" "$PSQL_CONF_PATH"
echo "PostgreSQL slave setup completed successfully" echo "PostgreSQL slave setup completed successfully"
else else
echo "PostgreSQL standby already configured (standby.signal exists), skipping base backup" echo "PostgreSQL standby already configured (standby.signal exists), skipping base backup"
fi fi
''; '');
} }