From f6bf84ac789c430ef71123c35a91d70d8162e9b9 Mon Sep 17 00:00:00 2001 From: Hristo Venev Date: Sun, 14 Apr 2024 17:38:19 +0300 Subject: Package for Fedora --- .gitignore | 1 + build-rpm.sh | 14 ++++++++++++++ dist/fedora/pgbak.spec | 33 +++++++++++++++++++++++++++++++++ etc/systemd-pgbak.conf | 3 +++ pgbak.c | 18 +++++++++++++----- 5 files changed, 64 insertions(+), 5 deletions(-) create mode 100755 build-rpm.sh create mode 100644 dist/fedora/pgbak.spec create mode 100644 etc/systemd-pgbak.conf diff --git a/.gitignore b/.gitignore index cf6b926..35794aa 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /pgbak +/build diff --git a/build-rpm.sh b/build-rpm.sh new file mode 100755 index 0000000..bd63808 --- /dev/null +++ b/build-rpm.sh @@ -0,0 +1,14 @@ +#!/bin/bash +set -ex +version=$(sed -n 's/^Version: *\([0-9.]\+\)$/\1/p' < dist/fedora/pgbak.spec) +[[ -n "$version" ]] +if [[ -d "build" ]]; then + rm -r build +fi +mkdir build build/{src,rpms,srpms} +git archive --prefix="pgbak-$version/" HEAD > "build/src/pgbak-$version.tar.xz" +rpmbuild \ + -D"_sourcedir $PWD/build/src" \ + -D"_srcrpmdir $PWD/build/srpms" \ + -D"_rpmdir $PWD/build/rpms" \ + -ba dist/fedora/pgbak.spec diff --git a/dist/fedora/pgbak.spec b/dist/fedora/pgbak.spec new file mode 100644 index 0000000..823e9a9 --- /dev/null +++ b/dist/fedora/pgbak.spec @@ -0,0 +1,33 @@ +Name: pgbak +Version: 0.0.1 +Release: %autorelease +Summary: A robust WAL archiver for PostgreSQL + +License: LGPL-3.0-or-later +URL: https://git.venev.name/hristo/pgbak/about/ +Source0: pgbak-%{version}.tar.xz + +BuildRequires: make +BuildRequires: gcc + +%description +pgbak is a robust WAL archiver for PostgreSQL. It automates compression as well as the creation of full snapshots. + + +%prep +%autosetup + +%build +%make_build + + +%install +install -Dm755 -t "$RPM_BUILD_ROOT"%{_bindir} pgbak +install -Dm755 etc/systemd-pgbak.conf "$RPM_BUILD_ROOT"%{_unitdir}/postgresql.service.d/10-pgbak.conf + + +%files +%{_bindir}/pgbak +%{_unitdir}/postgresql.service.d/10-pgbak.conf +%license LICENSE LICENSE.GPL3 +%doc README.md diff --git a/etc/systemd-pgbak.conf b/etc/systemd-pgbak.conf new file mode 100644 index 0000000..dd841c9 --- /dev/null +++ b/etc/systemd-pgbak.conf @@ -0,0 +1,3 @@ +[Service] +Environment=PGBAK=/var/lib/pgsql/backups +ExecStopPost=-/usr/bin/pgbak try-wait 60 diff --git a/pgbak.c b/pgbak.c index 3fed5eb..c3d84d5 100644 --- a/pgbak.c +++ b/pgbak.c @@ -660,20 +660,28 @@ int main(int argc, char **argv) { return 1; } + const char *op = argv[1]; + bool is_try_wait = !strcmp(op, "try-wait"); + backup_dfd = open(backup_dir, O_RDONLY | O_DIRECTORY | O_CLOEXEC); if(backup_dfd < 0) { + if(is_try_wait && errno == ENOENT) { + return 0; + } fprintf(stderr, "Failed to open $PGBAK directory '%s': %m\n", backup_dir); return 1; } - lck_fd = openat(backup_dfd, "pgbak.lock", O_RDWR | O_CREAT | O_CLOEXEC, 0600); + int lck_flags = O_RDWR | (is_try_wait ? 0 : O_CREAT) | O_CLOEXEC; + lck_fd = openat(backup_dfd, "pgbak.lock", lck_flags, 0600); if(lck_fd < 0) { + if(is_try_wait && errno == ENOENT) { + return 0; + } fprintf(stderr, "Failed to open $PGBAK/pgbak.lock: %m\n"); - _Exit(1); + return 1; } - const char *op = argv[1]; - if(!strcmp(op, "wal")) { if(argc != 3) goto usage; return cmd_wal(argv[2]); @@ -694,7 +702,7 @@ int main(int argc, char **argv) { return cmd_sync(true, true); } - if(!strcmp(op, "wait")) { + if(is_try_wait || !strcmp(op, "wait")) { if(argc != 2 && argc != 3) goto usage; long timeout = -1; if(argc > 2) { -- cgit