Coder Social home page Coder Social logo

mccarthysean / timescaledb-backup-s3 Goto Github PK

View Code? Open in Web Editor NEW
8.0 1.0 4.0 78 KB

For creating a Docker image that runs alongside the TimescaleDB container, and uses pg_dump to back up the container periodically, and then uploads the backup to an AWS S3 bucket

License: MIT License

Shell 78.06% Dockerfile 21.94%

timescaledb-backup-s3's Introduction

TimescaleDB-Backup-S3

For creating a Docker container that runs alongside the TimescaleDB container. It uses pg_dump to back up the container periodically, and then uploads the backup to an AWS S3 bucket.

Docker images here: https://hub.docker.com/r/mccarthysean/timescaledb_backup_s3/tags

The container also contains a restore.sh file which uses pg_restore to restore the backup.

For even more convenience, there's also a download_backup_from_AWS_S3.sh script to download a backup file from your AWS S3 bucket, prior to restoring it.

I hope this Docker container makes your life a bit easier.

-Sean

Usage

Docker:

$ docker run \
  -e AWS_ACCESS_KEY_ID=key \
  -e AWS_SECRET_ACCESS_KEY=secret \
  -e AWS_DEFAULT_REGION=us-west-2 \
  -e S3_BUCKET=my-bucket \
  -e S3_PREFIX=subfolder \
  -e POSTGRES_HOST=localhost \
  -e POSTGRES_USER=user \
  -e POSTGRES_PASSWORD=password \
  -e POSTGRES_DATABASE=dbname \
  -e SCHEDULE='0 7 * * *' \
  mccarthysean/timescaledb_backup_s3:latest-14

See the docker-compose.example.yml file for typical usage, like below:

  timescale:
    image: timescale/timescaledb-ha:pg13.8-ts2.8.1-latest
    volumes: 
      - type: volume
        source: timescale-db-pg13
        # the location in the container where the data are stored
        target: /var/lib/postgresql/data
        read_only: false
    env_file: .env
    ports:
      - 0.0.0.0:5432:5432

  backup:
    # image: mccarthysean/timescaledb_backup_s3:13-1.0.10
    image: mccarthysean/timescaledb_backup_s3:14-1.0.10
    env_file: .env
    environment:
      # cron-schedule this backup job to backup and upload to AWS S3 every so often
      # * * * * * command(s)
      # - - - - -
      # | | | | |
      # | | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
      # | | | ------- Month (1 - 12)
      # | | --------- Day of month (1 - 31)
      # | ----------- Hour (0 - 23)
      # ------------- Minute (0 - 59)
      SCHEDULE: '0 7 * * *'
      # The AWS S3 bucket to which the backup file should be uploaded
      S3_BUCKET: backup-timescaledb
      # S3_PREFIX creates a sub-folder in the above AWS S3 bucket
      S3_PREFIX: daily-backups
    networks:
      traefik-public:
    healthcheck:
      # Periodically check if PostgreSQL is ready, for Docker status reporting
      test: ["ping", "-c", "1", "timescale"]
      interval: 60s
      timeout: 5s
      retries: 5
    deploy:
      placement:
        constraints:
          # Since this is for the stateful database,
          # only run it on the swarm manager, not on workers
          - "node.role==manager"
      restart_policy:
        condition: on-failure

Also see the .env_template (to be saved as .env in production) for the environment variables needed.

# For AWS, an access key for a role with write/put permissions to AWS S3 bucket
AWS_ACCESS_KEY_ID=some-key
AWS_SECRET_ACCESS_KEY=password
AWS_DEFAULT_REGION=us-west-2

# For the Postgres/TimescaleDB init/default setup.
# the docker-compose.example.yml file specifies the host as "timescale"
POSTGRES_HOST=timescale
POSTGRES_PORT=5432
POSTGRES_DATABASE=postgres
POSTGRES_USER=postgres
POSTGRES_PASSWORD=password

Automatic Periodic Backups

Set the cron SCHEDULE environment variable like -e SCHEDULE="0 0 * * *" to run the backup automatically.

-Sean

timescaledb-backup-s3's People

Contributors

mccarthysean avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

timescaledb-backup-s3's Issues

DB Structure Restoring

Hi,

I have a couple questions regarding the restore.. So does it only work for a newly created instance of postgre? That is the db that I'm trying to restore is empty (no tables, nothing)? Or can I do it on a live DB, that is it has the tables and data inside it and the restore would just drop everything and restore from the backup?

Second thing, I get these warnings on the daily backup. Is that normal?

2022/02/03 05:00:01 pg_dump: warning: there are circular foreign-key constraints on this table:
2022/02/03 05:00:01 pg_dump:   hypertable
2022/02/03 05:00:01 pg_dump: You might not be able to restore the dump without using --disable-triggers or temporarily dropping the constraints.
2022/02/03 05:00:01 pg_dump: Consider using a full dump instead of a --data-only dump to avoid this problem.
2022/02/03 05:00:01 pg_dump: warning: there are circular foreign-key constraints on this table:
2022/02/03 05:00:01 pg_dump:   chunk
2022/02/03 05:00:01 pg_dump: You might not be able to restore the dump without using --disable-triggers or temporarily dropping the constraints.
2022/02/03 05:00:01 pg_dump: Consider using a full dump instead of a --data-only dump to avoid this problem.
2022/02/03 05:00:01 pg_dump: NOTICE:  hypertable data are in the chunks, no data will be copied
2022/02/03 05:00:01 DETAIL:  Data for hypertables are stored in the chunks of a hypertable so COPY TO of a hypertable will not copy any data.
2022/02/03 05:00:01 HINT:  Use "COPY (SELECT * FROM <hypertable>) TO ..." to copy all data in hypertable, or copy each chunk individually.
2022/02/03 05:00:01 pg_dump: NOTICE:  hypertable data are in the chunks, no data will be copied
2022/02/03 05:00:01 DETAIL:  Data for hypertables are stored in the chunks of a hypertable so COPY TO of a hypertable will not copy any data.
2022/02/03 05:00:01 HINT:  Use "COPY (SELECT * FROM <hypertable>) TO ..." to copy all data in hypertable, or copy each chunk individually.

Finally, I might be wrong but I just tested a restore on a newly created postgre db and it restored all the data just fine and all. However, it did not restore any of the structure of my database. Tables constraints, indexes, triggers etc. Is there no way around this or did I restore it wrong?

Error when building image

Hey, I tried building the image for pg14 and I'm getting the following errors

Step 8/22 : RUN go get -u github.com/timescale/timescaledb-backup/ || true &&     cd /go/pkg/mod/github.com/timescale/[email protected] &&     sed -i 's/github.com\/timescale\/ts-dump-restore/github.com\/timescale\/timescaledb-backup/g' go.mod &&     cd cmd/ts-dump &&     go mod tidy &&     go build -o /usr/local/go/bin/ts-dump &&     cd ../ts-restore &&     go mod tidy &&     go build -o /usr/local/go/bin/ts-restore
 ---> Running in 521db1870e5c
go: go.mod file not found in current directory or any parent directory.
	'go get' is no longer supported outside a module.
	To build and install a command, use 'go install' with a version,
	like 'go install example.com/cmd@latest'
	For more information, see https://golang.org/doc/go-get-install-deprecation
	or run 'go help get' or 'go help install'.
/bin/sh: cd: line 1: can't cd to /go/pkg/mod/github.com/timescale/[email protected]: No such file or directory
The command '/bin/sh -c go get -u github.com/timescale/timescaledb-backup/ || true &&     cd /go/pkg/mod/github.com/timescale/[email protected] &&     sed -i 's/github.com\/timescale\/ts-dump-restore/github.com\/timescale\/timescaledb-backup/g' go.mod &&     cd cmd/ts-dump &&     go mod tidy &&     go build -o /usr/local/go/bin/ts-dump &&     cd ../ts-restore &&     go mod tidy &&     go build -o /usr/local/go/bin/ts-restore' returned a non-zero code: 2

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.