Skip to content

Commit

Permalink
Merge pull request #23 from riotkit-org/release-1.0.0
Browse files Browse the repository at this point in the history
Release 1.0.0
  • Loading branch information
blackandred authored Jan 21, 2023
2 parents 1ffff2b + 47348f7 commit e9e7e0b
Show file tree
Hide file tree
Showing 20 changed files with 266 additions and 485 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tag.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build
run: "make dockerfile assets_build build GO_VERSION=${{ env.GO_VERSION }} POSTGRES_VERSION=${{ matrix.POSTGRES_VERSION }}"
run: "make build GO_VERSION=${{ env.GO_VERSION }} POSTGRES_VERSION=${{ matrix.POSTGRES_VERSION }}"

- name: Build docker image and push release image
run: "make ci_rename_release_binary ci_release_snapshot ci_release POSTGRES_VERSION=${{ matrix.POSTGRES_VERSION }}"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build
run: "make dockerfile assets_build build ci_check_embedded_binaries GO_VERSION=${{ env.GO_VERSION }} POSTGRES_VERSION=${{ matrix.POSTGRES_VERSION }}"
run: "make build GO_VERSION=${{ env.GO_VERSION }} POSTGRES_VERSION=${{ matrix.POSTGRES_VERSION }}"

- name: Test
run: "make test POSTGRES_VERSION=${{ matrix.POSTGRES_VERSION }}"
Expand Down
26 changes: 26 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
ARG POSTGRES_VERSION
ARG GO_VERSION

# =============================================
# Build application && embed PostgreSQL in it
# =============================================
FROM golang:${GO_VERSION} as builder

ADD . /workspace
RUN apt-get update && apt-get install make -y

RUN cd /workspace && \
make build


# ============================================================
# Create target image basing on a specific PostgreSQL version
# ============================================================
FROM postgres:${POSTGRES_VERSION}

RUN apt-get update && apt-get install -y gpg

COPY --from=builder /workspace/.build/pgbr /usr/bin/pgbr
RUN chmod +x /usr/bin/pgbr

ENTRYPOINT ["/usr/bin/pgbr"]
24 changes: 3 additions & 21 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,33 +1,15 @@
include test.mk
include ci.mk

PG_DATA=assets/.build/data

copy_libs_and_executables:
mkdir -p ${PG_DATA}
cp -p /usr/lib/postgresql/${POSTGRES_VERSION}/bin/psql ${PG_DATA}/psql
cp -p /usr/lib/postgresql/${POSTGRES_VERSION}/bin/pg_dumpall ${PG_DATA}/pg_dumpall
cp -p /usr/lib/postgresql/${POSTGRES_VERSION}/bin/pg_dump ${PG_DATA}/pg_dump
cp -p /usr/lib/postgresql/${POSTGRES_VERSION}/bin/pg_restore ${PG_DATA}/pg_restore

./hack/get-binary-with-libs.py /usr/lib/postgresql/${POSTGRES_VERSION}/bin/psql ./${PG_DATA}
./hack/get-binary-with-libs.py /usr/lib/postgresql/${POSTGRES_VERSION}/bin/pg_dump ./${PG_DATA}
./hack/get-binary-with-libs.py /usr/lib/postgresql/${POSTGRES_VERSION}/bin/pg_dumpall ./${PG_DATA}
./hack/get-binary-with-libs.py /usr/lib/postgresql/${POSTGRES_VERSION}/bin/pg_restore ./${PG_DATA}

clean:
rm -rf .build/*

assets_build: ## Builds PostgreSQL assets using docker
docker build . -f build.Dockerfile --build-arg POSTGRES_VERSION=${POSTGRES_VERSION} --build-arg GO_VERSION=${GO_VERSION} -t build
@docker rm -f builder 2>/dev/null
docker create --name builder build
docker cp builder:/workspace/.build ./
docker cp builder:/workspace/assets ./

build:
CGO_ENABLED=0 GO111MODULE=on go build -tags nomemcopy -o ./.build/pgbr
chmod +x ./.build/pgbr

test:
export PGBR_USE_CONTAINER=true; \
export PGBR_CONTAINER_IMAGE=bitnami/postgresql; \
export POSTGRES_VERSION=${POSTGRES_VERSION}; \
go test -v ./...
102 changes: 17 additions & 85 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,15 @@ pgbr
PostgreSQL simple backup & restore helper tool created for usage with Backup Repository, but can be used also standalone.

**Features:**
- `psql`, `pg_dump`, `pg_dumpall`, `pg_restore` packaged **in a single binary**, available as subcommands e.g. `pgbr psql -- -h 127.0.0.1 -d riotkit -c "SELECT 1"`
- Minimum system requirements, no extra binaries or libraries and **even no PostgreSQL client is required, just this one binary**
- Opinionated backup & restore commands basing on PostgreSQL built-in commands

**Requirements:**
- [patchelf](https://github.com/NixOS/patchelf)
- Linux
- Linux (x86_64/amd64 architecture)
- PostgreSQL in desired version

Conception
----------

**Single-binary**

`pgbr` binary has compiled PostgreSQL tools, including libc (musl/glibc) and dynamic libraries, everything is unpacked into temporary directory, then `patchelf` patches
interpreter to match `ld-linux` or `ld-musl` at selected path and `pgbr` is invoking a process with extra `LD_LIBRARY_PATH` environment set.

**Sensible defaults**

Backup & Restore should be simple and fault-tolerant, that's why this tool is automating basic things like disconnecting clients, or connecting
Expand Down Expand Up @@ -53,82 +46,6 @@ Procedure:
cat dump.gz | ./.build/pgbr db restore --password riotkit --user riotkit --connection-database=postgres
```

psql & pg_dump & pg_dumpall & pg_restore
----------------------------------------

- psql: `pgbr psql -- ...`
- pg_dump: `pgbr pg_dump -- ...`
- pg_dumpall: `pgbr pg_dumpall -- ...`
- pg_restore: `pgbr pg_restore -- ...`

```bash
pgbr psql -- --help

#psql is the PostgreSQL interactive terminal.
#
#Usage:
# psql [OPTION]... [DBNAME [USERNAME]]
#
#General options:
# -c, --command=COMMAND run only single command (SQL or internal) and exit
# -d, --dbname=DBNAME database name to connect to (default: "damian")
# -f, --file=FILENAME execute commands from file, then exit
# -l, --list list available databases, then exit
# -v, --set=, --variable=NAME=VALUE
# set psql variable NAME to VALUE
# (e.g., -v ON_ERROR_STOP=1)
# -V, --version output version information, then exit
# -X, --no-psqlrc do not read startup file (~/.psqlrc)
# -1 ("one"), --single-transaction
# execute as a single transaction (if non-interactive)
# -?, --help[=options] show this help, then exit
# --help=commands list backslash commands, then exit
# --help=variables list special variables, then exit
#
#Input and output options:
# -a, --echo-all echo all input from script
# -b, --echo-errors echo failed commands
# -e, --echo-queries echo commands sent to server
# -E, --echo-hidden display queries that internal commands generate
# -L, --log-file=FILENAME send session log to file
# -n, --no-readline disable enhanced command line editing (readline)
# -o, --output=FILENAME send query results to file (or |pipe)
# -q, --quiet run quietly (no messages, only query output)
# -s, --single-step single-step mode (confirm each query)
# -S, --single-line single-line mode (end of line terminates SQL command)
#
#Output format options:
# -A, --no-align unaligned table output mode
# --csv CSV (Comma-Separated Values) table output mode
# -F, --field-separator=STRING
# field separator for unaligned output (default: "|")
# -H, --html HTML table output mode
# -P, --pset=VAR[=ARG] set printing option VAR to ARG (see \pset command)
# -R, --record-separator=STRING
# record separator for unaligned output (default: newline)
# -t, --tuples-only print rows only
# -T, --table-attr=TEXT set HTML table tag attributes (e.g., width, border)
# -x, --expanded turn on expanded table output
# -z, --field-separator-zero
# set field separator for unaligned output to zero byte
# -0, --record-separator-zero
# set record separator for unaligned output to zero byte
#
#Connection options:
# -h, --host=HOSTNAME database server host or socket directory (default: "local socket")
# -p, --port=PORT database server port (default: "5432")
# -U, --username=USERNAME database user name (default: "damian")
# -w, --no-password never prompt for password
# -W, --password force password prompt (should happen automatically)
#
#For more information, type "\?" (for internal commands) or "\help" (for SQL
#commands) from within psql, or consult the psql section in the PostgreSQL
#documentation.
#
#Report bugs to <pgsql-bugs@lists.postgresql.org>.
#PostgreSQL home page: <https://www.postgresql.org/>
```

Passing extra arguments
-----------------------

Expand All @@ -139,3 +56,18 @@ Both `pgbr db backup` and `pgbr db restore` are supporting UNIX-like parameters
```bash
pgbr db backup --password riotkit --user riotkit -- --role=my-role > dump.gz
```

Using tooling from docker image
-------------------------------

`pgbr` can rely on your host OS or use `docker run` to trigger `psql`, `pg_dump`, `pg_dumpall` or `pg_restore`.

In order to use a docker image that will provide client tools you can set those environment variables:

```bash
export PGBR_USE_CONTAINER=true
export POSTGRES_VERSION=15

# optional: image name
export PGBR_CONTAINER_IMAGE=my-registry/image-name
```
Empty file removed assets/.build/data/.gitkeep
Empty file.
122 changes: 0 additions & 122 deletions assets/extraction.go

This file was deleted.

31 changes: 0 additions & 31 deletions assets/unpacking.go

This file was deleted.

29 changes: 0 additions & 29 deletions build.Dockerfile

This file was deleted.

Loading

0 comments on commit e9e7e0b

Please sign in to comment.