• Stars
    star
    450
  • Rank 97,143 (Top 2 %)
  • Language
    Shell
  • License
    GNU General Publi...
  • Created over 9 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A cron job that will back up databases running in a docker postgres container

Table of Contents

Docker PG Backup

A simple docker container that runs PostgreSQL / PostGIS backups (PostGIS is not required it will backup any PG database). It is primarily intended to be used with our docker postgis docker image. By default, it will create a backup once per night (at 23h00)in a nicely ordered directory by a year / month.

Getting the image

There are various ways to get the image onto your system:

The preferred way (but using most bandwidth for the initial image) is to get our docker trusted build like this:

docker pull kartoza/pg-backup:$POSTGRES_MAJOR_VERSION-$POSTGIS_MAJOR_VERSION.${POSTGIS_MINOR_RELEASE}

Where the environment variables are

POSTGRES_MAJOR_VERSION=13
POSTGIS_MAJOR_VERSION=3
POSTGIS_MINOR_RELEASE=1 

We highly suggest that you use a tagged image that match the PostgreSQL image you are running i.e (kartoza/pg-backup:15-3.3 for backing up kartoza/postgis:15-3.3 DB). The latest tag may change and may not successfully back up your database.

To build the image yourself do:

git clone https://github.com/kartoza/docker-pg-backup.git
cd docker-pg-backup
./build.sh # It will build the latest version corresponding the latest PostgreSQL version

Running the image

To create a running container do:

POSTGRES_MAJOR_VERSION=15
POSTGIS_MAJOR_VERSION=3
POSTGIS_MINOR_RELEASE=3 
docker run --name "db"  -p 25432:5432 -d -t kartoza/postgis:$POSTGRES_MAJOR_VERSION-$POSTGIS_MAJOR_VERSION.${POSTGIS_MINOR_RELEASE}
docker run --name="backups"  --link db:db -v `pwd`/backups:/backups  -d kartoza/pg-backup:$POSTGRES_MAJOR_VERSION-$POSTGIS_MAJOR_VERSION.${POSTGIS_MINOR_RELEASE}

Specifying environment variables

You can also use the following environment variables to pass a username and password etc for the database connection.

  • POSTGRES_USER if not set, defaults to : docker
  • POSTGRES_PASS if not set, defaults to : docker
  • POSTGRES_PORT if not set, defaults to : 5432
  • POSTGRES_HOST if not set, defaults to : db
  • ARCHIVE_FILENAME you can use your specified filename format here, default to empty, which means it will use default filename format.
  • DBLIST a space-separated list of databases for backup, e.g. gis data. Default is all databases.
  • REMOVE_BEFORE remove all old backups older than specified amount of days, e.g. 30 would only keep backup files younger than 30 days. Default: no files are ever removed.
  • DUMP_ARGS The default dump arguments based on official PostgreSQL Dump options.
  • RESTORE_ARGS Additional restore commands based on official PostgreSQL restore
  • STORAGE_BACKEND The default backend is to store the backup files. It can either be FILE or S3(Example minio or amazon bucket) backends.
  • DB_TABLES A boolean variable to specify if the user wants to dump the DB as individual tables. Defaults to No
  • CRON_SCHEDULE specifies the cron schedule when the backup needs to run. Defaults to midnight daily.
  • DB_DUMP_ENCRYPTION boolean value specifying if you need the backups to be encrypted.
  • RUN_ONCE useful to run the container as a once off job and exit. Useful in Kubernetes context

Note To avoid interpolation issues with the env variable ${CRON_SCHEDULE} you will need to provide the variable as a quoted string i.e ${CRON_SCHEDULE}='*/1 * * * ' or ${CRON_SCHEDULE}="/1 * * * *"

Here is a more typical example using docker-composer:

Filename format

The default backup archive generated will be stored in the /backups directory (inside the container):

/backups/$(date +%Y)/$(date +%B)/${DUMPPREFIX}_${DB}.$(date +%d-%B-%Y).dmp

As a concrete example, with DUMPPREFIX=PG and if your postgis has DB name gis. The backup archive would be something like:

/backups/2019/February/PG_gis.13-February-2019.dmp

If you specify ARCHIVE_FILENAME instead (default value is empty). The filename will be fixed according to this prefix. Let's assume ARCHIVE_FILENAME=latest The backup archive would be something like

/backups/latest.gis.dmp

Backing up to S3 bucket

The script uses s3cmd for backing up files to S3 bucket.

  • ACCESS_KEY_ID Access key for the bucket
  • SECRET_ACCESS_KEY Secret Access key for the bucket
  • DEFAULT_REGION Defaults to 'us-west-2'
  • HOST_BASE
  • HOST_BUCKET
  • SSL_SECURE The determines if the S3 bucket is
  • BUCKET Indicates the bucket name that will be created.

You can read more about configuration options for s3cmd

For a typical usage of this look at the docker-compose-s3.yml

Mounting Configs

The image supports mounting the following configs:

  • s3cfg when backing to S3 backend
  • backup-cron for any custom configuration you need to specify in the file.

An environment variable ${EXTRA_CONFIG_DIR} controls the location of the folder.

If you need to mount s3cfg file. You can run the following:

-e ${EXTRA_CONFIG_DIR}=/settings
-v /data:/settings

Where s3cfg is located in /data

Restoring

The image provides a simple restore script. There are two ways to restore files based on the location of the backup files.

  • Restore from file based backups.
  • Restore from cloud based backups.

Restore from file based backups

You need to specify some environment variables first:

  • TARGET_DB The db name to restore
  • WITH_POSTGIS Kartoza specific, to generate POSTGIS extension along with the restore process
  • TARGET_ARCHIVE The full path of the archive to restore

Note: The restore script will try to delete the TARGET_DB if it matches an existing database, so make sure you know what you are doing. Then it will create a new one and restore the content from TARGET_ARCHIVE

It is generally a good practice to restore into an empty new database and then manually drop and rename the databases.

i.e. if your original database is named gis, you can restore it into a new database called gis_restore

If you specify these environment variables using docker-compose.yml file, then you can execute a restore process like this:

docker-compose exec dbbackups /backup-scripts/restore.sh

Restoring from S3 bucket

The script uses s3cmd for restoring files S3 bucket to a postgresql database.

To restore from S3 bucket, first you have to exec into your running container. You have to launch the /backup-scripts/restore.sh with two parameters

  • the first parameter is the target date that you want to restore: ex "2023-03-24" for the 24th March 2023.
  • the second parameter is for the database name you want your backup to be restored: ex "vaultdb"

You can read more about configuration options for s3cmd

For a typical usage of this look at the docker-compose-s3.yml

Credits

Tim Sutton ([email protected])

Admire Nyakudya ([email protected])

Rizky Maulana ([email protected])

More Repositories

1

docker-postgis

Dockerfile for postgis
Shell
648
star
2

docker-geoserver

A docker recipe for GeoServer
Shell
610
star
3

docker-osm

A docker compose project to setup an OSM PostGIS database with automatic updates from OSM periodically
C
285
star
4

docker-qgis-desktop

A docker project that will run your QGIS desktop app inside a docker container
Shell
96
star
5

docker-qgis-server

A dockerfile that contains a running QGIS server
QML
69
star
6

docker-mapproxy

A docker recipe for http://mapproxy.org/
Shell
62
star
7

QGIS-Legend-Patches

A collection of patches to use in your legends in QGIS!
Python
49
star
8

osgs

A rich, integrated, and opinionated GIS Stack with a focus on configurability and ease of use, built from Open Source Components
Makefile
40
star
9

leaflet-wms-legend

A simple leaflet WMS legend widget
JavaScript
37
star
10

docker-mapserver

Mapserver in Docker
HTML
37
star
11

prj.app

A django app for creating visual changelogs for software releases
Python
30
star
12

fbf-project

Project data and resources for WB Forecast Based Financing work
TSQL
23
star
13

docker-tilemill

A docker project for tilemill
Shell
21
star
14

docker-ssh

A simple docker container that runs ssh
Shell
20
star
15

kartoza-rancher-catalogue

This is our catalogue of Rancher recipes
19
star
16

osm-reporter

osm-reporter
JavaScript
19
star
17

awesome-geodata

A curated list of awesome geospatial data sources and services
19
star
18

maps.kartoza.com

Maps and web site for http://maps.kartoza.com
Python
18
star
19

docker-helpers

Helper commands for docker because typing long docker commands is boring...
Shell
15
star
20

django-bims

JavaScript
12
star
21

charts

Kartoza Helm charts for Kubernetes
Smarty
11
star
22

geocontext

A django app to retrieve context for spatial feature
Python
9
star
23

QGISTrainingWorkshop

This is a hands on workshop for QGIS, InaSAFE and OSM
QML
9
star
24

docker-qgis-orchestration

Orchestration scripts for QGIS docker services
9
star
25

docker-mezzanine

A docker project for mezzanine (http://mezzanine.jupo.org/)
JavaScript
9
star
26

geonode_qgis_server

Django app to make geonode work with QGIS Server
Python
9
star
27

SpeciesExplorer

A QGIS (3 or greater) plugin for quickly retrieving species occurrence data from GBIF.
Python
9
star
28

docker-django

A django ready docker container with nginx and uwsgi for serving up your site
Shell
8
star
29

docker-geogig

A docker container for using geogig (formerly known as geogit)
Shell
7
star
30

otf-project

Python
7
star
31

geosafe

InaSAFE package for Geonode
Python
7
star
32

docker-tao-web

Docker appliance for the tao examination platform (see http://taotesting.com/)
Shell
6
star
33

stream_feature_extractor

A QGIS plugin to extract stream features (wells, sinks, confluences etc.) from a stream network
QML
6
star
34

topostyle

publish and style South African 1:50000 topographical map vector data from NGI
QML
6
star
35

TheKartozaHandbook

Organisational Handbook and Technical Docs Repository for Kartoza. Here is where we highlight the procedures, principles and processes related to Development, DevOps, and GIS in line with the organisation's best practices
Nix
6
star
36

django-wms-client

A django application that provides generic WMS client support.
Python
5
star
37

docker-sftp-backup

Docker project to back up your data to an sftp server using rotating backup system
Python
5
star
38

docker-geosafe

Geosafe and GeoNode_QGIS-server deployment in docker
Python
5
star
39

flask_user_map

A simple flask for creating user community maps
JavaScript
5
star
40

IGRAC-GGIS

HTML
4
star
41

ckanext-dalrrd-emc-dcpr

Electronic Metadata Catalog for South Africa's Department of Agriculture, Land Reform and Rural Development
Python
4
star
42

QGISReportsWorkshop

Workshop presented to the Swiss QGIS User group March 2022
Python
4
star
43

SAEOSS-Portal

The SAEOSS Portal has been proposed as a system of software components functioning together as the national central earth observation geospatial repository, with a view to metadata and open geospatial API standards compliance as well as user impact maximization
CSS
4
star
44

parcel_plugin

QML
3
star
45

docker-geonode

A docker project for geonode
Python
3
star
46

docker-osm-examples

And directory for examples of docker-osm usage
QML
2
star
47

trail-mapper-ios

Mobile app for mapping trails and trail conditions
Swift
2
star
48

docker-django-base

A base docker image for django projects.
Shell
2
star
49

RandomDEMGenerator

An experiment to create random DEMS
Python
2
star
50

IGRAC-Mobile

TypeScript
2
star
51

sg-diagram-downloader

A tool for QGIS that will download SG (South African Surveyor General) diagrams
Python
2
star
52

docker-tilestream

A docker project for tilestream
Shell
2
star
53

geodata-mart

Geoprocessing as a Service
JavaScript
2
star
54

IGRAC-WellAndMonitoringDatabase

A PostgreSQL schema implementing the groundwater interchange schema defined in GWML2
JavaScript
2
star
55

django-training

Training couse materials for django
HTML
2
star
56

feti

Further Education and Training Institute Website
PLpgSQL
2
star
57

miniSASS

miniSASS website for GroundTruth
JavaScript
2
star
58

docker-tegola-maputnik

A demonstrator project showing how to run tegola vector tile server and maputnik tile style editor in docker.
Shell
2
star
59

qgis_dataset_qa_workbench

A QGIS3 plugin for assisting in dataset Quality Assurance workflows
Python
2
star
60

watchkeeper

A security alerts platform for iMMAP
Python
1
star
61

QGISCheatSheet

Our handy guide for QGIS!
1
star
62

trail-mapper-django

A django app for the Kartoza trail mapper app
CSS
1
star
63

LEDET_BIMS

Limpopo Biodiversity Information Management System
PLpgSQL
1
star
64

timesheet-project

Python
1
star
65

least-cost-electrification-somalia

Global Electrification Platform for Global
HTML
1
star
66

node-red-flows

Example flows using node red
1
star
67

Woolpert

Repo for the Woolpert project
JavaScript
1
star
68

rir-dashboard

An open platform for risk informed response planning.
JavaScript
1
star
69

geoserver-deploy

Example deployment of geoserver and postgis using docker and fig
1
star
70

kobo-docker-builder

Build script compilation for Kobotoolbox
Shell
1
star
71

utilities

miscelleneous scripts
1
star
72

clean-cooking-platform

JavaScript
1
star
73

internship-program

Internship program repository containing all the course materials and assignment submissions
1
star
74

qgis-server-monitor

An iOS app to monitor if QGIS servers are online
Swift
1
star
75

GeoContextQGISPlugin

A QGIS Plugin for the GeoContext API
Python
1
star
76

kartoza-docker-wordpress

The wordpress site powering kartoza.com, in docker
Makefile
1
star
77

ingc_geonode_theme

INGC Geonode Theme
CSS
1
star
78

docker-metacat

A docker project to set up KNB Metacat and dependencies
Shell
1
star
79

qgis-chat-plugin

A QGIS plugin for chatting on Gitter
Python
1
star
80

docker-qgis-base

A base repo upon which docker-qgis-desktop and docker-qgis-server are built
1
star
81

osm-cartography

Our collaborative work to provide an awesome QGIS cartography for OSM
QML
1
star
82

etherpad

Docker project to provide ether pad - an online document collaboration tool
1
star
83

osm-africa

An Africa project for tilemill derived from https://github.com/mapbox/osm-bright
1
star
84

freshwaterbiodiversity.org

HTML
1
star
85

django-react-base

A boilerplate project for quickly setting up a Django backend with a React frontend
Python
1
star
86

tilemaker-workflows

Internal repo for working with global OSM tiles and tilemaker
Lua
1
star
87

jakarta-flood-maps

A web based flood mapping application for jakarta
Python
1
star
88

InWARDS-Dashboard-Desktop

An application written in Electron.js for visualising stream flow conditions.
Vue
1
star