Skip to content

Instantly share code, notes, and snippets.

@wh13371
Created July 21, 2017 12:12
Show Gist options
  • Save wh13371/d70ad5537f84ad4c64d3cca7c9707156 to your computer and use it in GitHub Desktop.
Save wh13371/d70ad5537f84ad4c64d3cca7c9707156 to your computer and use it in GitHub Desktop.
python - oracle export
#! /usr/bin/python
import sys, time, os, datetime, json
import logging, logging.handlers
import subprocess
log = logging.getLogger(__name__)
UID = "moo"
PWD = "moo"
DB_CONN_STR = "192.168.10.11:1521/XE"
EXPORT_DIR = "/home/fizz/EXP"
def now(fmt='%Y:%m:%d %H:%M:%S.%f'):
return datetime.datetime.now().strftime(fmt)
def logger_config(LOG_FILENAME = 'oraexp.log', segment=200000, expire=10):
log.setLevel(logging.DEBUG)
log_format = logging.Formatter('%(message)s')
file_handler = logging.handlers.RotatingFileHandler(LOG_FILENAME, maxBytes=segment, backupCount=expire)
file_handler.setFormatter(log_format)
log.addHandler(file_handler)
console_handler = logging.StreamHandler()
console_handler.setFormatter(log_format)
log.addHandler(console_handler)
def run(cmd):
p = subprocess.Popen(cmd, shell=True, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
output = p.communicate()[0]
output = output#.decode('utf-8')
return output
def main():
logger_config()
TIMESTAMP = now('%Y_%m_%d_%H_%M_%S_%f')
d = EXPORT_DIR + '/' + TIMESTAMP
log.info (d)
if not os.path.exists(d):
os.makedirs(d)
os.chdir(d)
log.info('@' + now())
cmd_str_data = {"uid" : UID, "pwd" : PWD, "cxn" : DB_CONN_STR, "FILE" : "file=BKUP_" + TIMESTAMP + ".dmp", "LOG" : "log=BKUP_" + TIMESTAMP + ".log", "EXTRA": "STATISTICS=NONE"}
s = "/usr/lib/oracle/12.2/client64/bin/exp {uid}/{pwd}@{cxn} {FILE} {LOG} {EXTRA}".format(**cmd_str_data)
log.info (s)
db_exp = run(s)
log.debug (db_exp)
log.info('#' + now())
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment