mirror of
https://github.com/nextcloud/all-in-one.git
synced 2025-12-20 06:26:57 +00:00
Initial import
This commit is contained in:
commit
2295a33590
884 changed files with 93939 additions and 0 deletions
24
Containers/postgresql/Dockerfile
Normal file
24
Containers/postgresql/Dockerfile
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
# From https://github.com/docker-library/postgres/blob/master/13/buster/Dockerfile
|
||||
FROM postgres:13-buster
|
||||
|
||||
RUN set -ex; \
|
||||
\
|
||||
apt-get update; \
|
||||
apt-get install -y --no-install-recommends \
|
||||
openssl \
|
||||
; \
|
||||
rm -rf /var/lib/apt/lists/*
|
||||
|
||||
COPY start.sh /usr/bin/
|
||||
RUN chmod +x /usr/bin/start.sh
|
||||
|
||||
RUN mkdir /mnt/data; \
|
||||
chown postgres:postgres /mnt/data;
|
||||
|
||||
VOLUME /mnt/data
|
||||
|
||||
# Give root a random password
|
||||
RUN echo "root:$(openssl rand -base64 12)" | chpasswd
|
||||
|
||||
USER postgres
|
||||
ENTRYPOINT ["start.sh"]
|
||||
87
Containers/postgresql/start.sh
Normal file
87
Containers/postgresql/start.sh
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Variables
|
||||
DATADIR="/var/lib/postgresql/data"
|
||||
DUMP_DIR="/mnt/data"
|
||||
DUMP_FILE="$DUMP_DIR/database-dump.sql"
|
||||
export PGPASSWORD="$POSTGRES_PASSWORD"
|
||||
|
||||
# Don't start database as long as backup is running
|
||||
while [ -f "$DUMP_DIR/backup-is-running" ]; do
|
||||
echo "Waiting for backup container to finish..."
|
||||
sleep 10
|
||||
done
|
||||
|
||||
# Check if dump dir is writeable
|
||||
if ! [ -w "$DUMP_DIR" ]; then
|
||||
echo "DUMP dir is not writeable by postgres user."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Test if some things match
|
||||
if ( [ -f "$DATADIR/PG_VERSION" ] && [ "$PG_MAJOR" != "$(cat "$DATADIR/PG_VERSION")" ] ) \
|
||||
|| ( ! [ -f "$DATADIR/PG_VERSION" ] && [ -f "$DUMP_FILE" ] ); then
|
||||
# The DUMP_file must be provided
|
||||
if ! [ -f "$DUMP_FILE" ]; then
|
||||
echo "Unable to restore the database because the database dump is missing."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Inform
|
||||
echo "Restoring from database dump."
|
||||
|
||||
# Exit if any command fails
|
||||
set -e
|
||||
|
||||
# Remove old database files
|
||||
rm -rf "$DATADIR/"*
|
||||
|
||||
# Change database port to a random port temporarily
|
||||
export PGPORT=11000
|
||||
|
||||
# Create new database
|
||||
exec docker-entrypoint.sh postgres &
|
||||
|
||||
# Wait 2s for creation
|
||||
sleep 2s
|
||||
|
||||
# Restore database
|
||||
echo "Restoring the database from database dump"
|
||||
psql "$POSTGRES_DB" -U "$POSTGRES_USER" < "$DUMP_FILE"
|
||||
|
||||
# Shut down the database to be able to start it again
|
||||
pg_ctl stop -m fast
|
||||
|
||||
# Change database port back to default
|
||||
export PGPORT=5432
|
||||
|
||||
# Don't exit if command fails anymore
|
||||
set +e
|
||||
fi
|
||||
|
||||
# Cover the last case
|
||||
if ! [ -f "$DATADIR/PG_VERSION" ] && ! [ -f "$DUMP_FILE" ]; then
|
||||
# Remove old database files if somehow there should be some
|
||||
rm -rf "$DATADIR/"*
|
||||
fi
|
||||
|
||||
# Catch docker stop attempts
|
||||
trap 'true' SIGINT SIGTERM
|
||||
|
||||
# Start the database
|
||||
exec docker-entrypoint.sh postgres &
|
||||
wait $!
|
||||
|
||||
# Continue with shutdown procedure: do database dump, etc.
|
||||
rm -f "$DUMP_FILE.temp"
|
||||
if pg_dump --username "$POSTGRES_USER" "$POSTGRES_DB" > "$DUMP_FILE.temp"; then
|
||||
rm -f "$DUMP_FILE"
|
||||
mv "$DUMP_FILE.temp" "$DUMP_FILE"
|
||||
pg_ctl stop -m fast
|
||||
echo 'Database dump successful!'
|
||||
exit 0
|
||||
else
|
||||
pg_ctl stop -m fast
|
||||
echo "Database dump unsucessful!"
|
||||
exit 1
|
||||
fi
|
||||
Loading…
Add table
Add a link
Reference in a new issue