mirror of
https://github.com/Mailu/Mailu.git
synced 2025-02-15 13:33:21 +02:00
Automatic restore of backups when /data is empty
This commit is contained in:
parent
de3f125ed3
commit
3ce73f5bc0
@ -7,7 +7,7 @@ RUN apk add --no-cache \
|
||||
RUN pip3 install jinja2
|
||||
# Image specific layers under this line
|
||||
RUN apk add --no-cache \
|
||||
postgresql postgresql-libs postgresql-contrib busybox-suid sudo\
|
||||
postgresql postgresql-libs postgresql-contrib busybox-suid sudo tar \
|
||||
&& apk add --virtual .build-deps gcc musl-dev postgresql-dev python3-dev \
|
||||
&& pip3 install psycopg2 anosql \
|
||||
&& apk --purge del .build-deps
|
||||
|
@ -4,7 +4,7 @@ dest="/backup/base-$(date +%F-%H%M)"
|
||||
last=$(ls -d /backup/base* | tail -n1)
|
||||
mkdir $dest || exit $?
|
||||
|
||||
pg_basebackup --pgdata=$dest --format=tar --gzip --username=postgres || exit $?
|
||||
pg_basebackup --wal-method=none --pgdata=$dest --format=tar --gzip --username=postgres || exit $?
|
||||
|
||||
# Clean old base backups, keep the last and the current.
|
||||
for d in /backup/base*; do
|
||||
|
@ -28,10 +28,23 @@ def setup():
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
# Bootstrap the database if postgresql is running for the first time
|
||||
if not os.path.exists('/data/pg_wal'):
|
||||
# Check if /data is empty
|
||||
if not os.listdir("/data"):
|
||||
os.system("chown -R postgres:postgres /data")
|
||||
os.system("sudo -u postgres initdb -D /data")
|
||||
os.system("chmod 0700 /data")
|
||||
base_backups=glob.glob("/backup/base-*")
|
||||
if base_backups:
|
||||
# Restore the latest backup
|
||||
subprocess.call(["tar", "--same-owner", "-zpxvf", base_backups[-1] + "/base.tar.gz" , "-C", "/data"])
|
||||
if os.listdir("/backup/wal_archive"):
|
||||
with open("/data/recovery.conf", "w") as rec:
|
||||
rec.write("restore_command = 'cp /backup/wal_archive/%f %p'\n")
|
||||
rec.write("standby_mode = off\n")
|
||||
os.system("chown postgres:postgres /data/recovery.conf")
|
||||
#os.system("sudo -u postgres pg_ctl start -D /data -o '-h \"''\" '")
|
||||
else:
|
||||
# Bootstrap the database
|
||||
os.system("sudo -u postgres initdb -D /data")
|
||||
|
||||
# Create backup directory structure, if it does not yet exist
|
||||
os.system("mkdir -p /backup/wal_archive")
|
||||
@ -42,8 +55,11 @@ convert = lambda src, dst: open(dst, "w").write(jinja2.Template(open(src).read()
|
||||
for pg_file in glob.glob("/conf/*.conf"):
|
||||
convert(pg_file, os.path.join("/data", os.path.basename(pg_file)))
|
||||
|
||||
# Run postgresql locally for DB and user creation
|
||||
# (Re)start postgresql locally for DB and user creation
|
||||
os.system("sudo -u postgres pg_ctl start -D /data -o '-h \"''\" '")
|
||||
while os.path.isfile("recovery.conf"):
|
||||
pass
|
||||
os.system("sudo -u postgres pg_ctl -D /data promote")
|
||||
setup()
|
||||
os.system("sudo -u postgres pg_ctl stop -m smart -w -D /data")
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user