Package: psqlsync
PostgreSQLSyncTool to create basic PostgreSQL dumps and restore them from local files.Don't use this for production-critical backups, SQL dumps (the method used by this library) are neither efficient nor safe for that purpose. Instead, use a tool likeBarman.Based onpostgres_manage_pythonbyvalferon. Thanks to him for the core logic.This was forked to create a more minimal and maintainable package for the specific use case of syncing a populated testing database.Getting StartedSetupThis library requires the installation of a PostgreSQL client, as it runs pg_restore and pg_dump directly from the command line, as there are no Python bindings for these functions, unfortunately. Use the below instructions to install the PostgreSQL 14 client (instructions from here). Note that this installs some basic tools (see the first line of the shell below), replace them at your discretion if e.g. you don't want to bloat your container environment.sudoaptinstallcurlca-certificatesgnupglsb-release
curlhttps://www.postgresql.org/media/keys/ACCC4CF8.asc|gpg--dearmor|sudotee/etc/apt/trusted.gpg.d/apt.postgresql.org.gpg>/dev/null
sudosh-c'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'sudoaptupdate
sudoaptinstallpostgresql-client-14Create configuration file (ie. sample.toml)[setup]storage_engine="LOCAL"[local_storage]path="./backups/"[postgresql]host="<your_psql_addr(probably 127.0.0.1)>"port="<your_psql_port(probably 5432)>"db="<your_db_name>"user="<your_username>"password="<your_password>"UsageList databases on a PostgreSQL serverpsqlsync --config sample.toml --action list_dbsCreate database backup and store it (based on config file details)psqlsync --config sample.toml --action backup --verboseList previously created database backups available on storage enginepsqlsync --config sample.toml --action listRestore previously created database backups available on storage engine (check available dates withlistaction, it matches the time string, so any unique part of the string suffices)pslsync --config sample.toml --action restore --date 2021Restore previously created database backups into a new destination databasepslsync --config sample.toml --action restore --date 20211219-14 --dest-db new_DB_nameEnter password in prompt, so it does not have to be stored in plaintext in the config filepslsync --config sample.toml --action backup --prompt-pass
Password for database:Command helpusage: psqlsync [-h] --action action [--time YYYYMMdd-HHmmss] [--dest-db dest_db] [--verbose] --config CONFIG
                [--prompt-pass]

psqlsync

optional arguments:
  -h, --help            show this help message and exit
  --action action       'list' (backups), 'list_dbs' (available dbs), 'restore' (requires --time), 'backup'
  --time YYYYMMdd-HHmmss
                        Time to use for restore (show with --action list). If unique, will smart match. (If
                        there's just one backup matching YYYMM, providing that is enough)
  --dest-db dest_db     Name of the new restored database
  --verbose             Verbose output
  --config CONFIG       Database configuration file path (.toml)
  --prompt-pass         Show a password prompt instead of the password defined in the config.Run programmaticallyThebackupandrestoreaction have been seperated into easily callable Python functions inpsqlsync.actions. You can import this module and call these functions from your Python code.AuthorsTip ten BrinkVal Feron-Initial workLicenseThe original code, created byvalferonin thepostgres_manage_python repository, is licensed under the MIT License. This project as a whole, most notably my original code, is licensed under the Apache License v2.0.