Automatic restore of backups when /data is empty

master
Tim Möhlmann 6 years ago
parent de3f125ed3
commit 3ce73f5bc0
No known key found for this signature in database
GPG Key ID: 8677988D8072E8DE

@ -7,7 +7,7 @@ RUN apk add --no-cache \
RUN pip3 install jinja2 RUN pip3 install jinja2
# Image specific layers under this line # Image specific layers under this line
RUN apk add --no-cache \ 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 \ && apk add --virtual .build-deps gcc musl-dev postgresql-dev python3-dev \
&& pip3 install psycopg2 anosql \ && pip3 install psycopg2 anosql \
&& apk --purge del .build-deps && apk --purge del .build-deps

@ -4,7 +4,7 @@ dest="/backup/base-$(date +%F-%H%M)"
last=$(ls -d /backup/base* | tail -n1) last=$(ls -d /backup/base* | tail -n1)
mkdir $dest || exit $? 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. # Clean old base backups, keep the last and the current.
for d in /backup/base*; do for d in /backup/base*; do

@ -28,10 +28,23 @@ def setup():
conn.commit() conn.commit()
conn.close() conn.close()
# Bootstrap the database if postgresql is running for the first time # Check if /data is empty
if not os.path.exists('/data/pg_wal'): if not os.listdir("/data"):
os.system("chown -R postgres:postgres /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 # Create backup directory structure, if it does not yet exist
os.system("mkdir -p /backup/wal_archive") 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"): for pg_file in glob.glob("/conf/*.conf"):
convert(pg_file, os.path.join("/data", os.path.basename(pg_file))) 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 \"''\" '") 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() setup()
os.system("sudo -u postgres pg_ctl stop -m smart -w -D /data") os.system("sudo -u postgres pg_ctl stop -m smart -w -D /data")

Loading…
Cancel
Save