From 1f21d8d28bdedaf96346ea4bc4529f483d70d79f Mon Sep 17 00:00:00 2001 From: bordin Date: Tue, 20 Dec 2016 14:34:59 +0700 Subject: [PATCH 1/5] add postgresql backup agent --- 9.6/Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/9.6/Dockerfile b/9.6/Dockerfile index 7a9169a0f8..b71f682633 100644 --- a/9.6/Dockerfile +++ b/9.6/Dockerfile @@ -33,7 +33,7 @@ ENV PG_VERSION 9.6.1-2.pgdg80+1 RUN echo 'deb http://apt.postgresql.org/pub/repos/apt/ jessie-pgdg main' $PG_MAJOR > /etc/apt/sources.list.d/pgdg.list RUN apt-get update \ - && apt-get install -y postgresql-common \ + && apt-get install -y postgresql-common autopostgresqlbackup \ && sed -ri 's/#(create_main_cluster) .*$/\1 = false/' /etc/postgresql-common/createcluster.conf \ && apt-get install -y \ postgresql-$PG_MAJOR=$PG_VERSION \ @@ -50,7 +50,7 @@ RUN mkdir -p /var/run/postgresql && chown -R postgres /var/run/postgresql ENV PATH /usr/lib/postgresql/$PG_MAJOR/bin:$PATH ENV PGDATA /var/lib/postgresql/data VOLUME /var/lib/postgresql/data - +VOLUME /var/lib/autopostgresqlbackup COPY docker-entrypoint.sh / ENTRYPOINT ["/docker-entrypoint.sh"] From 022514adbcfdd66d0194cef5167ee0884bc6ec9a Mon Sep 17 00:00:00 2001 From: bordin Date: Tue, 20 Dec 2016 14:57:32 +0700 Subject: [PATCH 2/5] add 9.5 autopostgresqlbackup --- 9.5/Dockerfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/9.5/Dockerfile b/9.5/Dockerfile index c9c0a283ca..6ebdb0156a 100644 --- a/9.5/Dockerfile +++ b/9.5/Dockerfile @@ -33,7 +33,7 @@ ENV PG_VERSION 9.5.5-1.pgdg80+1 RUN echo 'deb http://apt.postgresql.org/pub/repos/apt/ jessie-pgdg main' $PG_MAJOR > /etc/apt/sources.list.d/pgdg.list RUN apt-get update \ - && apt-get install -y postgresql-common \ + && apt-get install -y postgresql-common autopostgresqlbackup \ && sed -ri 's/#(create_main_cluster) .*$/\1 = false/' /etc/postgresql-common/createcluster.conf \ && apt-get install -y \ postgresql-$PG_MAJOR=$PG_VERSION \ @@ -50,6 +50,7 @@ RUN mkdir -p /var/run/postgresql && chown -R postgres /var/run/postgresql ENV PATH /usr/lib/postgresql/$PG_MAJOR/bin:$PATH ENV PGDATA /var/lib/postgresql/data VOLUME /var/lib/postgresql/data +VOLUME /var/lib/autopostgresqlbackup COPY docker-entrypoint.sh / From c03a12238f9c5794f218066c754a88124107a45f Mon Sep 17 00:00:00 2001 From: bordin Date: Mon, 21 Aug 2017 14:51:40 +0700 Subject: [PATCH 3/5] Update Dockerfile --- 9.5/Dockerfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/9.5/Dockerfile b/9.5/Dockerfile index c9c0a283ca..6ebdb0156a 100644 --- a/9.5/Dockerfile +++ b/9.5/Dockerfile @@ -33,7 +33,7 @@ ENV PG_VERSION 9.5.5-1.pgdg80+1 RUN echo 'deb http://apt.postgresql.org/pub/repos/apt/ jessie-pgdg main' $PG_MAJOR > /etc/apt/sources.list.d/pgdg.list RUN apt-get update \ - && apt-get install -y postgresql-common \ + && apt-get install -y postgresql-common autopostgresqlbackup \ && sed -ri 's/#(create_main_cluster) .*$/\1 = false/' /etc/postgresql-common/createcluster.conf \ && apt-get install -y \ postgresql-$PG_MAJOR=$PG_VERSION \ @@ -50,6 +50,7 @@ RUN mkdir -p /var/run/postgresql && chown -R postgres /var/run/postgresql ENV PATH /usr/lib/postgresql/$PG_MAJOR/bin:$PATH ENV PGDATA /var/lib/postgresql/data VOLUME /var/lib/postgresql/data +VOLUME /var/lib/autopostgresqlbackup COPY docker-entrypoint.sh / From fd44794f61f2bcca93693cdcc230b2a6814b197b Mon Sep 17 00:00:00 2001 From: bordin Date: Tue, 29 Aug 2017 11:10:28 +0700 Subject: [PATCH 4/5] update to 9.5.5-1 --- 9.2/Dockerfile | 0 9.2/alpine/Dockerfile | 0 9.3/Dockerfile | 0 9.3/alpine/Dockerfile | 0 9.4/Dockerfile | 0 9.4/alpine/Dockerfile | 0 9.5/Dockerfile | 6 ++++++ 9.5/alpine/Dockerfile | 0 9.6/Dockerfile | 0 9.6/alpine/Dockerfile | 0 AUTHORS | 0 Dockerfile-alpine.template | 0 Dockerfile-debian.template | 0 LICENSE | 0 README.md | 0 15 files changed, 6 insertions(+) mode change 100644 => 100755 9.2/Dockerfile mode change 100644 => 100755 9.2/alpine/Dockerfile mode change 100644 => 100755 9.3/Dockerfile mode change 100644 => 100755 9.3/alpine/Dockerfile mode change 100644 => 100755 9.4/Dockerfile mode change 100644 => 100755 9.4/alpine/Dockerfile mode change 100644 => 100755 9.5/Dockerfile mode change 100644 => 100755 9.5/alpine/Dockerfile mode change 100644 => 100755 9.6/Dockerfile mode change 100644 => 100755 9.6/alpine/Dockerfile mode change 100644 => 100755 AUTHORS mode change 100644 => 100755 Dockerfile-alpine.template mode change 100644 => 100755 Dockerfile-debian.template mode change 100644 => 100755 LICENSE mode change 100644 => 100755 README.md diff --git a/9.2/Dockerfile b/9.2/Dockerfile old mode 100644 new mode 100755 diff --git a/9.2/alpine/Dockerfile b/9.2/alpine/Dockerfile old mode 100644 new mode 100755 diff --git a/9.3/Dockerfile b/9.3/Dockerfile old mode 100644 new mode 100755 diff --git a/9.3/alpine/Dockerfile b/9.3/alpine/Dockerfile old mode 100644 new mode 100755 diff --git a/9.4/Dockerfile b/9.4/Dockerfile old mode 100644 new mode 100755 diff --git a/9.4/alpine/Dockerfile b/9.4/alpine/Dockerfile old mode 100644 new mode 100755 diff --git a/9.5/Dockerfile b/9.5/Dockerfile old mode 100644 new mode 100755 index 6ebdb0156a..07e0d655db --- a/9.5/Dockerfile +++ b/9.5/Dockerfile @@ -30,6 +30,12 @@ RUN apt-key adv --keyserver ha.pool.sks-keyservers.net --recv-keys B97B0AFCAA1A4 ENV PG_MAJOR 9.5 ENV PG_VERSION 9.5.5-1.pgdg80+1 +##set timezone +ENV TZ=Asia/Bangkok +#RUN echo -e "#!/bin/sh\nntpd -d -q -n -p pool.ntp.org" > /etc/cron.hourly/clocksync +#RUN chmod +x /etc/periodic/hourly/clocksync + + RUN echo 'deb http://apt.postgresql.org/pub/repos/apt/ jessie-pgdg main' $PG_MAJOR > /etc/apt/sources.list.d/pgdg.list RUN apt-get update \ diff --git a/9.5/alpine/Dockerfile b/9.5/alpine/Dockerfile old mode 100644 new mode 100755 diff --git a/9.6/Dockerfile b/9.6/Dockerfile old mode 100644 new mode 100755 diff --git a/9.6/alpine/Dockerfile b/9.6/alpine/Dockerfile old mode 100644 new mode 100755 diff --git a/AUTHORS b/AUTHORS old mode 100644 new mode 100755 diff --git a/Dockerfile-alpine.template b/Dockerfile-alpine.template old mode 100644 new mode 100755 diff --git a/Dockerfile-debian.template b/Dockerfile-debian.template old mode 100644 new mode 100755 diff --git a/LICENSE b/LICENSE old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 From e1924bb49adbfe970bab4ca794fc1d27af741f37 Mon Sep 17 00:00:00 2001 From: bordin Date: Wed, 30 Aug 2017 11:05:26 +0700 Subject: [PATCH 5/5] update to 9.6 alpine autobackup --- 9.5/Dockerfile | 41 ++- 9.5/alpine/Dockerfile | 53 +++- 9.5/alpine/autopgsqlbackup.sh | 497 ++++++++++++++++++++++++++++++++ 9.5/alpine/docker-entrypoint.sh | 50 +++- 9.6/Dockerfile | 4 +- 9.6/alpine/Dockerfile | 58 +++- 9.6/alpine/autopgsqlbackup.sh | 497 ++++++++++++++++++++++++++++++++ 9.6/alpine/docker-entrypoint.sh | 48 ++- 8 files changed, 1186 insertions(+), 62 deletions(-) create mode 100644 9.5/alpine/autopgsqlbackup.sh create mode 100755 9.6/alpine/autopgsqlbackup.sh diff --git a/9.5/Dockerfile b/9.5/Dockerfile index 07e0d655db..730695b641 100755 --- a/9.5/Dockerfile +++ b/9.5/Dockerfile @@ -1,6 +1,16 @@ # vim:set ft=dockerfile: FROM debian:jessie +RUN set -ex; \ + if ! command -v gpg > /dev/null; then \ + apt-get update; \ + apt-get install -y --no-install-recommends \ + gnupg2 \ + dirmngr \ + ; \ + rm -rf /var/lib/apt/lists/*; \ + fi + # explicitly set user/group IDs RUN groupadd -r postgres --gid=999 && useradd -r -g postgres --uid=999 postgres @@ -13,7 +23,7 @@ RUN set -x \ && export GNUPGHOME="$(mktemp -d)" \ && gpg --keyserver ha.pool.sks-keyservers.net --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4 \ && gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu \ - && rm -r "$GNUPGHOME" /usr/local/bin/gosu.asc \ + && rm -rf "$GNUPGHOME" /usr/local/bin/gosu.asc \ && chmod +x /usr/local/bin/gosu \ && gosu nobody true \ && apt-get purge -y --auto-remove ca-certificates wget @@ -25,16 +35,19 @@ ENV LANG en_US.utf8 RUN mkdir /docker-entrypoint-initdb.d -RUN apt-key adv --keyserver ha.pool.sks-keyservers.net --recv-keys B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8 +RUN set -ex; \ +# pub 4096R/ACCC4CF8 2011-10-13 [expires: 2019-07-02] +# Key fingerprint = B97B 0AFC AA1A 47F0 44F2 44A0 7FCC 7D46 ACCC 4CF8 +# uid PostgreSQL Debian Repository + key='B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8'; \ + export GNUPGHOME="$(mktemp -d)"; \ + gpg --keyserver ha.pool.sks-keyservers.net --recv-keys "$key"; \ + gpg --export "$key" > /etc/apt/trusted.gpg.d/postgres.gpg; \ + rm -rf "$GNUPGHOME"; \ + apt-key list ENV PG_MAJOR 9.5 -ENV PG_VERSION 9.5.5-1.pgdg80+1 - -##set timezone -ENV TZ=Asia/Bangkok -#RUN echo -e "#!/bin/sh\nntpd -d -q -n -p pool.ntp.org" > /etc/cron.hourly/clocksync -#RUN chmod +x /etc/periodic/hourly/clocksync - +ENV PG_VERSION 9.5.8-1.pgdg80+1 RUN echo 'deb http://apt.postgresql.org/pub/repos/apt/ jessie-pgdg main' $PG_MAJOR > /etc/apt/sources.list.d/pgdg.list @@ -51,16 +64,16 @@ RUN mv -v /usr/share/postgresql/$PG_MAJOR/postgresql.conf.sample /usr/share/post && ln -sv ../postgresql.conf.sample /usr/share/postgresql/$PG_MAJOR/ \ && sed -ri "s!^#?(listen_addresses)\s*=\s*\S+.*!\1 = '*'!" /usr/share/postgresql/postgresql.conf.sample -RUN mkdir -p /var/run/postgresql && chown -R postgres /var/run/postgresql +RUN mkdir -p /var/run/postgresql && chown -R postgres:postgres /var/run/postgresql && chmod 2777 /var/run/postgresql ENV PATH /usr/lib/postgresql/$PG_MAJOR/bin:$PATH ENV PGDATA /var/lib/postgresql/data +RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 777 "$PGDATA" # this 777 will be replaced by 700 at runtime (allows semi-arbitrary "--user" values) VOLUME /var/lib/postgresql/data VOLUME /var/lib/autopostgresqlbackup - -COPY docker-entrypoint.sh / - -ENTRYPOINT ["/docker-entrypoint.sh"] +COPY docker-entrypoint.sh /usr/local/bin/ +RUN ln -s usr/local/bin/docker-entrypoint.sh / # backwards compat +ENTRYPOINT ["docker-entrypoint.sh"] EXPOSE 5432 CMD ["postgres"] diff --git a/9.5/alpine/Dockerfile b/9.5/alpine/Dockerfile index 7e25990c96..8d40b4f413 100755 --- a/9.5/alpine/Dockerfile +++ b/9.5/alpine/Dockerfile @@ -1,9 +1,17 @@ # vim:set ft=dockerfile: -FROM alpine:3.4 +FROM alpine:3.5 # alpine includes "postgres" user/group in base install -# /etc/passwd:22:postgres:x:70:70::/var/lib/postgresql:/bin/sh -# /etc/group:34:postgres:x:70: +# /etc/passwd:22:postgres:x:70:70::/var/lib/postgresql:/bin/sh +# /etc/group:34:postgres:x:70: +# the home directory for the postgres user, however, is not created by default +# see https://github.com/docker-library/postgres/issues/274 +RUN set -ex; \ + postgresHome="$(getent passwd postgres)"; \ + postgresHome="$(echo "$postgresHome" | cut -d: -f6)"; \ + [ "$postgresHome" = '/var/lib/postgresql' ]; \ + mkdir -p "$postgresHome"; \ + chown -R postgres:postgres "$postgresHome" # su-exec (gosu-compatible) is installed further down @@ -14,8 +22,8 @@ ENV LANG en_US.utf8 RUN mkdir /docker-entrypoint-initdb.d ENV PG_MAJOR 9.5 -ENV PG_VERSION 9.5.5 -ENV PG_SHA256 02c65290be74de6604c3fed87c9fd3e6b32e949f0ab8105a75bd7ed5aa71f394 +ENV PG_VERSION 9.5.8 +ENV PG_SHA256 ade57068f134f36710fa953e1ef79185abd96572f8098741413132f79ed37202 RUN set -ex \ \ @@ -36,6 +44,8 @@ RUN set -ex \ \ && apk add --no-cache --virtual .build-deps \ bison \ + coreutils \ + dpkg-dev dpkg \ flex \ gcc \ # krb5-dev \ @@ -46,7 +56,10 @@ RUN set -ex \ make \ # openldap-dev \ openssl-dev \ +# configure: error: prove not found perl \ +# configure: error: Perl module IPC::Run is required to run TAP tests + perl-ipc-run \ # perl-dev \ # python-dev \ # python3-dev \ @@ -55,9 +68,19 @@ RUN set -ex \ zlib-dev \ \ && cd /usr/src/postgresql \ +# update "DEFAULT_PGSOCKET_DIR" to "/var/run/postgresql" (matching Debian) +# see https://anonscm.debian.org/git/pkg-postgresql/postgresql.git/tree/debian/patches/51-default-sockets-in-var.patch?id=8b539fcb3e093a521c095e70bdfa76887217b89f + && awk '$1 == "#define" && $2 == "DEFAULT_PGSOCKET_DIR" && $3 == "\"/tmp\"" { $3 = "\"/var/run/postgresql\""; print; next } { print }' src/include/pg_config_manual.h > src/include/pg_config_manual.h.new \ + && grep '/var/run/postgresql' src/include/pg_config_manual.h.new \ + && mv src/include/pg_config_manual.h.new src/include/pg_config_manual.h \ + && gnuArch="$(dpkg-architecture --query DEB_BUILD_GNU_TYPE)" \ +# explicitly update autoconf config.guess and config.sub so they support more arches/libcs + && wget -O config/config.guess 'https://git.savannah.gnu.org/cgit/config.git/plain/config.guess?id=7d3d27baf8107b630586c962c057e22149653deb' \ + && wget -O config/config.sub 'https://git.savannah.gnu.org/cgit/config.git/plain/config.sub?id=7d3d27baf8107b630586c962c057e22149653deb' \ # configure options taken from: # https://anonscm.debian.org/cgit/pkg-postgresql/postgresql.git/tree/debian/rules?h=9.5 && ./configure \ + --build="$gnuArch" \ # "/usr/src/postgresql/src/backend/access/common/tupconvert.c:105: undefined reference to `libintl_gettext'" # --enable-nls \ --enable-integer-datetimes \ @@ -71,6 +94,8 @@ RUN set -ex \ --with-pgport=5432 \ --with-system-tzdata=/usr/share/zoneinfo \ --prefix=/usr/local \ + --with-includes=/usr/local/include \ + --with-libraries=/usr/local/lib \ \ # these make our image abnormally large (at least 100MB larger), which seems uncouth for an "Alpine" (ie, "small") variant :) # --with-krb5 \ @@ -83,7 +108,7 @@ RUN set -ex \ --with-openssl \ --with-libxml \ --with-libxslt \ - && make -j "$(getconf _NPROCESSORS_ONLN)" world \ + && make -j "$(nproc)" world \ && make install-world \ && make -C contrib install \ \ @@ -105,7 +130,6 @@ RUN set -ex \ && cd / \ && rm -rf \ /usr/src/postgresql \ - /usr/local/include/* \ /usr/local/share/doc \ /usr/local/share/man \ && find /usr/local -name '*.a' -delete @@ -113,15 +137,24 @@ RUN set -ex \ # make the sample config easier to munge (and "correct by default") RUN sed -ri "s!^#?(listen_addresses)\s*=\s*\S+.*!\1 = '*'!" /usr/local/share/postgresql/postgresql.conf.sample -RUN mkdir -p /var/run/postgresql && chown -R postgres /var/run/postgresql +RUN mkdir -p /var/run/postgresql && chown -R postgres:postgres /var/run/postgresql && chmod 2777 /var/run/postgresql ENV PATH /usr/lib/postgresql/$PG_MAJOR/bin:$PATH ENV PGDATA /var/lib/postgresql/data +RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 777 "$PGDATA" # this 777 will be replaced by 700 at runtime (allows semi-arbitrary "--user" values) + VOLUME /var/lib/postgresql/data +VOLUME /var/lib/autopostgresqlbackup + +COPY docker-entrypoint.sh /usr/local/bin/ + +COPY autopgsqlbackup.sh /usr/sbin/autopgsqlbackup -COPY docker-entrypoint.sh / -ENTRYPOINT ["/docker-entrypoint.sh"] +RUN ln -s usr/local/bin/docker-entrypoint.sh / # backwards compat +ENTRYPOINT ["docker-entrypoint.sh"] EXPOSE 5432 CMD ["postgres"] + + diff --git a/9.5/alpine/autopgsqlbackup.sh b/9.5/alpine/autopgsqlbackup.sh new file mode 100644 index 0000000000..5acb6535b3 --- /dev/null +++ b/9.5/alpine/autopgsqlbackup.sh @@ -0,0 +1,497 @@ +#!/bin/bash +# +# PostgreSQL Backup Script Ver 1.0 +# http://autopgsqlbackup.frozenpc.net +# Copyright (c) 2005 Aaron Axelsen +# +# This script is based of the AutoMySQLBackup Script Ver 2.2 +# It can be found at http://sourceforge.net/projects/automysqlbackup/ +# +# The PostgreSQL changes are based on a patch agaisnt AutoMySQLBackup 1.9 +# created by Friedrich Lobenstock +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +#===================================================================== +# Set the following variables to your system needs +# (Detailed instructions below variables) +#===================================================================== + +# Username to access the PostgreSQL server e.g. dbuser +USERNAME=postgres + +# Password +# create a file $HOME/.pgpass containing a line like this +# hostname:*:*:dbuser:dbpass +# replace hostname with the value of DBHOST and postgres with +# the value of USERNAME + +# Host name (or IP address) of PostgreSQL server e.g localhost +DBHOST=localhost + +# List of DBNAMES for Daily/Weekly Backup e.g. "DB1 DB2 DB3" +DBNAMES="all" + +# Backup directory location e.g /backups +BACKUPDIR="/var/backups/postgres" + +# Mail setup +# What would you like to be mailed to you? +# - log : send only log file +# - files : send log file and sql files as attachments (see docs) +# - stdout : will simply output the log to the screen if run manually. +MAILCONTENT="stdout" + +# Set the maximum allowed email size in k. (4000 = approx 5MB email [see docs]) +MAXATTSIZE="4000" + +# Email Address to send mail to? (user@domain.com) +MAILADDR="root@localhost" + + +# ============================================================ +# === ADVANCED OPTIONS ( Read the doc's below for details )=== +#============================================================= + +# List of DBBNAMES for Monthly Backups. +MDBNAMES="template1 $DBNAMES" + +# List of DBNAMES to EXLUCDE if DBNAMES are set to all (must be in " quotes) +DBEXCLUDE="" + +# Include CREATE DATABASE in backup? +CREATE_DATABASE=yes + +# Separate backup directory and file for each DB? (yes or no) +SEPDIR=yes + +# Which day do you want weekly backups? (1 to 7 where 1 is Monday) +DOWEEKLY=6 + +# Choose Compression type. (gzip or bzip2) +COMP=bzip2 + +# Command to run before backups (uncomment to use) +#PREBACKUP="/etc/pgsql-backup-pre" + +# Command run after backups (uncomment to use) +#POSTBACKUP="bash /home/backups/scripts/ftp_pgsql" + +#===================================================================== +# Options documentation +#===================================================================== +# Set USERNAME and PASSWORD of a user that has at least SELECT permission +# to ALL databases. +# +# Set the DBHOST option to the server you wish to backup, leave the +# default to backup "this server".(to backup multiple servers make +# copies of this file and set the options for that server) +# +# Put in the list of DBNAMES(Databases)to be backed up. If you would like +# to backup ALL DBs on the server set DBNAMES="all".(if set to "all" then +# any new DBs will automatically be backed up without needing to modify +# this backup script when a new DB is created). +# +# If the DB you want to backup has a space in the name replace the space +# with a % e.g. "data base" will become "data%base" +# NOTE: Spaces in DB names may not work correctly when SEPDIR=no. +# +# You can change the backup storage location from /backups to anything +# you like by using the BACKUPDIR setting.. +# +# The MAILCONTENT and MAILADDR options and pretty self explanitory, use +# these to have the backup log mailed to you at any email address or multiple +# email addresses in a space seperated list. +# (If you set mail content to "log" you will require access to the "mail" program +# on your server. If you set this to "files" you will have to have mutt installed +# on your server. If you set it sto stdout it will log to the screen if run from +# the console or to the cron job owner if run through cron) +# +# MAXATTSIZE sets the largest allowed email attachments total (all backup files) you +# want the script to send. This is the size before it is encoded to be sent as an email +# so if your mail server will allow a maximum mail size of 5MB I would suggest setting +# MAXATTSIZE to be 25% smaller than that so a setting of 4000 would probably be fine. +# +# Finally copy automysqlbackup.sh to anywhere on your server and make sure +# to set executable permission. You can also copy the script to +# /etc/cron.daily to have it execute automatically every night or simply +# place a symlink in /etc/cron.daily to the file if you wish to keep it +# somwhere else. +# NOTE:On Debian copy the file with no extention for it to be run +# by cron e.g just name the file "automysqlbackup" +# +# Thats it.. +# +# +# === Advanced options doc's === +# +# The list of MDBNAMES is the DB's to be backed up only monthly. You should +# always include "mysql" in this list to backup your user/password +# information along with any other DBs that you only feel need to +# be backed up monthly. (if using a hosted server then you should +# probably remove "mysql" as your provider will be backing this up) +# NOTE: If DBNAMES="all" then MDBNAMES has no effect as all DBs will be backed +# up anyway. +# +# If you set DBNAMES="all" you can configure the option DBEXCLUDE. Other +# wise this option will not be used. +# This option can be used if you want to backup all dbs, but you want +# exclude some of them. (eg. a db is to big). +# +# Set CREATE_DATABASE to "yes" (the default) if you want your SQL-Dump to create +# a database with the same name as the original database when restoring. +# Saying "no" here will allow your to specify the database name you want to +# restore your dump into, making a copy of the database by using the dump +# created with automysqlbackup. +# NOTE: Not used if SEPDIR=no +# +# The SEPDIR option allows you to choose to have all DBs backed up to +# a single file (fast restore of entire server in case of crash) or to +# seperate directories for each DB (each DB can be restored seperately +# in case of single DB corruption or loss). +# +# To set the day of the week that you would like the weekly backup to happen +# set the DOWEEKLY setting, this can be a value from 1 to 7 where 1 is Monday, +# The default is 6 which means that weekly backups are done on a Saturday. +# +# COMP is used to choose the copmression used, options are gzip or bzip2. +# bzip2 will produce slightly smaller files but is more processor intensive so +# may take longer to complete. +# +# Use PREBACKUP and POSTBACKUP to specify Per and Post backup commands +# or scripts to perform tasks either before or after the backup process. +# +# +#===================================================================== +# Backup Rotation.. +#===================================================================== +# +# Daily Backups are rotated weekly.. +# Weekly Backups are run by default on Saturday Morning when +# cron.daily scripts are run...Can be changed with DOWEEKLY setting.. +# Weekly Backups are rotated on a 5 week cycle.. +# Monthly Backups are run on the 1st of the month.. +# Monthly Backups are NOT rotated automatically... +# It may be a good idea to copy Monthly backups offline or to another +# server.. +# +#===================================================================== +# Please Note!! +#===================================================================== +# +# I take no resposibility for any data loss or corruption when using +# this script.. +# This script will not help in the event of a hard drive crash. If a +# copy of the backup has not be stored offline or on another PC.. +# You should copy your backups offline regularly for best protection. +# +# Happy backing up... +# +#===================================================================== +# Change Log +#===================================================================== +# +# VER 1.0 - (2005-03-25) +# Initial Release - based on AutoMySQLBackup 2.2 +# +#===================================================================== +#===================================================================== +# +# Should not need to be modified from here down!! +# +#===================================================================== +#===================================================================== +PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/postgres/bin:/usr/local/pgsql/bin +DATE=`date +%Y-%m-%d` # Datestamp e.g 2002-09-21 +DOW=`date +%A` # Day of the week e.g. Monday +DNOW=`date +%u` # Day number of the week 1 to 7 where 1 represents Monday +DOM=`date +%d` # Date of the Month e.g. 27 +M=`date +%B` # Month e.g January +W=`date +%V` # Week Number e.g 37 +VER=1.0 # Version Number +LOGFILE=$BACKUPDIR/$DBHOST-`date +%N`.log # Logfile Name +OPT="" # OPT string for use with mysqldump ( see man mysqldump ) +BACKUPFILES="" # thh: added for later mailing + +# Create required directories +if [ ! -e "$BACKUPDIR" ] # Check Backup Directory exists. + then + mkdir -p "$BACKUPDIR" +fi + +if [ ! -e "$BACKUPDIR/daily" ] # Check Daily Directory exists. + then + mkdir -p "$BACKUPDIR/daily" +fi + +if [ ! -e "$BACKUPDIR/weekly" ] # Check Weekly Directory exists. + then + mkdir -p "$BACKUPDIR/weekly" +fi + +if [ ! -e "$BACKUPDIR/monthly" ] # Check Monthly Directory exists. + then + mkdir -p "$BACKUPDIR/monthly" +fi + + +# IO redirection for logging. +touch $LOGFILE +exec 6>&1 # Link file descriptor #6 with stdout. + # Saves stdout. +exec > $LOGFILE # stdout replaced with file $LOGFILE. + + +# Functions + +# Database dump function +dbdump () { +pg_dump --username=$USERNAME $HOST $OPT $1 > $2 +return 0 +} + +# Compression function +SUFFIX="" +compression () { +if [ "$COMP" = "gzip" ]; then + gzip -f "$1" + echo + echo Backup Information for "$1" + gzip -l "$1.gz" + SUFFIX=".gz" +elif [ "$COMP" = "bzip2" ]; then + echo Compression information for "$1.bz2" + bzip2 -f -v $1 2>&1 + SUFFIX=".bz2" +else + echo "No compression option set, check advanced settings" +fi +return 0 +} + + +# Run command before we begin +if [ "$PREBACKUP" ] + then + echo ====================================================================== + echo "Prebackup command output." + echo + eval $PREBACKUP + echo + echo ====================================================================== + echo +fi + + +if [ "$SEPDIR" = "yes" ]; then # Check if CREATE DATABSE should be included in Dump + if [ "$CREATE_DATABASE" = "no" ]; then + OPT="$OPT" + else + OPT="$OPT --create" + fi +else + OPT="$OPT" +fi + +# Hostname for LOG information +if [ "$DBHOST" = "localhost" ]; then + DBHOST="`hostname -f`" + HOST="" +else + HOST="-h $DBHOST" +fi + +# If backing up all DBs on the server +if [ "$DBNAMES" = "all" ]; then + DBNAMES="`psql -U $USERNAME $HOST -l -A -F: | sed -ne "/:/ { /Name:Owner/d; /template0/d; s/:.*$//; p }"`" + + # If DBs are excluded + for exclude in $DBEXCLUDE + do + DBNAMES=`echo $DBNAMES | sed "s/\b$exclude\b//g"` + done + + MDBNAMES=$DBNAMES +fi + +echo ====================================================================== +echo AutoPostgreSQLBackup VER $VER +echo http://autopgsqlbackup.frozenpc.net/ +echo +echo Backup of Database Server - $DBHOST +echo ====================================================================== + +# Test is seperate DB backups are required +if [ "$SEPDIR" = "yes" ]; then +echo Backup Start Time `date` +echo ====================================================================== + # Monthly Full Backup of all Databases + if [ $DOM = "01" ]; then + for MDB in $MDBNAMES + do + + # Prepare $DB for using + MDB="`echo $MDB | sed 's/%/ /g'`" + + if [ ! -e "$BACKUPDIR/monthly/$MDB" ] # Check Monthly DB Directory exists. + then + mkdir -p "$BACKUPDIR/monthly/$MDB" + fi + echo Monthly Backup of $MDB... + dbdump "$MDB" "$BACKUPDIR/monthly/$MDB/${MDB}_$DATE.$M.$MDB.sql" + compression "$BACKUPDIR/monthly/$MDB/${MDB}_$DATE.$M.$MDB.sql" + BACKUPFILES="$BACKUPFILES $BACKUPDIR/monthly/$MDB/${MDB}_$DATE.$M.$MDB.sql$SUFFIX" + echo ---------------------------------------------------------------------- + done + fi + + for DB in $DBNAMES + do + # Prepare $DB for using + DB="`echo $DB | sed 's/%/ /g'`" + + # Create Separate directory for each DB + if [ ! -e "$BACKUPDIR/daily/$DB" ] # Check Daily DB Directory exists. + then + mkdir -p "$BACKUPDIR/daily/$DB" + fi + + if [ ! -e "$BACKUPDIR/weekly/$DB" ] # Check Weekly DB Directory exists. + then + mkdir -p "$BACKUPDIR/weekly/$DB" + fi + + # Weekly Backup + if [ $DNOW = $DOWEEKLY ]; then + echo Weekly Backup of Database \( $DB \) + echo Rotating 5 weeks Backups... + if [ "$W" -le 05 ];then + REMW=`expr 48 + $W` + elif [ "$W" -lt 15 ];then + REMW=0`expr $W - 5` + else + REMW=`expr $W - 5` + fi + eval rm -fv "$BACKUPDIR/weekly/$DB/week.$REMW.*" + echo + dbdump "$DB" "$BACKUPDIR/weekly/$DB/${DB}_week.$W.$DATE.sql" + compression "$BACKUPDIR/weekly/$DB/${DB}_week.$W.$DATE.sql" + BACKUPFILES="$BACKUPFILES $BACKUPDIR/weekly/$DB/${DB}_week.$W.$DATE.sql$SUFFIX" + echo ---------------------------------------------------------------------- + + # Daily Backup + else + echo Daily Backup of Database \( $DB \) + echo Rotating last weeks Backup... + eval rm -fv "$BACKUPDIR/daily/$DB/*.$DOW.sql.*" + echo + dbdump "$DB" "$BACKUPDIR/daily/$DB/${DB}_$DATE.$DOW.sql" + compression "$BACKUPDIR/daily/$DB/${DB}_$DATE.$DOW.sql" + BACKUPFILES="$BACKUPFILES $BACKUPDIR/daily/$DB/${DB}_$DATE.$DOW.sql$SUFFIX" + echo ---------------------------------------------------------------------- + fi + done +echo Backup End `date` +echo ====================================================================== + + +else # One backup file for all DBs +echo Backup Start `date` +echo ====================================================================== + # Monthly Full Backup of all Databases + if [ $DOM = "01" ]; then + echo Monthly full Backup of \( $MDBNAMES \)... + dbdump "$MDBNAMES" "$BACKUPDIR/monthly/$DATE.$M.all-databases.sql" + compression "$BACKUPDIR/monthly/$DATE.$M.all-databases.sql" + BACKUPFILES="$BACKUPFILES $BACKUPDIR/monthly/$DATE.$M.all-databases.sql$SUFFIX" + echo ---------------------------------------------------------------------- + fi + + # Weekly Backup + if [ $DNOW = $DOWEEKLY ]; then + echo Weekly Backup of Databases \( $DBNAMES \) + echo + echo Rotating 5 weeks Backups... + if [ "$W" -le 05 ];then + REMW=`expr 48 + $W` + elif [ "$W" -lt 15 ];then + REMW=0`expr $W - 5` + else + REMW=`expr $W - 5` + fi + eval rm -fv "$BACKUPDIR/weekly/week.$REMW.*" + echo + dbdump "$DBNAMES" "$BACKUPDIR/weekly/week.$W.$DATE.sql" + compression "$BACKUPDIR/weekly/week.$W.$DATE.sql" + BACKUPFILES="$BACKUPFILES $BACKUPDIR/weekly/week.$W.$DATE.sql$SUFFIX" + echo ---------------------------------------------------------------------- + + # Daily Backup + else + echo Daily Backup of Databases \( $DBNAMES \) + echo + echo Rotating last weeks Backup... + eval rm -fv "$BACKUPDIR/daily/*.$DOW.sql.*" + echo + dbdump "$DBNAMES" "$BACKUPDIR/daily/$DATE.$DOW.sql" + compression "$BACKUPDIR/daily/$DATE.$DOW.sql" + BACKUPFILES="$BACKUPFILES $BACKUPDIR/daily/$DATE.$DOW.sql$SUFFIX" + echo ---------------------------------------------------------------------- + fi +echo Backup End Time `date` +echo ====================================================================== +fi +echo Total disk space used for backup storage.. +echo Size - Location +echo `du -hs "$BACKUPDIR"` +echo + + +# Run command when we're done +if [ "$POSTBACKUP" ] + then + echo ====================================================================== + echo "Postbackup command output." + echo + eval $POSTBACKUP + echo + echo ====================================================================== +fi + +#Clean up IO redirection +exec 1>&6 6>&- # Restore stdout and close file descriptor #6. + +if [ "$MAILCONTENT" = "files" ] +then + #Get backup size + ATTSIZE=`du -c $BACKUPFILES | grep "[[:digit:][:space:]]total$" |sed s/\s*total//` + if [ $MAXATTSIZE -ge $ATTSIZE ] + then + BACKUPFILES=`echo "$BACKUPFILES" | sed -e "s# # -a #g"` #enable multiple attachments + mutt -s "PostgreSQL Backup Log and SQL Files for $DBHOST - $DATE" $BACKUPFILES $MAILADDR < $LOGFILE #send via mutt + else + cat "$LOGFILE" | mail -s "WARNING! - PostgreSQL Backup exceeds set maximum attachment size on $HOST - $DATE" $MAILADDR + fi +elif [ "$MAILCONTENT" = "log" ] +then + cat "$LOGFILE" | mail -s "PostgreSQL Backup Log for $DBHOST - $DATE" $MAILADDR +else + cat "$LOGFILE" +fi + +# Clean up Logfile +eval rm -f "$LOGFILE" + +exit 0 \ No newline at end of file diff --git a/9.5/alpine/docker-entrypoint.sh b/9.5/alpine/docker-entrypoint.sh index 2c87e77fb1..af29536955 100755 --- a/9.5/alpine/docker-entrypoint.sh +++ b/9.5/alpine/docker-entrypoint.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash set -e # usage: file_env VAR [DEFAULT] @@ -27,19 +27,38 @@ if [ "${1:0:1}" = '-' ]; then set -- postgres "$@" fi -if [ "$1" = 'postgres' ]; then +# allow the container to be started with `--user` +if [ "$1" = 'postgres' ] && [ "$(id -u)" = '0' ]; then mkdir -p "$PGDATA" - chmod 700 "$PGDATA" chown -R postgres "$PGDATA" + chmod 700 "$PGDATA" + + mkdir -p /var/run/postgresql + chown -R postgres /var/run/postgresql + chmod 775 /var/run/postgresql - mkdir -p /run/postgresql - chmod g+s /run/postgresql - chown -R postgres /run/postgresql + # Create the transaction log directory before initdb is run (below) so the directory is owned by the correct user + if [ "$POSTGRES_INITDB_XLOGDIR" ]; then + mkdir -p "$POSTGRES_INITDB_XLOGDIR" + chown -R postgres "$POSTGRES_INITDB_XLOGDIR" + chmod 700 "$POSTGRES_INITDB_XLOGDIR" + fi + + exec su-exec postgres "$BASH_SOURCE" "$@" +fi + +if [ "$1" = 'postgres' ]; then + mkdir -p "$PGDATA" + chown -R "$(id -u)" "$PGDATA" 2>/dev/null || : + chmod 700 "$PGDATA" 2>/dev/null || : # look specifically for PG_VERSION, as it is expected in the DB dir if [ ! -s "$PGDATA/PG_VERSION" ]; then file_env 'POSTGRES_INITDB_ARGS' - eval "su-exec postgres initdb $POSTGRES_INITDB_ARGS" + if [ "$POSTGRES_INITDB_XLOGDIR" ]; then + export POSTGRES_INITDB_ARGS="$POSTGRES_INITDB_ARGS --xlogdir $POSTGRES_INITDB_XLOGDIR" + fi + eval "initdb --username=postgres $POSTGRES_INITDB_ARGS" # check password first so we can output the warning before postgres # messes it up @@ -67,11 +86,15 @@ if [ "$1" = 'postgres' ]; then authMethod=trust fi - { echo; echo "host all all all $authMethod"; } | su-exec postgres tee -a "$PGDATA/pg_hba.conf" > /dev/null + { + echo + echo "host all all all $authMethod" + } >> "$PGDATA/pg_hba.conf" - # internal start of server in order to allow set-up using psql-client + # internal start of server in order to allow set-up using psql-client # does not listen on external TCP/IP and waits until start finishes - su-exec postgres pg_ctl -D "$PGDATA" \ + PGUSER="${PGUSER:-postgres}" \ + pg_ctl -D "$PGDATA" \ -o "-c listen_addresses='localhost'" \ -w start @@ -110,14 +133,15 @@ if [ "$1" = 'postgres' ]; then echo done - su-exec postgres pg_ctl -D "$PGDATA" -m fast -w stop + PGUSER="${PGUSER:-postgres}" \ + pg_ctl -D "$PGDATA" -m fast -w stop echo echo 'PostgreSQL init process complete; ready for start up.' echo fi - - exec su-exec postgres "$@" +else + crond -bS fi exec "$@" diff --git a/9.6/Dockerfile b/9.6/Dockerfile index b71f682633..a7a738bd82 100755 --- a/9.6/Dockerfile +++ b/9.6/Dockerfile @@ -50,9 +50,9 @@ RUN mkdir -p /var/run/postgresql && chown -R postgres /var/run/postgresql ENV PATH /usr/lib/postgresql/$PG_MAJOR/bin:$PATH ENV PGDATA /var/lib/postgresql/data VOLUME /var/lib/postgresql/data -VOLUME /var/lib/autopostgresqlbackup +VOLUME /var/backups/postgres COPY docker-entrypoint.sh / - +COPY autopgsqlbackup.sh /usr/sbin/autopgsqlbackup ENTRYPOINT ["/docker-entrypoint.sh"] EXPOSE 5432 diff --git a/9.6/alpine/Dockerfile b/9.6/alpine/Dockerfile index d41e9b186a..52cb63ad13 100755 --- a/9.6/alpine/Dockerfile +++ b/9.6/alpine/Dockerfile @@ -1,26 +1,37 @@ # vim:set ft=dockerfile: -FROM alpine:3.4 +FROM alpine:3.5 # alpine includes "postgres" user/group in base install -# /etc/passwd:22:postgres:x:70:70::/var/lib/postgresql:/bin/sh -# /etc/group:34:postgres:x:70: +# /etc/passwd:22:postgres:x:70:70::/var/lib/postgresql:/bin/sh +# /etc/group:34:postgres:x:70: +# the home directory for the postgres user, however, is not created by default +# see https://github.com/docker-library/postgres/issues/274 +RUN set -ex; \ + postgresHome="$(getent passwd postgres)"; \ + postgresHome="$(echo "$postgresHome" | cut -d: -f6)"; \ + [ "$postgresHome" = '/var/lib/postgresql' ]; \ + mkdir -p "$postgresHome"; \ + chown -R postgres:postgres "$postgresHome" # su-exec (gosu-compatible) is installed further down # make the "en_US.UTF-8" locale so postgres will be utf-8 enabled by default # alpine doesn't require explicit locale-file generation ENV LANG en_US.utf8 +ENV TZ=Asia/Bangkok + RUN mkdir /docker-entrypoint-initdb.d ENV PG_MAJOR 9.6 -ENV PG_VERSION 9.6.1 -ENV PG_SHA256 e5101e0a49141fc12a7018c6dad594694d3a3325f5ab71e93e0e51bd94e51fcd +ENV PG_VERSION 9.6.4 +ENV PG_SHA256 2b3ab16d82e21cead54c08b95ce3ac480696944a68603b6c11b3205b7376ce13 RUN set -ex \ \ && apk add --no-cache --virtual .fetch-deps \ ca-certificates \ + tzdata \ openssl \ tar \ \ @@ -36,6 +47,8 @@ RUN set -ex \ \ && apk add --no-cache --virtual .build-deps \ bison \ + coreutils \ + dpkg-dev dpkg \ flex \ gcc \ # krb5-dev \ @@ -46,7 +59,10 @@ RUN set -ex \ make \ # openldap-dev \ openssl-dev \ +# configure: error: prove not found perl \ +# configure: error: Perl module IPC::Run is required to run TAP tests + perl-ipc-run \ # perl-dev \ # python-dev \ # python3-dev \ @@ -55,9 +71,19 @@ RUN set -ex \ zlib-dev \ \ && cd /usr/src/postgresql \ +# update "DEFAULT_PGSOCKET_DIR" to "/var/run/postgresql" (matching Debian) +# see https://anonscm.debian.org/git/pkg-postgresql/postgresql.git/tree/debian/patches/51-default-sockets-in-var.patch?id=8b539fcb3e093a521c095e70bdfa76887217b89f + && awk '$1 == "#define" && $2 == "DEFAULT_PGSOCKET_DIR" && $3 == "\"/tmp\"" { $3 = "\"/var/run/postgresql\""; print; next } { print }' src/include/pg_config_manual.h > src/include/pg_config_manual.h.new \ + && grep '/var/run/postgresql' src/include/pg_config_manual.h.new \ + && mv src/include/pg_config_manual.h.new src/include/pg_config_manual.h \ + && gnuArch="$(dpkg-architecture --query DEB_BUILD_GNU_TYPE)" \ +# explicitly update autoconf config.guess and config.sub so they support more arches/libcs + && wget -O config/config.guess 'https://git.savannah.gnu.org/cgit/config.git/plain/config.guess?id=7d3d27baf8107b630586c962c057e22149653deb' \ + && wget -O config/config.sub 'https://git.savannah.gnu.org/cgit/config.git/plain/config.sub?id=7d3d27baf8107b630586c962c057e22149653deb' \ # configure options taken from: # https://anonscm.debian.org/cgit/pkg-postgresql/postgresql.git/tree/debian/rules?h=9.5 && ./configure \ + --build="$gnuArch" \ # "/usr/src/postgresql/src/backend/access/common/tupconvert.c:105: undefined reference to `libintl_gettext'" # --enable-nls \ --enable-integer-datetimes \ @@ -71,6 +97,8 @@ RUN set -ex \ --with-pgport=5432 \ --with-system-tzdata=/usr/share/zoneinfo \ --prefix=/usr/local \ + --with-includes=/usr/local/include \ + --with-libraries=/usr/local/lib \ \ # these make our image abnormally large (at least 100MB larger), which seems uncouth for an "Alpine" (ie, "small") variant :) # --with-krb5 \ @@ -83,7 +111,7 @@ RUN set -ex \ --with-openssl \ --with-libxml \ --with-libxslt \ - && make -j "$(getconf _NPROCESSORS_ONLN)" world \ + && make -j "$(nproc)" world \ && make install-world \ && make -C contrib install \ \ @@ -105,7 +133,6 @@ RUN set -ex \ && cd / \ && rm -rf \ /usr/src/postgresql \ - /usr/local/include/* \ /usr/local/share/doc \ /usr/local/share/man \ && find /usr/local -name '*.a' -delete @@ -113,15 +140,26 @@ RUN set -ex \ # make the sample config easier to munge (and "correct by default") RUN sed -ri "s!^#?(listen_addresses)\s*=\s*\S+.*!\1 = '*'!" /usr/local/share/postgresql/postgresql.conf.sample -RUN mkdir -p /var/run/postgresql && chown -R postgres /var/run/postgresql +RUN mkdir -p /var/run/postgresql && chown -R postgres:postgres /var/run/postgresql && chmod 2777 /var/run/postgresql ENV PATH /usr/lib/postgresql/$PG_MAJOR/bin:$PATH ENV PGDATA /var/lib/postgresql/data +RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 777 "$PGDATA" # this 777 will be replaced by 700 at runtime (allows semi-arbitrary "--user" values) + + + VOLUME /var/lib/postgresql/data +VOLUME /var/backups/postgres +COPY autopgsqlbackup.sh /usr/sbin/autopgsqlbackup + +RUN echo -e "#!/bin/sh\n/usr/sbin/autopgsqlbackup" > /etc/periodic/daily/autopgbackup +RUN chmod +x /etc/periodic/daily/autopgbackup + -COPY docker-entrypoint.sh / -ENTRYPOINT ["/docker-entrypoint.sh"] +COPY docker-entrypoint.sh /usr/local/bin/ +RUN ln -s usr/local/bin/docker-entrypoint.sh / # backwards compat +ENTRYPOINT ["docker-entrypoint.sh"] EXPOSE 5432 CMD ["postgres"] diff --git a/9.6/alpine/autopgsqlbackup.sh b/9.6/alpine/autopgsqlbackup.sh new file mode 100755 index 0000000000..5acb6535b3 --- /dev/null +++ b/9.6/alpine/autopgsqlbackup.sh @@ -0,0 +1,497 @@ +#!/bin/bash +# +# PostgreSQL Backup Script Ver 1.0 +# http://autopgsqlbackup.frozenpc.net +# Copyright (c) 2005 Aaron Axelsen +# +# This script is based of the AutoMySQLBackup Script Ver 2.2 +# It can be found at http://sourceforge.net/projects/automysqlbackup/ +# +# The PostgreSQL changes are based on a patch agaisnt AutoMySQLBackup 1.9 +# created by Friedrich Lobenstock +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +#===================================================================== +# Set the following variables to your system needs +# (Detailed instructions below variables) +#===================================================================== + +# Username to access the PostgreSQL server e.g. dbuser +USERNAME=postgres + +# Password +# create a file $HOME/.pgpass containing a line like this +# hostname:*:*:dbuser:dbpass +# replace hostname with the value of DBHOST and postgres with +# the value of USERNAME + +# Host name (or IP address) of PostgreSQL server e.g localhost +DBHOST=localhost + +# List of DBNAMES for Daily/Weekly Backup e.g. "DB1 DB2 DB3" +DBNAMES="all" + +# Backup directory location e.g /backups +BACKUPDIR="/var/backups/postgres" + +# Mail setup +# What would you like to be mailed to you? +# - log : send only log file +# - files : send log file and sql files as attachments (see docs) +# - stdout : will simply output the log to the screen if run manually. +MAILCONTENT="stdout" + +# Set the maximum allowed email size in k. (4000 = approx 5MB email [see docs]) +MAXATTSIZE="4000" + +# Email Address to send mail to? (user@domain.com) +MAILADDR="root@localhost" + + +# ============================================================ +# === ADVANCED OPTIONS ( Read the doc's below for details )=== +#============================================================= + +# List of DBBNAMES for Monthly Backups. +MDBNAMES="template1 $DBNAMES" + +# List of DBNAMES to EXLUCDE if DBNAMES are set to all (must be in " quotes) +DBEXCLUDE="" + +# Include CREATE DATABASE in backup? +CREATE_DATABASE=yes + +# Separate backup directory and file for each DB? (yes or no) +SEPDIR=yes + +# Which day do you want weekly backups? (1 to 7 where 1 is Monday) +DOWEEKLY=6 + +# Choose Compression type. (gzip or bzip2) +COMP=bzip2 + +# Command to run before backups (uncomment to use) +#PREBACKUP="/etc/pgsql-backup-pre" + +# Command run after backups (uncomment to use) +#POSTBACKUP="bash /home/backups/scripts/ftp_pgsql" + +#===================================================================== +# Options documentation +#===================================================================== +# Set USERNAME and PASSWORD of a user that has at least SELECT permission +# to ALL databases. +# +# Set the DBHOST option to the server you wish to backup, leave the +# default to backup "this server".(to backup multiple servers make +# copies of this file and set the options for that server) +# +# Put in the list of DBNAMES(Databases)to be backed up. If you would like +# to backup ALL DBs on the server set DBNAMES="all".(if set to "all" then +# any new DBs will automatically be backed up without needing to modify +# this backup script when a new DB is created). +# +# If the DB you want to backup has a space in the name replace the space +# with a % e.g. "data base" will become "data%base" +# NOTE: Spaces in DB names may not work correctly when SEPDIR=no. +# +# You can change the backup storage location from /backups to anything +# you like by using the BACKUPDIR setting.. +# +# The MAILCONTENT and MAILADDR options and pretty self explanitory, use +# these to have the backup log mailed to you at any email address or multiple +# email addresses in a space seperated list. +# (If you set mail content to "log" you will require access to the "mail" program +# on your server. If you set this to "files" you will have to have mutt installed +# on your server. If you set it sto stdout it will log to the screen if run from +# the console or to the cron job owner if run through cron) +# +# MAXATTSIZE sets the largest allowed email attachments total (all backup files) you +# want the script to send. This is the size before it is encoded to be sent as an email +# so if your mail server will allow a maximum mail size of 5MB I would suggest setting +# MAXATTSIZE to be 25% smaller than that so a setting of 4000 would probably be fine. +# +# Finally copy automysqlbackup.sh to anywhere on your server and make sure +# to set executable permission. You can also copy the script to +# /etc/cron.daily to have it execute automatically every night or simply +# place a symlink in /etc/cron.daily to the file if you wish to keep it +# somwhere else. +# NOTE:On Debian copy the file with no extention for it to be run +# by cron e.g just name the file "automysqlbackup" +# +# Thats it.. +# +# +# === Advanced options doc's === +# +# The list of MDBNAMES is the DB's to be backed up only monthly. You should +# always include "mysql" in this list to backup your user/password +# information along with any other DBs that you only feel need to +# be backed up monthly. (if using a hosted server then you should +# probably remove "mysql" as your provider will be backing this up) +# NOTE: If DBNAMES="all" then MDBNAMES has no effect as all DBs will be backed +# up anyway. +# +# If you set DBNAMES="all" you can configure the option DBEXCLUDE. Other +# wise this option will not be used. +# This option can be used if you want to backup all dbs, but you want +# exclude some of them. (eg. a db is to big). +# +# Set CREATE_DATABASE to "yes" (the default) if you want your SQL-Dump to create +# a database with the same name as the original database when restoring. +# Saying "no" here will allow your to specify the database name you want to +# restore your dump into, making a copy of the database by using the dump +# created with automysqlbackup. +# NOTE: Not used if SEPDIR=no +# +# The SEPDIR option allows you to choose to have all DBs backed up to +# a single file (fast restore of entire server in case of crash) or to +# seperate directories for each DB (each DB can be restored seperately +# in case of single DB corruption or loss). +# +# To set the day of the week that you would like the weekly backup to happen +# set the DOWEEKLY setting, this can be a value from 1 to 7 where 1 is Monday, +# The default is 6 which means that weekly backups are done on a Saturday. +# +# COMP is used to choose the copmression used, options are gzip or bzip2. +# bzip2 will produce slightly smaller files but is more processor intensive so +# may take longer to complete. +# +# Use PREBACKUP and POSTBACKUP to specify Per and Post backup commands +# or scripts to perform tasks either before or after the backup process. +# +# +#===================================================================== +# Backup Rotation.. +#===================================================================== +# +# Daily Backups are rotated weekly.. +# Weekly Backups are run by default on Saturday Morning when +# cron.daily scripts are run...Can be changed with DOWEEKLY setting.. +# Weekly Backups are rotated on a 5 week cycle.. +# Monthly Backups are run on the 1st of the month.. +# Monthly Backups are NOT rotated automatically... +# It may be a good idea to copy Monthly backups offline or to another +# server.. +# +#===================================================================== +# Please Note!! +#===================================================================== +# +# I take no resposibility for any data loss or corruption when using +# this script.. +# This script will not help in the event of a hard drive crash. If a +# copy of the backup has not be stored offline or on another PC.. +# You should copy your backups offline regularly for best protection. +# +# Happy backing up... +# +#===================================================================== +# Change Log +#===================================================================== +# +# VER 1.0 - (2005-03-25) +# Initial Release - based on AutoMySQLBackup 2.2 +# +#===================================================================== +#===================================================================== +# +# Should not need to be modified from here down!! +# +#===================================================================== +#===================================================================== +PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/postgres/bin:/usr/local/pgsql/bin +DATE=`date +%Y-%m-%d` # Datestamp e.g 2002-09-21 +DOW=`date +%A` # Day of the week e.g. Monday +DNOW=`date +%u` # Day number of the week 1 to 7 where 1 represents Monday +DOM=`date +%d` # Date of the Month e.g. 27 +M=`date +%B` # Month e.g January +W=`date +%V` # Week Number e.g 37 +VER=1.0 # Version Number +LOGFILE=$BACKUPDIR/$DBHOST-`date +%N`.log # Logfile Name +OPT="" # OPT string for use with mysqldump ( see man mysqldump ) +BACKUPFILES="" # thh: added for later mailing + +# Create required directories +if [ ! -e "$BACKUPDIR" ] # Check Backup Directory exists. + then + mkdir -p "$BACKUPDIR" +fi + +if [ ! -e "$BACKUPDIR/daily" ] # Check Daily Directory exists. + then + mkdir -p "$BACKUPDIR/daily" +fi + +if [ ! -e "$BACKUPDIR/weekly" ] # Check Weekly Directory exists. + then + mkdir -p "$BACKUPDIR/weekly" +fi + +if [ ! -e "$BACKUPDIR/monthly" ] # Check Monthly Directory exists. + then + mkdir -p "$BACKUPDIR/monthly" +fi + + +# IO redirection for logging. +touch $LOGFILE +exec 6>&1 # Link file descriptor #6 with stdout. + # Saves stdout. +exec > $LOGFILE # stdout replaced with file $LOGFILE. + + +# Functions + +# Database dump function +dbdump () { +pg_dump --username=$USERNAME $HOST $OPT $1 > $2 +return 0 +} + +# Compression function +SUFFIX="" +compression () { +if [ "$COMP" = "gzip" ]; then + gzip -f "$1" + echo + echo Backup Information for "$1" + gzip -l "$1.gz" + SUFFIX=".gz" +elif [ "$COMP" = "bzip2" ]; then + echo Compression information for "$1.bz2" + bzip2 -f -v $1 2>&1 + SUFFIX=".bz2" +else + echo "No compression option set, check advanced settings" +fi +return 0 +} + + +# Run command before we begin +if [ "$PREBACKUP" ] + then + echo ====================================================================== + echo "Prebackup command output." + echo + eval $PREBACKUP + echo + echo ====================================================================== + echo +fi + + +if [ "$SEPDIR" = "yes" ]; then # Check if CREATE DATABSE should be included in Dump + if [ "$CREATE_DATABASE" = "no" ]; then + OPT="$OPT" + else + OPT="$OPT --create" + fi +else + OPT="$OPT" +fi + +# Hostname for LOG information +if [ "$DBHOST" = "localhost" ]; then + DBHOST="`hostname -f`" + HOST="" +else + HOST="-h $DBHOST" +fi + +# If backing up all DBs on the server +if [ "$DBNAMES" = "all" ]; then + DBNAMES="`psql -U $USERNAME $HOST -l -A -F: | sed -ne "/:/ { /Name:Owner/d; /template0/d; s/:.*$//; p }"`" + + # If DBs are excluded + for exclude in $DBEXCLUDE + do + DBNAMES=`echo $DBNAMES | sed "s/\b$exclude\b//g"` + done + + MDBNAMES=$DBNAMES +fi + +echo ====================================================================== +echo AutoPostgreSQLBackup VER $VER +echo http://autopgsqlbackup.frozenpc.net/ +echo +echo Backup of Database Server - $DBHOST +echo ====================================================================== + +# Test is seperate DB backups are required +if [ "$SEPDIR" = "yes" ]; then +echo Backup Start Time `date` +echo ====================================================================== + # Monthly Full Backup of all Databases + if [ $DOM = "01" ]; then + for MDB in $MDBNAMES + do + + # Prepare $DB for using + MDB="`echo $MDB | sed 's/%/ /g'`" + + if [ ! -e "$BACKUPDIR/monthly/$MDB" ] # Check Monthly DB Directory exists. + then + mkdir -p "$BACKUPDIR/monthly/$MDB" + fi + echo Monthly Backup of $MDB... + dbdump "$MDB" "$BACKUPDIR/monthly/$MDB/${MDB}_$DATE.$M.$MDB.sql" + compression "$BACKUPDIR/monthly/$MDB/${MDB}_$DATE.$M.$MDB.sql" + BACKUPFILES="$BACKUPFILES $BACKUPDIR/monthly/$MDB/${MDB}_$DATE.$M.$MDB.sql$SUFFIX" + echo ---------------------------------------------------------------------- + done + fi + + for DB in $DBNAMES + do + # Prepare $DB for using + DB="`echo $DB | sed 's/%/ /g'`" + + # Create Separate directory for each DB + if [ ! -e "$BACKUPDIR/daily/$DB" ] # Check Daily DB Directory exists. + then + mkdir -p "$BACKUPDIR/daily/$DB" + fi + + if [ ! -e "$BACKUPDIR/weekly/$DB" ] # Check Weekly DB Directory exists. + then + mkdir -p "$BACKUPDIR/weekly/$DB" + fi + + # Weekly Backup + if [ $DNOW = $DOWEEKLY ]; then + echo Weekly Backup of Database \( $DB \) + echo Rotating 5 weeks Backups... + if [ "$W" -le 05 ];then + REMW=`expr 48 + $W` + elif [ "$W" -lt 15 ];then + REMW=0`expr $W - 5` + else + REMW=`expr $W - 5` + fi + eval rm -fv "$BACKUPDIR/weekly/$DB/week.$REMW.*" + echo + dbdump "$DB" "$BACKUPDIR/weekly/$DB/${DB}_week.$W.$DATE.sql" + compression "$BACKUPDIR/weekly/$DB/${DB}_week.$W.$DATE.sql" + BACKUPFILES="$BACKUPFILES $BACKUPDIR/weekly/$DB/${DB}_week.$W.$DATE.sql$SUFFIX" + echo ---------------------------------------------------------------------- + + # Daily Backup + else + echo Daily Backup of Database \( $DB \) + echo Rotating last weeks Backup... + eval rm -fv "$BACKUPDIR/daily/$DB/*.$DOW.sql.*" + echo + dbdump "$DB" "$BACKUPDIR/daily/$DB/${DB}_$DATE.$DOW.sql" + compression "$BACKUPDIR/daily/$DB/${DB}_$DATE.$DOW.sql" + BACKUPFILES="$BACKUPFILES $BACKUPDIR/daily/$DB/${DB}_$DATE.$DOW.sql$SUFFIX" + echo ---------------------------------------------------------------------- + fi + done +echo Backup End `date` +echo ====================================================================== + + +else # One backup file for all DBs +echo Backup Start `date` +echo ====================================================================== + # Monthly Full Backup of all Databases + if [ $DOM = "01" ]; then + echo Monthly full Backup of \( $MDBNAMES \)... + dbdump "$MDBNAMES" "$BACKUPDIR/monthly/$DATE.$M.all-databases.sql" + compression "$BACKUPDIR/monthly/$DATE.$M.all-databases.sql" + BACKUPFILES="$BACKUPFILES $BACKUPDIR/monthly/$DATE.$M.all-databases.sql$SUFFIX" + echo ---------------------------------------------------------------------- + fi + + # Weekly Backup + if [ $DNOW = $DOWEEKLY ]; then + echo Weekly Backup of Databases \( $DBNAMES \) + echo + echo Rotating 5 weeks Backups... + if [ "$W" -le 05 ];then + REMW=`expr 48 + $W` + elif [ "$W" -lt 15 ];then + REMW=0`expr $W - 5` + else + REMW=`expr $W - 5` + fi + eval rm -fv "$BACKUPDIR/weekly/week.$REMW.*" + echo + dbdump "$DBNAMES" "$BACKUPDIR/weekly/week.$W.$DATE.sql" + compression "$BACKUPDIR/weekly/week.$W.$DATE.sql" + BACKUPFILES="$BACKUPFILES $BACKUPDIR/weekly/week.$W.$DATE.sql$SUFFIX" + echo ---------------------------------------------------------------------- + + # Daily Backup + else + echo Daily Backup of Databases \( $DBNAMES \) + echo + echo Rotating last weeks Backup... + eval rm -fv "$BACKUPDIR/daily/*.$DOW.sql.*" + echo + dbdump "$DBNAMES" "$BACKUPDIR/daily/$DATE.$DOW.sql" + compression "$BACKUPDIR/daily/$DATE.$DOW.sql" + BACKUPFILES="$BACKUPFILES $BACKUPDIR/daily/$DATE.$DOW.sql$SUFFIX" + echo ---------------------------------------------------------------------- + fi +echo Backup End Time `date` +echo ====================================================================== +fi +echo Total disk space used for backup storage.. +echo Size - Location +echo `du -hs "$BACKUPDIR"` +echo + + +# Run command when we're done +if [ "$POSTBACKUP" ] + then + echo ====================================================================== + echo "Postbackup command output." + echo + eval $POSTBACKUP + echo + echo ====================================================================== +fi + +#Clean up IO redirection +exec 1>&6 6>&- # Restore stdout and close file descriptor #6. + +if [ "$MAILCONTENT" = "files" ] +then + #Get backup size + ATTSIZE=`du -c $BACKUPFILES | grep "[[:digit:][:space:]]total$" |sed s/\s*total//` + if [ $MAXATTSIZE -ge $ATTSIZE ] + then + BACKUPFILES=`echo "$BACKUPFILES" | sed -e "s# # -a #g"` #enable multiple attachments + mutt -s "PostgreSQL Backup Log and SQL Files for $DBHOST - $DATE" $BACKUPFILES $MAILADDR < $LOGFILE #send via mutt + else + cat "$LOGFILE" | mail -s "WARNING! - PostgreSQL Backup exceeds set maximum attachment size on $HOST - $DATE" $MAILADDR + fi +elif [ "$MAILCONTENT" = "log" ] +then + cat "$LOGFILE" | mail -s "PostgreSQL Backup Log for $DBHOST - $DATE" $MAILADDR +else + cat "$LOGFILE" +fi + +# Clean up Logfile +eval rm -f "$LOGFILE" + +exit 0 \ No newline at end of file diff --git a/9.6/alpine/docker-entrypoint.sh b/9.6/alpine/docker-entrypoint.sh index 2c87e77fb1..53c52996ec 100755 --- a/9.6/alpine/docker-entrypoint.sh +++ b/9.6/alpine/docker-entrypoint.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash set -e # usage: file_env VAR [DEFAULT] @@ -27,19 +27,38 @@ if [ "${1:0:1}" = '-' ]; then set -- postgres "$@" fi -if [ "$1" = 'postgres' ]; then +# allow the container to be started with `--user` +if [ "$1" = 'postgres' ] && [ "$(id -u)" = '0' ]; then mkdir -p "$PGDATA" - chmod 700 "$PGDATA" chown -R postgres "$PGDATA" + chmod 700 "$PGDATA" + + mkdir -p /var/run/postgresql + chown -R postgres /var/run/postgresql + chmod 775 /var/run/postgresql + + # Create the transaction log directory before initdb is run (below) so the directory is owned by the correct user + if [ "$POSTGRES_INITDB_XLOGDIR" ]; then + mkdir -p "$POSTGRES_INITDB_XLOGDIR" + chown -R postgres "$POSTGRES_INITDB_XLOGDIR" + chmod 700 "$POSTGRES_INITDB_XLOGDIR" + fi + crond -bS + exec su-exec postgres "$BASH_SOURCE" "$@" +fi - mkdir -p /run/postgresql - chmod g+s /run/postgresql - chown -R postgres /run/postgresql +if [ "$1" = 'postgres' ]; then + mkdir -p "$PGDATA" + chown -R "$(id -u)" "$PGDATA" 2>/dev/null || : + chmod 700 "$PGDATA" 2>/dev/null || : # look specifically for PG_VERSION, as it is expected in the DB dir if [ ! -s "$PGDATA/PG_VERSION" ]; then file_env 'POSTGRES_INITDB_ARGS' - eval "su-exec postgres initdb $POSTGRES_INITDB_ARGS" + if [ "$POSTGRES_INITDB_XLOGDIR" ]; then + export POSTGRES_INITDB_ARGS="$POSTGRES_INITDB_ARGS --xlogdir $POSTGRES_INITDB_XLOGDIR" + fi + eval "initdb --username=postgres $POSTGRES_INITDB_ARGS" # check password first so we can output the warning before postgres # messes it up @@ -67,11 +86,15 @@ if [ "$1" = 'postgres' ]; then authMethod=trust fi - { echo; echo "host all all all $authMethod"; } | su-exec postgres tee -a "$PGDATA/pg_hba.conf" > /dev/null + { + echo + echo "host all all all $authMethod" + } >> "$PGDATA/pg_hba.conf" - # internal start of server in order to allow set-up using psql-client + # internal start of server in order to allow set-up using psql-client # does not listen on external TCP/IP and waits until start finishes - su-exec postgres pg_ctl -D "$PGDATA" \ + PGUSER="${PGUSER:-postgres}" \ + pg_ctl -D "$PGDATA" \ -o "-c listen_addresses='localhost'" \ -w start @@ -110,14 +133,13 @@ if [ "$1" = 'postgres' ]; then echo done - su-exec postgres pg_ctl -D "$PGDATA" -m fast -w stop + PGUSER="${PGUSER:-postgres}" \ + pg_ctl -D "$PGDATA" -m fast -w stop echo echo 'PostgreSQL init process complete; ready for start up.' echo fi - - exec su-exec postgres "$@" fi exec "$@"