Docker container with pg_backup_rotated.sh script from https://wiki.postgresql.org/wiki/Automated_Backup_on_Linux This image runs pg_backup_rotated.sh daily via cron at configurable time. pg_backup_rotated.sh produces backups of various configurable formats and rotates weekly and daily backups.
Docker build variables:
POSTGRES_IMAGE_VERSION
- Postgres docker image tag this image will be based on. ('alpine' by default)
Docker environment variables:
POSTGRES_HOSTNAME
- ip/hostname of postgres serverPOSTGRES_USER
- user to connect as ('postgres' by default)POSTGRES_PASSWORD
- password of POSTGRES_USERPOSTGRES_PASSWORD_FILE
- location of file containing password of POSTGRES_USER (can be /run/secrets/SECRETNAME if you put password in SECRETNAME)CRON_RUN_MINUTE
, CRON_RUN_HOUR - run backups daily at this time every dayWEEKS_TO_KEEP
- how many weeks to keep weekly backups (default 5)DAYS_TO_KEEP
- number of days to keep daily backups (default 7)DAY_OF_WEEK_TO_KEEP
- which day to take the weekly backup from (1-7 = Monday-Sunday, default 5-Friday)ENABLE_GLOBALS_BACKUPS
- will produce gzipped sql file containing the cluster globals, like users and passwords, if set to "yes" (default)ENABLE_PLAIN_BACKUPS
- will produce a gzipped plain-format backup if set to "yes" (default)ENABLE_CUSTOM_BACKUPS
- will produce a custom-format backup if set to "yes" (default)SCHEMA_ONLY_LIST
- List of strings to match against in database name, separated by space or comma, for which we only wish to keep a backup of the schema, not the data. Any database names which contain any of these values will be considered candidates. (e.g. "system_log" will match "dev_system_log_2010-01"). Default is empty list.
Volumes:
- /backups - backups go there
Optional volumes:
- /etc/localtime - map your host /etc/localtime there read-only to have cron operate in host's timezone
You can also tmpfs /var/lib/postgresql/data, it is exported by base image but is not used in this image
Test:
A docker-compose-example.yml
is provided to help with testing. Check it for further details.
You can quickly try something like:
$ docker-compose -f docker-compose-example.yml up -d pg_backup_test
$ docker exec -it docker-pg_backup-pg_backup_test-1 bash
bash-5.1# crontab -l
10 3 * * * /pgpass.sh /pg_backup_rotated.sh
bash-5.1# /pgpass.sh /pg_backup_rotated.sh
Making backup directory in /backups/2022-03-24-daily/
Performing globals backup
--------------------------------------------
....
bash-5.1# gzip -cd /backups/2022-03-24-daily/postgres.sql.gz
...
The backup script code is taken as-is from postgress wiki, I am not sure which license is it, I claim no rights to this code. Configuration script is based on config file from postgres wiki. The code that is written by me is in the public domain (CC0).