Code Monkey home page Code Monkey logo

docker-postgres-backup-local's Introduction

Docker pulls GitHub actions

postgres-backup-local

Backup PostgresSQL to the local filesystem with periodic rotating backups, based on schickling/postgres-backup-s3. Backup multiple databases from the same host by setting the database names in POSTGRES_DB separated by commas or spaces.

Supports the following Docker architectures: linux/amd64, linux/arm64, linux/arm/v7, linux/s390x, linux/ppc64le.

Please consider reading detailed the How the backups folder works?.

This application requires the docker volume /backups to be a POSIX-compliant filesystem to store the backups (mainly with support for hardlinks and softlinks). So filesystems like VFAT, EXFAT, SMB/CIFS, ... can't be used with this docker image.

Usage

Docker:

docker run -u postgres:postgres -e POSTGRES_HOST=postgres -e POSTGRES_DB=dbname -e POSTGRES_USER=user -e POSTGRES_PASSWORD=password  prodrigestivill/postgres-backup-local

Docker Compose:

version: '2'
services:
    postgres:
        image: postgres
        restart: always
        environment:
            - POSTGRES_DB=database
            - POSTGRES_USER=username
            - POSTGRES_PASSWORD=password
         #  - POSTGRES_PASSWORD_FILE=/run/secrets/db_password <-- alternative for POSTGRES_PASSWORD (to use with docker secrets)
    pgbackups:
        image: prodrigestivill/postgres-backup-local
        restart: always
        user: postgres:postgres # Optional: see below
        volumes:
            - /var/opt/pgbackups:/backups
        links:
            - postgres
        depends_on:
            - postgres
        environment:
            - POSTGRES_HOST=postgres
            - POSTGRES_DB=database
            - POSTGRES_USER=username
            - POSTGRES_PASSWORD=password
         #  - POSTGRES_PASSWORD_FILE=/run/secrets/db_password <-- alternative for POSTGRES_PASSWORD (to use with docker secrets)
            - POSTGRES_EXTRA_OPTS=-Z1 --schema=public --blobs
            - SCHEDULE=@daily
            - BACKUP_KEEP_DAYS=7
            - BACKUP_KEEP_WEEKS=4
            - BACKUP_KEEP_MONTHS=6
            - HEALTHCHECK_PORT=8080

For security reasons it is recommended to run it as user postgres:postgres.

In case of running as postgres user, the system administrator must initialize the permission of the destination folder as follows:

# for default images (debian)
mkdir -p /var/opt/pgbackups && chown -R 999:999 /var/opt/pgbackups
# for alpine images
mkdir -p /var/opt/pgbackups && chown -R 70:70 /var/opt/pgbackups

Environment Variables

Most variables are the same as in the official postgres image.

env variable description
BACKUP_DIR Directory to save the backup at. Defaults to /backups.
BACKUP_SUFFIX Filename suffix to save the backup. Defaults to .sql.gz.
BACKUP_KEEP_DAYS Number of daily backups to keep before removal. Defaults to 7.
BACKUP_KEEP_WEEKS Number of weekly backups to keep before removal. Defaults to 4.
BACKUP_KEEP_MONTHS Number of monthly backups to keep before removal. Defaults to 6.
BACKUP_KEEP_MINS Number of minutes for last folder backups to keep before removal. Defaults to 1440.
BACKUP_LATEST_TYPE Type of latest pointer (symlink,hardlink,none). Defaults to symlink.
HEALTHCHECK_PORT Port listening for cron-schedule health check. Defaults to 8080.
POSTGRES_DB Comma or space separated list of postgres databases to backup. If POSTGRES_CLUSTER is set this refers to the database to connect to for dumping global objects and discovering what other databases should be dumped (typically is either postgres or template1). Required.
POSTGRES_DB_FILE Alternative to POSTGRES_DB, but with one database per line, for usage with docker secrets.
POSTGRES_EXTRA_OPTS Additional options for pg_dump (or pg_dumpall options if POSTGRES_CLUSTER is set). Defaults to -Z1.
POSTGRES_CLUSTER Set to TRUE in order to use pg_dumpall instead. Also set POSTGRES_EXTRA_OPTS to any value or empty since the default value is not compatible with pg_dumpall.
POSTGRES_HOST Postgres connection parameter; postgres host to connect to. Required.
POSTGRES_PASSWORD Postgres connection parameter; postgres password to connect with. Required.
POSTGRES_PASSWORD_FILE Alternative to POSTGRES_PASSWORD, for usage with docker secrets.
POSTGRES_PASSFILE_STORE Alternative to POSTGRES_PASSWORD in passfile format, for usage with postgres clusters.
POSTGRES_PORT Postgres connection parameter; postgres port to connect to. Defaults to 5432.
POSTGRES_USER Postgres connection parameter; postgres user to connect with. Required.
POSTGRES_USER_FILE Alternative to POSTGRES_USER, for usage with docker secrets.
SCHEDULE Cron-schedule specifying the interval between postgres backups. Defaults to @daily.
TZ POSIX TZ variable specifying the timezone used to evaluate SCHEDULE cron (example "Europe/Paris").
WEBHOOK_URL URL to be called after an error or after a successful backup (POST with a JSON payload, check hooks/00-webhook file for more info). Default disabled.
WEBHOOK_ERROR_URL URL to be called in case backup fails. Default disabled.
WEBHOOK_PRE_BACKUP_URL URL to be called when backup starts. Default disabled.
WEBHOOK_POST_BACKUP_URL URL to be called when backup completes successfully. Default disabled.
WEBHOOK_EXTRA_ARGS Extra arguments for the curl execution in the webhook (check hooks/00-webhook file for more info).

Special Environment Variables

This variables are not intended to be used for normal deployment operations:

env variable description
POSTGRES_PORT_5432_TCP_ADDR Sets the POSTGRES_HOST when the latter is not set.
POSTGRES_PORT_5432_TCP_PORT Sets POSTGRES_PORT when POSTGRES_HOST is not set.

How the backups folder works?

First a new backup is created in the last folder with the full time.

Once this backup finish succefully then, it is hard linked (instead of coping to avoid use more space) to the rest of the folders (daily, weekly and monthly). This step replaces the old backups for that category storing always only the latest for each category (so the monthly backup for a month is always storing the latest for that month and not the first).

So the backup folder are structured as follows:

  • BACKUP_DIR/last/DB-YYYYMMDD-HHmmss.sql.gz: all the backups are stored separatly in this folder.
  • BACKUP_DIR/daily/DB-YYYYMMDD.sql.gz: always store (hard link) the latest backup of that day.
  • BACKUP_DIR/weekly/DB-YYYYww.sql.gz: always store (hard link) the latest backup of that week (the last day of the week will be Sunday as it uses ISO week numbers).
  • BACKUP_DIR/monthly/DB-YYYYMM.sql.gz: always store (hard link) the latest backup of that month (normally the ~31st).

And the following symlinks are also updated after each successfull backup for simlicity:

BACKUP_DIR/last/DB-latest.sql.gz -> BACKUP_DIR/last/DB-YYYYMMDD-HHmmss.sql.gz
BACKUP_DIR/daily/DB-latest.sql.gz -> BACKUP_DIR/daily/DB-YYYYMMDD.sql.gz
BACKUP_DIR/weekly/DB-latest.sql.gz -> BACKUP_DIR/weekly/DB-YYYYww.sql.gz
BACKUP_DIR/monthly/DB-latest.sql.gz -> BACKUP_DIR/monthly/DB-YYYYMM.sql.gz

For cleaning the script removes the files for each category only if the new backup has been successfull. To do so it is using the following independent variables:

  • BACKUP_KEEP_MINS: will remove files from the last folder that are older than its value in minutes after a new successfull backup without affecting the rest of the backups (because they are hard links).
  • BACKUP_KEEP_DAYS: will remove files from the daily folder that are older than its value in days after a new successfull backup.
  • BACKUP_KEEP_WEEKS: will remove files from the weekly folder that are older than its value in weeks after a new successfull backup (remember that it starts counting from the end of each week not the beggining).
  • BACKUP_KEEP_MONTHS: will remove files from the monthly folder that are older than its value in months (of 31 days) after a new successfull backup (remember that it starts counting from the end of each month not the beggining).

Hooks

The folder hooks inside the container can contain hooks/scripts to be run in differrent cases getting the exact situation as a first argument (error, pre-backup or post-backup).

Just create an script in that folder with execution permission so that run-parts can execute it on each state change.

Please, as an example take a look in the script already present there that implements the WEBHOOK_URL functionality.

Manual Backups

By default this container makes daily backups, but you can start a manual backup by running /backup.sh.

This script as example creates one backup as the running user and saves it the working folder.

docker run --rm -v "$PWD:/backups" -u "$(id -u):$(id -g)" -e POSTGRES_HOST=postgres -e POSTGRES_DB=dbname -e POSTGRES_USER=user -e POSTGRES_PASSWORD=password  prodrigestivill/postgres-backup-local /backup.sh

Automatic Periodic Backups

You can change the SCHEDULE environment variable in -e SCHEDULE="@daily" to alter the default frequency. Default is daily.

More information about the scheduling can be found here.

Folders daily, weekly and monthly are created and populated using hard links to save disk space.

Restore examples

Some examples to restore/apply the backups.

Restore using the same container

To restore using the same backup container, replace $BACKUPFILE, $CONTAINER, $USERNAME and $DBNAME from the following command:

docker exec --tty --interactive $CONTAINER /bin/sh -c "zcat $BACKUPFILE | psql --username=$USERNAME --dbname=$DBNAME -W"

Restore using a new container

Replace $BACKUPFILE, $VERSION, $HOSTNAME, $PORT, $USERNAME and $DBNAME from the following command:

docker run --rm --tty --interactive -v $BACKUPFILE:/tmp/backupfile.sql.gz postgres:$VERSION /bin/sh -c "zcat /tmp/backupfile.sql.gz | psql --host=$HOSTNAME --port=$PORT --username=$USERNAME --dbname=$DBNAME -W"

docker-postgres-backup-local's People

Contributors

capnis avatar cdiarra-ledger avatar dargmuesli avatar jokay avatar kolahzary avatar pedrocapdevila avatar prodrigestivill avatar rbi avatar sberlin avatar slhck avatar thinkscape avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

docker-postgres-backup-local's Issues

Support intraday backups?

Will the current script support intraday backups? I was hoping to run every 6 hours giving me 4 per day but wanted to check first.

I can't quite see whether the daily clean up would remove some files but I'm assuming not given there is an HH - mm timestamp on files created on the day

Change time zone

Hi,

Is it possible to change the time zone for this container?

Thanks!

The backup.sh file is not execute

I'm trying execute backup.sh file, but the console return
./backup.sh: 67: ./backup.sh: Syntax error: end of file unexpected (expecting "fi")
I'm read the file and the sentence "else if " is incorrect.

Changelog

Hey there,
is there any changelog to check if upgrading to a new major version introduces breaking changes?
I'd really like to know that to make sure my backups won't break.
If there isn't could there be a changelog added? For example by tagging releases in Git and writing about included changes in GitHub releases.
Thank you, have a great day!

Access right issue

I have been using your container for several months but recently I began having this kind of errors:

- 2021/04/28 08:37:03 3539 cmd: /backup.sh
- 2021/04/28 08:37:03 3539: Creating dump of strapi database from 192.168.1.40...
- 2021/04/28 08:37:03 3539: pg_dump: error: query failed: ERROR:  permission denied for table core_store
pg_dump: error: query was: LOCK TABLE public.core_store IN ACCESS SHARE MODE
- 2021/04/28 08:37:03 3539 Exit Status: 1

I checked the credentials to the database and the rest of the docker-compose.yml file and everything seems fine.
I guess this errors is not strictly linked to the container but can you direct me to a solution?
Best,
Stephen

Question about "/var/lib/postgresql/data" volume

Hi, have a little question.

I have actually this compose file:

  pgbackups:
    image: prodrigestivill/postgres-backup-local
    container_name: paperless-ng_db_bak
    restart: unless-stopped
    user: 1000:100
    volumes:
      - /srv/dev-disk-by-label-HC2/AppData/paperless-ng_db_bak/backups:/backups
      - pgbackups_data:/var/lib/postgresql/data
    depends_on:
      - db
    environment:
      - TZ=Europe/Rome
      - POSTGRES_CLUSTER=TRUE
      - POSTGRES_HOST=paperless-ng_db
      - POSTGRES_DB=xxx
      - POSTGRES_USER=xxx
      - POSTGRES_PASSWORD=xxx
      - POSTGRES_EXTRA_OPTS=
      - SCHEDULE=@midnight
      - BACKUP_KEEP_DAYS=7
      - BACKUP_KEEP_WEEKS=0
      - BACKUP_KEEP_MONTHS=0

I checked performing a manually backup and it works.

I just don't understand why a new volume is created with the container, pointing to /var/lib/postgresql/data. In my compose file i called the volume pgbackups_data just to recognize it.

Thank you in advance!

curl is not installed

Hey everybody,

it seems like the 11-alpine image is always unhealthy.

When I do docker exec -it my_postgres_backup_local /bin/sh and enter curl, it says that curl was not found.

That might be the reason why the healthcheck also does not work.

Backup the entire database cluster

Hi all,

It would be nice if you could add an option to backup the entire database cluster, so you could get all the roles and permissions in the backup as well.

Something like this:

pg_dumpall -v -h <db.host.address> -p <db.host.port> -U <db.user> -f "<path/to/the/backup/file.sql>"

Cheers,

Sam

Hint on how to restore

I'm really embarrassed to ask, but I inherited a project using your image and I wonder on how to restore a backup.
It's a totally noob question and I'm sorry, but I don't have much experience on how to service a postgres database and perhaps you have a hint for me.

I tried the accepted answer https://stackoverflow.com/a/29913462/4634044 but with no luck.

cat backup.sql | docker exec -i image psql --username=myusername

failes with
psql: FATAL: database "myusername" does not exist

Perhaps you could share how you restore your backups?

Postgres 12 should be included

As I have seen in the file "retag.sh", postgres 12 is not yet included. I get a version sensitive error when using this image on containers with postgres 12.

Missing DB File

Hi,

Seems that latest updates has caused an issue of executing the backup.sh script. The script seems to throw an error of missing DB file although the POSTGRES_DB_FILE variable is not used and the POSTGRES_DB is used instead. Reverting to older version from two weeks ago seems to work fine.

Compress directory backups as a single file

I leave an example of a small DB:

image

The last backup is with the new configuration and, as can be seen, it increases the size by 166%.

image

Then if I compress that directory. The size happens to be similar to the previous ones (which is cheaper for me to store in the cloud, especially when we talk about GBs of storage and multiple DBs).

tar -zcf ****-20200904-173736-dump.tar.gz ***-20200904-173736-dump
2.4M    ***-20200904-041500.sql.gz
6.4M    ***-20200904-173736-dump
2.6M    ***-20200904-173736-dump.tar.gz

Besides being cheaper in my opinion, it is also easier to manipulate a compressed file than a folder, don't you consider the same?

It is only a suggestion and an exchange of opinion. I can solve it by compressing that directory myself, but I was interested in sharing my point of view with you since this project is looking very good. Let me know if you think the same as me!

Originally posted by @midestefanis in #28 (comment)

README.md unclear on what environment variables do

Description

The README doesn't mention what environment variables are used for. I looked through the bash script, and it appears these are three directories which are populated with hardlinks to the database backups, so that days, weeks and months can be pruned properly.

I didn't understand how hardlinks worked until I read this source code, so it may be worth including a section in the README which goes over all of the environment variables flags so others do not need to read the script in order to understand how it works. I've made an example table below.

Environment Variables

Since there is already an example yaml, I have left out example values.

env variable description
POSTGRES_HOST postgres connection parameter; postgres host
POSTGRES_DB postgres connection parameter; postgres database to connect to
POSTGRES_USER postgres connection parameter; postgres user to connect with
POSTGRES_PASSWORD postgres connection parameter; postgres password to connect with
POSTGRES_PASSWORD_FILE alternative to POSTGRES_PASSWORD, to use with docker secrets
POSTGRES_EXTRA_OPTS additional options to supply pg_dump when creating back-ups
SCHEDULE cron-schedule specifying the interval between taking postgres backups
BACKUP_KEEP_DAYS number of days to keep backups before removing
BACKUP_KEEP_WEEKS number of weeks to keep backups before removing
BACKUP_KEEP_MONTHS number of months to keep backups before removing
HEALTHCHECK_PORT I don't know how this is used, but I am assuming this provides a health-check endpoint

Reconsider use of gzip -9

pg_dumpall -l "${DB}" ${POSTGRES_EXTRA_OPTS} | gzip -9 > "${DFILE}"

In my tests, gzip -9 results in around a 7% saving in file size, but almost 3x cost in execution time.

Ideally it would be configurable.

no compression:
real    7m7.690s
user    0m14.668s
sys     1m3.074s

gzip (default):
real    24m0.328s
user    23m14.971s
sys     1m13.967s

gzip -9:
real    70m26.249s
user    70m6.240s
sys     1m26.190s

-rw-r--r--. 1 root root 43153842570 Feb 18 01:41 dump.sql
-rw-r--r--. 1 root root  5249807591 Feb 18 02:05 dump.sql.default.gz
-rw-r--r--. 1 root root  4933680154 Feb 18 03:16 dump.sql.minus9.gz

mkdir: cannot create directory ‘/backups/daily/ even running mkdir and chown

Hi, I followed the readme and tried to run commands to create the directory to store the backups with this:

# for default images (debian)
mkdir -p /var/opt/pgbackups && chown -R 999:999 /var/opt/pgbackups
# for alpine images
mkdir -p /var/opt/pgbackups && chown -R 70:70 /var/opt/pgbackups

When I run the commands I neet to do it with sudo to make it work, because if don't it throws this error: chown: changing ownership of '/var/opt/pgbackups': Operation not permitted and mkdir: cannot create directory ‘/var/opt/pgbackups’: Permission denied

I don't have postgresql on host, only on container but even running those commands it keeps saying :

2021/07/22 03:39:23 443 cmd: /backup.sh
2021/07/22 03:39:23 443: mkdir: cannot create directory ‘/backups/daily/’
2021/07/22 03:39:23 443: : Permission denied
mkdir: cannot create directory ‘/backups/weekly/’: Permission denied
2021/07/22 03:39:23 443: mkdir: cannot create directory ‘/backups/monthly/’: Permission denied
2021/07/22 03:39:23 443 Exit Status: 1

I'm using a VPS with ubuntu 20.04, Docker version 20.10.7 build f0df350, docker-compose version 1.25.0

I also ran docker-compose exec db /bin/bash and inside it I got uid 70 and gid 70 for postgres user/group but even assignin those uid and gid keeps throwing permission denied. I'm missing a step?

Running ls on /var/opt/pgbackups:

drwxr-xr-x 2 70 70 4096 Jul 22 14:41 pgbackups

Running id postgres on docker-compose -f docker-compose.prod.yml exec db /bin/bash:

uid=70(postgres) gid=70(postgres) groups=70(postgres),70(postgres)

Restore: the input is not a TTY

When trying to restore a backup to a remote server:
zcat backupfile.sql.gz | docker run --rm --tty --interactive postgres:$VERSION psql --host=$HOSTNAME --port=$PORT --username=$USERNAME --dbname=$DBNAME -W

When replacing all the variables, I get the following error:
the input is not a TTY

I cannot restore my backup using the command you provided. I am using ubuntu 20.10 with docker version 20.10.2.
Is there something that I am doing wrong ?

Support backups volume mapped to an S3 bucket

Thanks for a great tool.

I'm using it with a volume mapped to an S3 bucket. Because of this the hard linking in the script fails.

I could probably make a PR if its welcome to optionally use file copy instead of linking?

Using only one service for backup and database?

Hi,

I have seen that the Docker image is based on Postgres image.

Would it be possible to use this image directly as a database service and thus avoiding using 2 services on low RAM systems?

I don't know if this relevant however.

Regards

How to limit to daily and weekly backups

Hi and sorry for the noob question.

I wanted to keep only daily and weekly backups and disable monthly backups (to save a bit of HD space) but even if you do BACKUP_KEEP_MONTHS=0 it still does a monthly backup....

So, it´s there a way to disable monthly backups or the minimum allowed is to have at least one daily, weekly and monthly backup?

Cheers

container cannot create pgbackup

pgbackup | 2019/08/08 13:42:35 Opening port 80 for health checking
pgbackup | 2019/08/08 13:45:35 11 cmd: /backup.sh
pgbackup | 2019/08/08 13:45:35 11: Missing POSTGRES_DB_FILE file.
pgbackup | 2019/08/08 13:45:35 11 Exit Status: 1

Run as Non Root

Is is possible to run the container as a Non Root user? Thanks!

latest.sql.gz

Hello,

Thanks for the great job!
This is a nice similar docker-mysql-cron-backup script but for postgresql.

Should be great to have a symlink to the latest backup so that it's easier to automate restore process.

Exemple:

/backups/
├── latest.db_intranet.sql.gz -> daily/db_intranet-20210813-133946.sql.gz
├── daily
│   ├── db_intranet-20210813-133716.sql.gz
│   ├── db_intranet-20210813-133746.sql.gz
│   ├── db_intranet-20210813-133816.sql.gz
│   ├── db_intranet-20210813-133846.sql.gz
│   ├── db_intranet-20210813-133916.sql.gz
│   └── db_intranet-20210813-133946.sql.gz
├── monthly
│   └── db_intranet-202108.sql.gz
└── weekly
    └── db_intranet-202132.sql.gz

Problem with cluster feature

We are trying to setup periodic backup with the following configuration

environment:
    - POSTGRES_HOST=postgres
    - POSTGRES_PORT=5432
    - POSTGRES_DB=postgres://postgres:5432 (pg_dumpall needs connection string)
    - POSTGRES_USER=postgres
    - POSTGRES_PASSWORD=postgres
    - POSTGRES_EXTRA_OPTS=
    - POSTGRES_CLUSTER=TRUE
    - SCHEDULE=@every 5m
    - BACKUP_KEEP_DAYS=7
    - BACKUP_KEEP_WEEKS=4
    - BACKUP_KEEP_MONTHS=6
    - BACKUP_SUFFIX=.sql

DB server is PostgreSQL 9.5

root@a6fe2d177a0b:/# postgres -V
postgres (PostgreSQL) 9.5.23

But, whenever backup runs the following error is reported:

postgres-backup    | 2020/09/01 07:25:40 14: Creating pg_dumpall of postgres://postgres:5432 database from postgres...
postgres-backup    | 2020/09/01 07:25:40 14: pg_dumpall: could not open the output file "/backups/daily/postgres://postgres:5432-20200901-072540.sql": No such file or directory

Am I missing something?

Question: What is the POSTGRES_HOST

Im new to docker and databases and im quite confused what the POSTGRES_HOST value should be. I kept getting hung on listening for post 8080 and I think this is issue because it is not able to connect. Thank you for any help!

Error: aborting because of server version mismatch

I would like to use the 'latest' postgres docker image for my application. At the time of this post it is postgres version '14.0'. Backups are not successful and my 'postgres-backup' shows this error:

2021/10/24 22:58:28 13 Exit Status: 1
2021/10/24 22:58:48 24 cmd: /backup.sh 
2021/10/24 22:58:48 24: Creating dump of kylin_parachain database from db...
2021/10/24 22:58:48 24: pg_dump: error: server version: 14.0 (Debian 14.0-1.pgdg110+1); pg_dump version: 13.2 (Debian 13.2-1.pgdg100+1)
pg_dump: error: aborting because of server version mismatch
2021/10/24 22:58:48 24 Exit Status: 1

When I use the '13.2' postgres image, everything works great. Is it possible to use the 'latest' postgres version in my compose file or do I need to pick one postgres version and stick with it? Here is my compose file.

  postgres:
    environment: 
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: kylin_parachain
    image: postgres:latest
    ports:
      - 5433:5432
    restart: always
    container_name: postgres
    volumes:
      - ./postgres-data:/var/lib/postgresql/data
      - ./schemas.sql:/docker-entrypoint-initdb.d/schemas.sql
  postgres-backups:
    image: prodrigestivill/postgres-backup-local
    depends_on:
      - postgres
    restart: always
    user: postgres:postgres
    container_name: postgres-backups
    environment:
      POSTGRES_HOST: postgres
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: kylin_parachain
      SCHEDULE: "@every 20s"
      BACKUP_KEEP_DAYS: 7
      BACKUP_KEEP_WEEKS: 4
      BACKUP_KEEP_MONTHS: 6
      HEALTHCHECK_PORT: 8080
    volumes:
      - ./postgres-backups:/backups

Name clash for environment variables

The used environment variables, mostly prefixed with POSTGRES_ lead to a collision in my setup. I used your image in OpenShift to back up an instance (a.k.a. deployment) named 'postgres'. OpenShift uses the deployment name to inject environment variables similar to the one already read by the script: POSTGRES_PORT_5432_TCP_ADDR. This lead to the variable POSTGRES_PORT being set to tcp://x.x.x.x:5432 by OpenShift as soon as I configured the custom variable POSTGRES_HOST. This can be fixed with an additional switch checking the environment variables, but I think the underlying issue is the missing global namespace for custom environment variables. Do you agree or do you like to avoid breaking changes altogether? I won't make an PR with the switch as this is really an edge case, an error in usage even so. Instead the variable POSTGRES_PORT_5432_TCP_ADDR should be documented, I think. Effectively the implementation forces OpenShift users having a deployment named 'postgres' to unset POSTGRES_HOST.

ARM: Hanging on healthcheck port open

Hi, I had this container running for a few days, but noticed that it never made any backups until I manually ran backup.sh. It seems that my container is hanging during startup when it tries to open up the healthcheck port. I've tried different ports, but with the same output. I get this when I log into the running container and execute the go-cron job:

bash-4.4# /usr/local/bin/go-cron -s $SCHEDULE -p $HEALTHCHECK_PORT -- /backup.sh
2019/03/29 10:00:20 Running version: 
2019/03/29 10:00:20 new cron: "@daily"
2019/03/29 10:00:20 Opening port 7654 for health checking

The healtcheck returns the following:

bash-4.4# curl -f "http://localhost:$HEALTHCHECK_PORT/"
{
  "Running": {},
  "Last": {
  "Exit_status": 0,
  "Stdout": "",
  "Stderr": "",
  "ExitTime": "",
  "Pid": 0,
  "StartingTime": ""
},
"Schedule": "@daily"

My crontab -l returns

bash-4.4# crontab -l
# do daily/weekly/monthly maintenance
# min	hour	day	month	weekday	command
*/15	*	*	*	*	run-parts /etc/periodic/15min
0	*	*	*	*	run-parts /etc/periodic/hourly
0	2	*	*	*	run-parts /etc/periodic/daily
0	3	*	*	6	run-parts /etc/periodic/weekly
0	5	1	*	*	run-parts /etc/periodic/monthly

I'm running on an ARM server so I rebuilt the image myself, so I don't know if that maybe makes a difference. I've also tried a version of go-cron that doesn't use a healtcheck port, but still no good. Do you have any advice or idea for how I can fix this and get it running?

Add Support for s390x and ppc64le

It looks like to generate the Dockeriles and manifests, you are using buildx. I see currently support for a couple architectures, but would you be willing to add s390x and ppc64le support?

Looks like the defaults/values would need to be changed in a couple places:

PLATFORMS=${PLATFORMS:-"linux/amd64 linux/arm64 linux/arm/v7"}

platforms = ["linux/amd64", "linux/arm64", "linux/arm/v7"]

Plus the readme!

To enable support it should be linux/s390x and linux/ppc64le.

@prodrigestivill Would you be open to build for those platforms as well? If so, I can open a PR.

About -user

Hi, would like to ask clarification about:

For security reasons it is recommended to run it as user postgres:postgres.

In case of running as postgres user, the system administrator must initialize the permission of the destination folder as follows:

# for default images (debian)
mkdir -p /var/opt/pgbackups && chown -R 999:999 /var/opt/pgbackups
# for alpine images
mkdir -p /var/opt/pgbackups && chown -R 70:70 /var/opt/pgbackups

Why i need to run with postgres user?

Thanks in advance!

Failed to create hard link

I get the following error message in the logs when running in Unraid:

2021/05/11 00:00:06 916: ln: failed to create hard link '/backups/weekly/nextcloud-202119.sql.gz' => '/backups/daily/nextcloud-20210511-000000.sql.gz': Function not implemented

I'm trying to backup multiple databases (names are comma separated), and it errors out after the first backup attempt. So it looks like I get a daily backup of the first db in the list, but it can't create the link in the weekly folder.

Backup ALL databases - POSTGRES_CLUSTER doesn't...

Hello from germany.

I use a postgres docker container for the online banking databases for the german banking software VRNWS.
So this container runs the main postgres database with the current data, but it also runs multiple archive-databases for each past year.
So there are about 14 small databases at the moment, and when another year passes there possibly will be a new archive database for 2022, too.

So i want to use the pg_dumpall function to simply backup every possible database from this specific postgres host and set POSTGRES_CLUSTER=TRUE . First i tried without POSTGRES_DB at all because i don't want to manually add each database name. - But that did't work. - After that i put in an empty POSTGRES_DB= setting in the YAML-file, which also didn't work. - I tried than to only add the maintenance DB name to POSTGRES_DB=postgres but keeping POSTGRES_CLUSTER=TRUE enabled, but now only the postgres database was backed up. - Originally i thought the POSTGRES_CLUSTER function will backup all databases without the need to name them all in first place !?

Any ideas what i am doing wrong ?

Container stopped (exit 0) with NO reason

I am running this as service with restart policy "on-failure", at the begging the service is reported as running (Replicas 1/1) after a while the service is reported as not running (Replicas 0/1).

The "docker stack ps app" is reporting the state "Shutdown"

# docker stack ps app                                                                   
ID             NAME              IMAGE                                       DESIRED STATE  CURRENT STATE                ERROR                              PORTS
v86mt8uk5og7   app_db-backup.1  prodrigestivill/postgres-backup-local:10     Shutdown       Complete 6 hours ago

The docker ps -a is reporting the state "Exited (0) 6 hours ago"

# docker ps -a
CONTAINER ID        IMAGE                                       COMMAND                  CREATED         STATUS                   PORTS       NAMES
32dfc96fbb2a        prodrigestivill/postgres-backup-local:10    "/bin/sh -c 'exec /u…"   7 hours ago     Exited (0) 6 hours ago               app_db-backup.1.v86mt8uk5og7x7su91gshyg7q

Following are the logs of the container

[root@GDN-S-GNSS2 ~]# docker container logs 32dfc96fbb2a
2019/07/01 06:39:37 Running version: 6f160c2
2019/07/01 06:39:37 new cron: 0 0 0 * * *
2019/07/01 06:39:37 Opening port 8080 for health checking
(0x7f8a23c161d8,0xf)
2019/07/01 06:54:59 Stopping
2019/07/01 06:54:59 Waiting
2019/07/01 06:54:59 Exiting

Some General Questions

Hey there,

I'm wondering what the POSTGRES_PORT_5432_TCP_ADDR variable means. It's undocumented in this repository.

if [ -n "${POSTGRES_PORT_5432_TCP_ADDR}" ]; then
POSTGRES_HOST=$POSTGRES_PORT_5432_TCP_ADDR
POSTGRES_PORT=$POSTGRES_PORT_5432_TCP_PORT

Also one original Postgres variable (PGPASSWORD) is set:

export PGPASSWORD=$POSTGRES_PASSWORD

why not PGHOST, PGPORT, PGUSER, ...? :)

Secure Backup with a password

It is possible to secure the created "gz" file with a password, so it can be transferred to a cloud backup server or kept in another place?

Build Postgres 14 with support for other platforms

Add support for Postgres 14 for all architectures.
This has been moved from a @dargmuesli PR #61.

The actual situacion is that my buildx setup and crosscompilers are currently not working.
With my current free time it will take some days to setup everything correctly again.

Meanwhile I compiled manually the following tags just for amd64 architecture:

  • prodrigestivill/postgres-backup-local:14
  • prodrigestivill/postgres-backup-local:14-debian
  • prodrigestivill/postgres-backup-local:14-debian-a2927fe
  • prodrigestivill/postgres-backup-local:14-alpine
  • prodrigestivill/postgres-backup-local:14-alpine-a2927fe

Does not recognize the name of the docker service in the env

Hello, thank you for all you are making for all with this docker image.
But I have some problem with my docker-compose environnement variable, it Does not recognize the name of the docker service in the env

      auth-db:
        image: postgres:12.0-alpine
        restart: always
        container_name: evoire-auth-db
        networks:
          - evoire-auth-net
        volumes:
          - auth_user_data:/var/lib/postgresql/data/
        env_file:
          - ./.env

      auth-pgbackups:
        container_name: Backup
        image: prodrigestivill/postgres-backup-local
        restart: always
        volumes:
          - ./backup:/backups
        links:
          - auth-db
        depends_on:
          - auth-db
        environment:
          - POSTGRES_HOST=auth-db
          - POSTGRES_DB=${POSTGRES_DB} 
          - POSTGRES_USER=${POSTGRES_USER}
          - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
          - POSTGRES_EXTRA_OPTS=-Z9 --schema=public --blobs
          - SCHEDULE=*/5 * * * *
          - BACKUP_KEEP_DAYS=7
          - BACKUP_KEEP_WEEKS=4
          - BACKUP_KEEP_MONTHS=6
          - HEALTHCHECK_PORT=81

Restoring locally -> input device is "not a TTY"

I am running a PostgreSQL container with the name Synapse_Database. It has a database called synapse which I reach as user synapse. Upon running the command below, the program exits immediately.

zcat local-backup.sql.gz | docker exec --tty --interactive Synapse_Database_Backup psql --username=synapse --dbname=synapse -W

Instead of seemingly doing anything, it only outputs the input device is not a TTY.

I'm running the command on WSL 2 if it matters. Is this a bug or am I doing something wrong?

Empty dumps

Hi. This might be a missconfiguration, probably something I did wrong, but I don't really know what else to try and the logs from pg_backup suggest that everything is fine while the dumps are empty. Any help would be appreaciated.

The dumps:


--
-- PostgreSQL database dump
--

-- Dumped from database version 12.2
-- Dumped by pg_dump version 13.0 (Debian 13.0-1.pgdg100+1)

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

--
-- Name: public; Type: SCHEMA; Schema: -; Owner: myuser
--

CREATE SCHEMA public;


ALTER SCHEMA public OWNER TO myuser;

--
-- Name: SCHEMA public; Type: COMMENT; Schema: -; Owner: myuser
--

COMMENT ON SCHEMA public IS 'standard public schema';


--
-- PostgreSQL database dump complete
--

The pg_dumper says:

pg_backup_container | 2020/11/11 11:45:56 61: SQL backup created successfully
pg_backup_container | 2020/11/11 11:46:56 77 cmd: /backup.sh
pg_backup_container | 2020/11/11 11:46:56 77: Creating dump of mydbname database from db...
pg_backup_container | 2020/11/11 11:46:56 77: '/backups/weekly/mydbname-202046.sql.gz' => '/backups/daily/feedfeedbackdb-20201111-114656.sql.gz'
pg_backup_container | 2020/11/11 11:46:56 77: '/backups/monthly/mydbname-202011.sql.gz' => '/backups/daily/feedfeedbackdb-20201111-114656.sql.gz'
pg_backup_container | 2020/11/11 11:46:56 77: Cleaning older than 7 days for mydbname database from db...
pg_backup_container | 2020/11/11 11:46:56 77: SQL backup created successfully

The database actually has data, it is supporting a website that is in use.

Here's the docker-compose:

  db:
    image: postgres:12-alpine
    container_name: postgres_container_feedfeedback
    hostname: postgres
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    environment:
      - POSTGRES_DB=mydbname
      - POSTGRES_USER=myuser
      - POSTGRES_PASSWORD=mypw123
    ports:
      - "5432:5432"
    networks:
      - postgres_network

  pgbackups:
    # https://github.com/prodrigestivill/docker-postgres-backup-local
    container_name: pg_backup_container
    image: prodrigestivill/postgres-backup-local
    restart: always
    volumes:
      - ./pg_backup:/backups
    depends_on:
      - db
    environment:
      - POSTGRES_HOST=db
      - POSTGRES_DB=mydbname
      - POSTGRES_USER=myuser
      - POSTGRES_PASSWORD=mypw123
      - POSTGRES_EXTRA_OPTS=-Z9 --schema=public --blobs
      - SCHEDULE=@every 0h1m00s  # @daily  # @every 0h30m00s
      - BACKUP_KEEP_DAYS=7
      - BACKUP_KEEP_WEEKS=4
      - BACKUP_KEEP_MONTHS=6
      - HEALTHCHECK_PORT=81
      - POSTGRES_PORT=5432  # must match the db port
    networks:
      - postgres_network

networks:
  postgres_network:
    driver: bridge

Only keep backup if its different than the last one

I'd like to have an option to check the last backup and if its the same, then discard the current one. It saves a bit of space, and it will be easier to go trough them in case you need to restore. Just an idea, mainly useful for frequent small backups I think. Using a hash this shouldn't be too slow.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.