From 705c607ac0a1cc929a7d919c8c814252032e76cd Mon Sep 17 00:00:00 2001 From: jaydee Date: Thu, 16 Mar 2023 08:28:47 +0100 Subject: [PATCH] pg_backup init --- pg_backup.py | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 pg_backup.py diff --git a/pg_backup.py b/pg_backup.py new file mode 100644 index 0000000..ed2e6f0 --- /dev/null +++ b/pg_backup.py @@ -0,0 +1,62 @@ +import re +import subprocess +import os +import json +import sys +import time +import json +import datetime +import getopt +import requests +import logging + + +try: + opts, args = getopt.getopt(sys.argv[1:], "hbl:o:s:t:", ["command=", "help", "output=", "backup"]) +except getopt.GetoptError as err: + # print help information and exit: + print(str(err)) # will print something like "option -a not recognized" + #usage() + sys.exit(2) +output = None +# QJ : getopts + +OUTPUT_FILE = "pg_backup.sql" + +for o, a in opts: + if o == "-h": + _ACTION = True + elif o in ("-b", "--backup"): + _BACKUP = True + elif o in ("-l", "--level"): + _LOG_LEVEL = a.upper() + elif o in ("-o", "--output"): + OUTPUT_FILE = a + elif o in ("-s", "--schema"): + SCHEMA = a + elif o in ("-t", "--tables"): + TABLES = a + + else: + _WIZZARD = True + +LOG_FILE = "/tmp/pg_backup.log" + +if _LOG_LEVEL == "DEBUG": + logging.basicConfig(filename=LOG_FILE, level=logging.DEBUG, format='%(asctime)s : %(levelname)s : %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') + logging.debug('using debug loging') +elif _LOG_LEVEL == "ERROR": + logging.basicConfig(filename=LOG_FILE, level=logging.ERROR, format='%(asctime)s : %(levelname)s : %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') + logging.info('using error loging') +elif _LOG_LEVEL == "SCAN": + logging.basicConfig(filename=LOG_FILE, level=logging.DEBUG, format='%(asctime)s : %(levelname)s : %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') + logging.info('using error loging') +else: + logging.basicConfig(filename=LOG_FILE, level=logging.INFO, format='%(asctime)s : %(levelname)s : %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') + + +cmnd = "sudo -u postgres pg_dump {} {} > {}".format(TABLES, SCHEMA, OUTPUT_FILE) +print(cmnd) +#status, output = subprocess.getstatusoutput(cmnd) + +