diff --git a/postgresql/Dockerfile b/postgresql/Dockerfile index 136d621..8977979 100644 --- a/postgresql/Dockerfile +++ b/postgresql/Dockerfile @@ -1,14 +1,17 @@ -FROM alpine:3.16 -RUN apk update;apk add bash sudo postgresql postgresql-contrib dos2unix +FROM docker.io/debian:latest +RUN apt update;apt install -y bash sudo postgresql postgresql-contrib cron WORKDIR /tmp ENV SHELL="/bin/bash" +ENV PG_VERSION=13 +ENV PG_BIN=/usr/lib/postgresql/$PG_VERSION/bin +ENV PG_DIR=/var/lib/postgresql/$PG_VERSION/main -ADD start.sh / -ADD setup.sh / -ADD backup.sh / +COPY start.sh / +COPY setup.sh / +COPY backup.sh / -ADD crontab / +COPY crontab / RUN crontab /crontab #POSTGRES Tweaks diff --git a/postgresql/backup.sh b/postgresql/backup.sh index 24eeca8..3c4a6e6 100644 --- a/postgresql/backup.sh +++ b/postgresql/backup.sh @@ -1,2 +1,2 @@ #!/bin/bash -su postgres -s /bin/bash -lc "pg_dump -d pleroma --format=custom > /saves/pleroma-`date +%Y-%m-%d-%H-%M`.pgdump" \ No newline at end of file +su postgres -s /bin/bash -lc "$PG_BIN/pg_dump -d pleroma --format=custom > /saves/pleroma-`date +%Y-%m-%d-%H-%M`.pgdump" diff --git a/postgresql/crontab b/postgresql/crontab index 5fc05a2..d5772d5 100644 --- a/postgresql/crontab +++ b/postgresql/crontab @@ -1,3 +1,4 @@ 00 01 * * * bash /backup.sh 00 02 * * * find /saves/ -type f -mtime +3 -exec rm -f {} \; -00 04 * * 7 su postgres -s $SHELL -lc "psql -d pleroma -c 'vacuum(full,analyse,verbose);'" \ No newline at end of file +00 04 * * 7 su postgres -s $SHELL -lc "psql -d pleroma -c 'vacuum(full,analyse,verbose);'" + diff --git a/postgresql/postgres.sh b/postgresql/postgres.sh new file mode 100644 index 0000000..6b10eb7 --- /dev/null +++ b/postgresql/postgres.sh @@ -0,0 +1,126 @@ +#!/bin/bash +databases+=( raygungothic nextcloud gitea prosody poster synapse minetest pooper ) +VERSION="14" +NEW_VERSION="15" +export PATH=$PATH:/usr/lib/postgresql/$VERSION/bin +PG_BIN=/usr/lib/postgresql/$VERSION/bin +LOCATION='/raid/server/saves' +PG_DIR="/var/lib/postgresql/$VERSION/main" +NEW_PG_DIR="/var/lib/postgresql/$NEW_VERSION/main" +PRIMARY='192.168.0.153' +NEW_PG_BIN=/usr/lib/postgresql/$NEW_VERSION/bin + + +upgrade(){ + mkdir -p $NEW_PG_DIR + chown -R postgres:postgres $NEW_PG_DIR + su postgres -s /bin/bash -lc "$NEW_PG_BIN/initdb $NEW_PG_DIR" + systemctl disable --now postgresql@$VERSION-main + su postgres -s /bin/bash -lc "$NEW_PG_BIN/pg_upgrade -d $PG_DIR -b $PG_BIN -B $NEW_PG_BIN -D $NEW_PG_DIR" + su postgres -s /bin/bash -lc "cp -f $PG_DIR/postgresql.conf $NEW_PG_DIR/" + su postgres -s /bin/bash -lc "cp -f $PG_DIR/pg_hba.conf $NEW_PG_DIR/" + systemctl enable --now postgresql@$NEW_VERSION-main +} + +replication-status(){ + echo "select * from pg_stat_replication;" | su postgres -s /bin/bash -lc "psql " + echo "select * from pg_stat_wal_receiver;" | su postgres -s /bin/bash -lc "psql " + echo "select pg_is_wal_replay_paused();" | su postgres -s /bin/bash -lc "psql " +} + +configure-secondary-replication () { + #echo "pg_ctl stop -D $PG_DIR" | su postgres -s /bin/bash + systemctl disable --now postgresql@$VERSION-main + cd $PG_DIR + rm -rf $PG_DIR/* + echo "$PG_BIN/pg_basebackup -R -P -h $PRIMARY -X stream -c fast -U root -W -D $PG_DIR/" | su postgres -s /bin/bash + chmod 0700 $PG_DIR + #echo "pg_ctl start -D $PG_DIR" | su postgres -s /bin/bash + systemctl enable --now postgresql@$VERSION-main + echo "select pg_reload_conf();" | su postgres -s /bin/bash -lc "psql " + grep primary_conninfo postgresql.auto.conf +} + +configure-primary-replication (){ + #echo "SELECT * FROM pg_drop_replication_slot('db02_repl_slot');" | su postgres -s /bin/bash -lc "psql " + #echo "select * from pg_create_physical_replication_slot('db02_repl_slot');" | su postgres -s /bin/bash -lc "psql " + echo "select pg_reload_conf();" | su postgres -s /bin/bash -lc "psql " + echo "select slot_name, slot_type, active, wal_status from pg_replication_slots;" | su postgres -s /bin/bash -lc "psql " + echo "$PG_BIN/pg_ctl restart -D $PG_DIR" | su postgres -s /bin/bash +} + +replication-failover () { + echo "select pg_promote();" | su postgres -s /bin/bash -lc "psql " + echo "SELECT pg_reload_conf();" | su postgres -s /bin/bash -lc "psql " +} + +backup (){ + vacuum_off + for i in "${databases[@]}" + do + echo;echo "Backing Up: "$i;echo + su postgres -s /bin/bash -lc "$PG_BIN/pg_dump -d $i --format=custom -f $LOCATION/$i-`cat /etc/hostname`-`date +%Y-%m-%d-%H-%M`.pgdump" + done + vacuum_on +} + +vacuum_off (){ + sed -i 's/autovacuum = on/autovacuum = off/i' $PG_DIR/postgresql.conf + echo "SELECT pg_reload_conf();" | su postgres -s /bin/bash -lc "psql " +} + +vacuum_on (){ + sed -i 's/autovacuum = off/autovacuum = on/i' $PG_DIR/postgresql.conf + echo "SELECT pg_reload_conf();" | su postgres -s /bin/bash -lc "psql " +} + +vacuum (){ + for i in "${databases[@]}" + do + echo "vacuum(full,analyse,verbose);" | su postgres $i -s /bin/bash -lc "psql " + done +} + + +help_menu (){ + clear + echo "./postgres.sh backup" + echo "./postgres.sh replication-status" + echo "./postgres.sh configure-primary-replication" + echo "./postgres.sh configure-secondary-replication" + echo "./postgres.sh base" + echo "./postgres.sh replication-failover" + echo "./postgres.sh vacuum" + echo "./postgres.sh setup" + echo "./postgres.sh vacuum-on" + echo "./postgres.sh vacuum-off" + echo "./postgres.sh upgrade" +} + + +if [ "$1" = "vacuum-on" ]; + then vacuum_on; +elif [ "$1" = "vacuum-off" ]; + then vacuum_off; +elif [ "$1" = "configure-primary-replication" ]; + then configure-primary-replication; +elif [ "$1" = "replication-failover" ]; + then replication-failover; +elif [ "$1" = "backup" ]; + then backup "$2"; +elif [ "$1" = "configure-secondary-replication" ]; + then configure-secondary-replication; +elif [ "$1" = "replication-status" ]; + then replication-status; +elif [ "$1" = "upgrade" ]; + then upgrade; +elif [ "$1" = "restore" ]; + then restore; +elif [ "$1" = "vacuum" ]; + then vacuum; +elif [ "$1" = "vacfull" ]; + then vacuum_full "$2"; +else + help_menu +fi + diff --git a/postgresql/setup.sh b/postgresql/setup.sh index 5be50cf..ab02fb2 100644 --- a/postgresql/setup.sh +++ b/postgresql/setup.sh @@ -1,6 +1,5 @@ #!/bin/bash export SHELL="/bin/bash" - #Database Setup mkdir /run/postgresql #mkdir -p /var/lib/postgresql/13/main @@ -8,14 +7,15 @@ chown -R postgres:postgres /run/postgresql/ chown -R postgres:postgres /var/lib/postgresql chown -R postgres:postgres /var/log/postgresql #su postgres -s $SHELL -lc "mkdir /var/lib/postgresql/data;chmod 0700 /var/lib/postgresql/13/main" -su postgres -s $SHELL -lc "initdb --auth-host=trust -D /var/lib/postgresql/data" -cp -f /etc/postgresql.conf /var/lib/postgresql/data/ -echo "host all all 0.0.0.0/0 md5" >> /var/lib//postgresql/data/pg_hba.conf -chown -R postgres:postgres /var/lib/postgresql -su postgres -s $SHELL -lc "pg_ctl start -D /var/lib/postgresql/data" +su postgres -s $SHELL -lc "initdb --auth-host=trust -D $PG_DIR" +cp -f /etc/postgresql.conf $PG_DIR/ +echo "host all all 0.0.0.0/0 md5" >> /etc/postgresql/$PG_VERSION/main/pg_hba.conf +cp -f /etc/postgresql/$PG_VERSION/main/pg_hba.conf $PG_DIR/ +chown -R postgres:postgres $PG_DIR +su postgres -s $SHELL -lc "$PG_BIN/pg_ctl start -D $PG_DIR" sleep 5 echo "create user root with encrypted password 'sql';"| su postgres -s $SHELL -lc psql echo "ALTER ROLE root SUPERUSER;" | su postgres -s $SHELL -lc psql echo "CREATE DATABASE pleroma with template = template0 OWNER = root ENCODING = 'UTF8';" | su postgres -s $SHELL -lc psql -touch /configured.txt \ No newline at end of file +touch /configured.txt diff --git a/postgresql/start.sh b/postgresql/start.sh index a781c71..5485dc2 100644 --- a/postgresql/start.sh +++ b/postgresql/start.sh @@ -8,12 +8,8 @@ chmod -R 777 /saves #Configure Local Time cp -f /usr/share/zoneinfo/US/Mountain /etc/localtime -#Windows Hack -dos2unix /postgres.sh - #Start Cron and Postgresql -crond -su postgres -s $SHELL -lc "pg_ctl start -D /var/lib/postgresql/data" +cron #Restore latest Database if container is new if [ ! -f "$FILE" ] @@ -23,4 +19,6 @@ bash /setup.sh touch /configured.txt fi +su postgres -s $SHELL -lc "$PG_BIN/pg_ctl start -D $PG_DIR" + tail -f /dev/null