From bff927c30b48c183f116986bbe46351e29755ab0 Mon Sep 17 00:00:00 2001 From: Nicholas Moen Date: Sat, 7 Oct 2023 19:25:21 -0600 Subject: [PATCH] chore: allow custom container name for Postgres backups; misc. cleanup of backup scripts --- .../postgres/backup-create-inside-container | 4 ++-- .../postgres/backup-restore-inside-container | 9 +++---- .../postgres/backup-create-outside-container | 24 +++++++++++++++---- .../postgres/backup-restore-outside-container | 24 +++++++++++++++---- support/scripts/backups/postgres/backups-list | 21 +++++++++++----- 5 files changed, 60 insertions(+), 22 deletions(-) diff --git a/support/containers/volumes/backups/postgres/backup-create-inside-container b/support/containers/volumes/backups/postgres/backup-create-inside-container index 5d07f10..a908f04 100755 --- a/support/containers/volumes/backups/postgres/backup-create-inside-container +++ b/support/containers/volumes/backups/postgres/backup-create-inside-container @@ -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 diff --git a/support/containers/volumes/backups/postgres/backup-restore-inside-container b/support/containers/volumes/backups/postgres/backup-restore-inside-container index 68e49e9..1b762cc 100755 --- a/support/containers/volumes/backups/postgres/backup-restore-inside-container +++ b/support/containers/volumes/backups/postgres/backup-restore-inside-container @@ -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 diff --git a/support/scripts/backups/postgres/backup-create-outside-container b/support/scripts/backups/postgres/backup-create-outside-container index 57cc2da..97490b8 100755 --- a/support/scripts/backups/postgres/backup-create-outside-container +++ b/support/scripts/backups/postgres/backup-create-outside-container @@ -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." } @@ -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 diff --git a/support/scripts/backups/postgres/backup-restore-outside-container b/support/scripts/backups/postgres/backup-restore-outside-container index 090c7e1..e90fbbd 100755 --- a/support/scripts/backups/postgres/backup-restore-outside-container +++ b/support/scripts/backups/postgres/backup-restore-outside-container @@ -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." } @@ -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" diff --git a/support/scripts/backups/postgres/backups-list b/support/scripts/backups/postgres/backups-list index cab9ff5..168011e 100755 --- a/support/scripts/backups/postgres/backups-list +++ b/support/scripts/backups/postgres/backups-list @@ -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'