2021-11-30 18:20:42 +08:00
#!/bin/bash
# Variables
DATADIR = "/var/lib/postgresql/data"
2022-12-29 07:41:29 +08:00
export DUMP_DIR = "/mnt/data"
2021-11-30 18:20:42 +08:00
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..."
2022-12-01 23:29:23 +08:00
echo "If this is incorrect because the backup container is not running anymore (because it was forcefully killed), you might delete the lock file which is by default stored here:"
echo "/var/lib/docker/volumes/nextcloud_aio_database_dump/_data/backup-is-running"
2021-11-30 18:20:42 +08:00
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
2022-12-20 17:59:01 +08:00
# Don't start if import failed
if [ -f " $DUMP_DIR /import.failed " ] ; then
echo "The database import failed. Please restore a backup and try again."
echo "For further clues on what went wrong, look at the logs above."
exit 1
fi
2022-12-29 07:41:29 +08:00
# Don't start if initialization failed
if [ -f " $DUMP_DIR /initialization.failed " ] ; then
echo "The database initialization failed. Most likely was a wrong timezone selected."
echo " The selected timezone is ' $TZ '. "
echo "Please check if it is in 'TZ database name' column of the timezone list: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List"
echo "For further clues on what went wrong, look at the logs above."
echo "You might start again from scratch by following https://github.com/nextcloud/all-in-one#how-to-properly-reset-the-instance and selecting a proper timezone."
exit 1
fi
2022-02-12 09:29:20 +08:00
# Delete the datadir once (needed for setting the correct credentials on old instances once)
if ! [ -f " $DUMP_DIR /export.failed " ] && ! [ -f " $DUMP_DIR /initial-cleanup-done " ] ; then
set -ex
rm -rf " ${ DATADIR : ? } / " *
touch " $DUMP_DIR /initial-cleanup-done "
set +ex
fi
2022-02-12 06:50:00 +08:00
2021-11-30 18:20:42 +08:00
# Test if some things match
2022-02-11 18:37:05 +08:00
# shellcheck disable=SC2235
2021-11-30 18:20:42 +08:00
if ( [ -f " $DATADIR /PG_VERSION " ] && [ " $PG_MAJOR " != " $( cat " $DATADIR /PG_VERSION " ) " ] ) \
2022-02-12 06:50:00 +08:00
|| ( ! [ -f " $DATADIR /PG_VERSION " ] && ( [ -f " $DUMP_FILE " ] || [ -f " $DUMP_DIR /export.failed " ] ) ) ; then
2021-11-30 18:20:42 +08:00
# 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
2021-12-08 01:45:52 +08:00
# If database export was unsuccessful, skip update
if [ -f " $DUMP_DIR /export.failed " ] ; then
echo "Database export failed the last time. Most likely was the export time not high enough."
2022-07-23 15:39:45 +08:00
echo "Please report this to https://github.com/nextcloud/all-in-one/issues. Thanks!"
2021-12-08 01:45:52 +08:00
exit 1
fi
2022-12-21 00:07:43 +08:00
# Write output to logfile.
exec > >( tee -i " $DUMP_DIR /database-import.log " )
exec 2>& 1
2021-11-30 18:20:42 +08:00
# Inform
echo "Restoring from database dump."
2022-12-20 17:59:01 +08:00
# Add import.failed file
touch " $DUMP_DIR /import.failed "
2021-11-30 18:20:42 +08:00
# Exit if any command fails
2022-02-12 07:26:12 +08:00
set -ex
2021-11-30 18:20:42 +08:00
# Remove old database files
2022-02-11 18:37:05 +08:00
rm -rf " ${ DATADIR : ? } / " *
2021-11-30 18:20:42 +08:00
# Change database port to a random port temporarily
export PGPORT = 11000
# Create new database
exec docker-entrypoint.sh postgres &
2022-02-18 18:41:52 +08:00
# Wait for creation
while ! nc -z localhost 11000; do
echo "Waiting for the database to start."
sleep 5
done
2021-11-30 18:20:42 +08:00
2022-07-23 15:39:45 +08:00
# Check if the line we grep for later on is there
GREP_STRING = 'Name: oc_appconfig; Type: TABLE; Schema: public; Owner:'
if ! grep -q " $GREP_STRING " " $DUMP_FILE " ; then
echo "The needed oc_appconfig line is not there which is unexpected."
echo "Please report this to https://github.com/nextcloud/all-in-one/issues. Thanks!"
exit 1
fi
# Get the Owner
DB_OWNER = " $( grep " $GREP_STRING " " $DUMP_FILE " | grep -oP 'Owner:.*$' | sed 's|Owner:||;s| ||g' ) "
2022-12-20 17:59:01 +08:00
if [ " $DB_OWNER " = " $POSTGRES_USER " ] ; then
2022-12-29 07:29:53 +08:00
echo " Unfortunately was the found database owner of the dump file the same as the POSTGRES_USER $POSTGRES_USER "
echo "It is not possible to import a database dump from this database owner."
echo "However you might rename the owner in the dumpfile to something else."
exit 1
2022-12-20 17:59:01 +08:00
elif [ " $DB_OWNER " != " oc_ $POSTGRES_USER " ] ; then
2022-07-23 15:39:45 +08:00
DIFFERENT_DB_OWNER = 1
2022-02-16 01:00:45 +08:00
psql -v ON_ERROR_STOP = 1 --username " $POSTGRES_USER " --dbname " $POSTGRES_DB " <<-EOSQL
2022-07-23 15:39:45 +08:00
CREATE USER " $DB_OWNER " WITH PASSWORD '$POSTGRES_PASSWORD' CREATEDB;
ALTER DATABASE " $POSTGRES_DB " OWNER TO " $DB_OWNER " ;
2022-02-16 01:00:45 +08:00
EOSQL
fi
2021-11-30 18:20:42 +08:00
# Restore database
echo "Restoring the database from database dump"
2022-02-16 01:00:45 +08:00
psql " $POSTGRES_DB " -U " $POSTGRES_USER " < " $DUMP_FILE "
# Correct permissions
2022-07-23 15:39:45 +08:00
if [ -n " $DIFFERENT_DB_OWNER " ] ; then
2022-02-16 01:00:45 +08:00
psql -v ON_ERROR_STOP = 1 --username " $POSTGRES_USER " --dbname " $POSTGRES_DB " <<-EOSQL
ALTER DATABASE " $POSTGRES_DB " OWNER TO " oc_ $POSTGRES_USER " ;
2022-07-23 15:39:45 +08:00
REASSIGN OWNED BY " $DB_OWNER " TO " oc_ $POSTGRES_USER " ;
2022-02-16 01:00:45 +08:00
EOSQL
fi
2021-11-30 18:20:42 +08:00
# 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
2022-02-12 07:26:12 +08:00
set +ex
2022-12-20 17:59:01 +08:00
# Remove import failed file if everything went correctly
rm " $DUMP_DIR /import.failed "
2021-11-30 18:20:42 +08:00
fi
# Cover the last case
if ! [ -f " $DATADIR /PG_VERSION " ] && ! [ -f " $DUMP_FILE " ] ; then
# Remove old database files if somehow there should be some
2022-02-11 18:37:05 +08:00
rm -rf " ${ DATADIR : ? } / " *
2021-11-30 18:20:42 +08:00
fi
2022-08-18 01:10:54 +08:00
echo "Setting max connections..."
MEMORY = $( mawk '/MemTotal/ {printf "%d", $2/1024}' /proc/meminfo)
MAX_CONNECTIONS = $(( MEMORY/50+3))
2022-08-23 04:45:03 +08:00
if [ -n " $MAX_CONNECTIONS " ] ; then
sed -i " s|^max_connections =.*|max_connections = $MAX_CONNECTIONS | " "/var/lib/postgresql/data/postgresql.conf"
fi
2022-08-18 01:10:54 +08:00
2021-11-30 18:20:42 +08:00
# 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 "
2021-12-08 01:45:52 +08:00
touch " $DUMP_DIR /export.failed "
2021-11-30 18:20:42 +08:00
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
2021-12-08 01:45:52 +08:00
rm " $DUMP_DIR /export.failed "
2021-11-30 18:20:42 +08:00
echo 'Database dump successful!'
exit 0
else
pg_ctl stop -m fast
2022-02-11 19:48:27 +08:00
echo "Database dump unsuccessful!"
2021-11-30 18:20:42 +08:00
exit 1
fi