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)