Git Product home page Git Product logo

postgres-s3-backuper's Introduction

Backup and restore PostgreSQL database with Yandex Object Storage (or any S3-compatible storage)

There are two Python scripts: backup.py and restore.py. The first one creates PostgreSQL dump, zip and encrypt it, and then upload to Yandex Object Storage (or any S3-compatible storage). The second one finds last dump in Yandex Object Storage, download, unzip and decrypt it, and then load to PostgreSQL database accordingly.

YouTube video about scripts (in Russian)

For both scripts you need:

  • Python3.6+ version,
  • installed pip packeges from requirements.txt,
  • Yandex Object Storage / AWS credentials in ~/.aws/credentials,
  • files with public (backup_key.pem.pub) and private (backup_key.pem) keys for encrypting and decrypting dump. You can generate both with openssl:
openssl req -x509 -nodes -days 1000000 -newkey rsa:4096 -keyout backup_key.pem\
 -subj "/C=US/ST=Illinois/L=Chicago/O=IT/CN=www.example.com" \
 -out backup_key.pem.pub
  • file with PostgreSQL database password ~/.pgpass with chmod 600, including, for example:
localhost:5432:your_database:your_db_user:your_db_user_password
  • check check_hostname() function in restore.py โ€” it checks hostname of current server (kind of protection against drop database tables on production server).

Example of backup database (substitute your values in the variables below, note, that here we need public key file for encrypting database):

DB_HOSTNAME=localhost \
DB_NAME=your_database  \
DB_USER=your_db_user  \
BACKUP_KEY_PUB_FILE=/home/www/.backup_key.pem.pub \
S3_BUCKET_NAME=your_s3_bucket  \
TIME_ZONE=Europe/Moscow \
python3 backup.py

Example of load database (note, that here we need private key file for decrypting database):

DB_HOSTNAME=localhost \
DB_NAME=your_database  \
DB_USER=your_db_user  \
BACKUP_KEY_PRIVATE_FILE=/home/www/.backup_key.pem \
S3_BUCKET_NAME=your_s3_bucket  \
TIME_ZONE=Europe/Moscow \
python3 restore.py

postgres-s3-backuper's People

Contributors

alexey-goloburdin avatar

Watchers

 avatar

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.