Skip to content

Commit

Permalink
chore: allow custom container name for Postgres backups; misc. cleanu…
Browse files Browse the repository at this point in the history
…p of backup scripts
  • Loading branch information
arcanemachine committed Oct 8, 2023
1 parent 0d17aa8 commit bff927c
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ cd /var/lib/postgresql/backups/ || exit 1
# make backup
pg_dump -h localhost -p 5432 -U "$POSTGRES_USER" -F c -b -v -f "phoenix-todo-list--pg-dump-$(date +'%Y-%m-%d-%H-%M-%S').dump" "$POSTGRES_DB"

# remove all local backups older than 2 days
find *.dump -type f -mtime +7 | xargs rm -f
# # remove all local backups older than 7 days
# find *.dump -type f -mtime +7 | xargs rm -f
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#!/bin/sh

show_help() {
echo "This script creates a pg_dump backup. It must be run inside the container.
echo "This script restores a pg_dump backup. It must be run inside the container.
The first positional argument must be the filename of the backup you want to restore.
- DO NOT pass in the full path. Just the filename.
- e.g. 'pg-dump-2022-11-01-08-57-30.dump'"
The first positional argument must be the filename of the backup you want to restore:
- The file must be located in the same directory as this script.
- DO NOT pass in the full path to the file. Just the filename.
- e.g. 'phoenix-todo-list--pg-dump-2022-11-01-08-57-30.dump'"
}

if [ "$1" = "--help" ]; then
Expand Down
24 changes: 19 additions & 5 deletions support/scripts/backups/postgres/backup-create-outside-container
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
#!/bin/bash
#!/bin/sh

# shellcheck disable=SC2153

show_help() {
echo "This script creates a pg_dump backup. It must be run outside the container.
The backup file will be created in the same directory as this script. It will be created with a timestamp in the filename.
- e.g. 'postgres--pg-dump-2022-11-05-06-23-37.dump'
- e.g. 'phoenix-todo-list--pg-dump-2022-11-05-06-23-37.dump'
This script accepts the following environment variable(s):
- POSTGRES_CONTAINER_NAME: The name of the Postgres container to be backed up
- Default: 'phoenix-todo-list_postgres'
To use Podman instead of Docker, pass '--podman' as the last positional argument."
}
Expand All @@ -14,12 +20,20 @@ if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
exit
fi

# configure postgres container name
if [ "$POSTGRES_CONTAINER_NAME" != "" ]; then
postgres_container_name="$POSTGRES_CONTAINER_NAME"
else
postgres_container_name="phoenix-todo-list-postgres-1"
fi

# configure container manager
if [ "$1" = "--podman" ]; then
container_program=podman
container_manager=podman
else
container_program=docker
container_manager=docker
fi

echo "Creating backup in Postgres container volume directory '/var/lib/postgresql/backups/'..."

$container_program exec -it postgres /var/lib/postgresql/backups/backup-create-inside-container
$container_manager exec -it "$postgres_container_name" /var/lib/postgresql/backups/backup-create-inside-container
24 changes: 19 additions & 5 deletions support/scripts/backups/postgres/backup-restore-outside-container
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
#!/bin/sh

# shellcheck disable=SC2153

show_help() {
echo "This script restores a pg_dump backup. It must be run outside the container.
The first positional argument must be the filename of the backup you want to restore.
This script accepts the following environment variable(s):
- POSTGRES_CONTAINER_NAME: The name of the Postgres container to be restored
- Default: 'phoenix-todo-list-postgres-1'
The first positional argument must be the filename of the backup you want to restore. The file must be located in the postgres backup
- DO NOT pass in the full path. Just the filename.
- e.g. 'pg-dump-2022-11-01-08-57-30.dump'
- e.g. 'phoenix-todo-list--pg-dump-2022-11-05-06-23-37.dump'
To use Podman instead of Docker, pass '--podman' as the last positional argument."
}
Expand All @@ -18,10 +24,18 @@ elif [ "$1" = "" ]; then
exit 2
fi

# configure postgres container name
if [ "$POSTGRES_CONTAINER_NAME" != "" ]; then
postgres_container_name="$POSTGRES_CONTAINER_NAME"
else
postgres_container_name="phoenix-todo-list-postgres-1"
fi

# configure container manager
if [ "$1" = "--podman" ]; then
container_program=podman
container_manager=podman
else
container_program=docker
container_manager=docker
fi

$container_program exec -it postgres /var/lib/postgresql/backups/backup-restore-inside-container "$1"
$container_manager exec -it "$postgres_container_name" /var/lib/postgresql/backups/backup-restore-inside-container "$1"
21 changes: 15 additions & 6 deletions support/scripts/backups/postgres/backups-list
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
#!/bin/bash
#!/bin/sh

show_help() {
echo "Lists available backups inside the Postgres container directory '/var/lib/postgresql/backups/'."
echo "Lists available backups inside the Postgres container directory '/var/lib/postgresql/backups/'.
This script accepts the following environment variable(s):
- POSTGRES_VOLUME_NAME: The name of the Postgres volume to be restored
- Default: 'phoenix-todo-list_postgres'
This script accepts the following positional arguments:
--podman - When passed as the last positional argument, use Podman instead of Docker"
}

if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
show_help
exit
elif [ "$1" = "--docker" ]; then
container_program=docker
fi

if [ "$1" = "--podman" ]; then
container_manager=podman
else
container_program=podman
container_manager=docker
fi

printf "Listing available backups in Postgres container volume directory '/var/lib/postgresql/backups/'...\n\n"

$container_program exec -it postgres sh -c 'cd /var/lib/postgresql/backups/ && ls *.dump'
$container_manager exec -it phoenix-todo-list_postgres sh -c 'cd /var/lib/postgresql/backups/ && ls *.dump'

0 comments on commit bff927c

Please sign in to comment.