Last active
May 19, 2020 10:31
-
-
Save plaes/c39ae7669914ee8d392b0c53a1fe9f50 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cat basebackup.py | |
import io, os | |
import psycopg2 | |
import tarfile | |
from contextlib import closing | |
from datetime import date | |
from time import time | |
def add_file(archive, filename, data): | |
with closing(io.BytesIO(data.encode())) as f: | |
tarinfo = tarfile.TarInfo(filename) | |
tarinfo.size = len(f.getvalue()) | |
tarinfo.mtime = time() | |
archive.addfile(tarinfo, fileobj=f) | |
def main(): | |
# TODO: Args... | |
connection = psycopg2.connect(port=5433) | |
cursor = connection.cursor() | |
cursor.execute("SELECT pg_start_backup('backup-date', false, false);") | |
# result = cursor.fetchone() | |
# print(result) | |
backup_file = "pgsql-basebackup-{}.tar.xz".format(date.today()) | |
data_dir = "paf_data-{}".format(date.today()) | |
tar = tarfile.open(backup_file, "w:xz") | |
tar.add("/var/lib/pgsql/12/paf_data", arcname=data_dir) | |
cursor.execute("SELECT * FROM pg_stop_backup(false, true);"); | |
lsn, labelfile_data, spcmapdata = cursor.fetchone(); | |
add_file(tar, os.path.join(data_dir, 'backup_label'), labelfile_data) | |
if spcmapdata: | |
add_file(tar, os.path.join(data_dir, 'tablespace_map'), spcmapdata) | |
tar.close() | |
cursor.close() | |
connection.close() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment