Commit 24faea3c authored by Björn's avatar Björn

adding

parent 06e723f2
#!/usr/bin/env python3
#
#
#
#
import argparse
import configparser
import subprocess
from datetime import datetime
from src.sendmail import Sendmail
class Mysqldump:
#
......@@ -10,9 +18,110 @@ class Mysqldump:
#
def __init__(self):
def add(self):
# adding parser for arguments
parser = argparse.ArgumentParser()
# define arguments
parser.add_argument('command', help='all')
# define options
parser.add_argument('-c', '--config', help='full path to config-file')
# sending email for output and error message
parser.add_argument('-s', '--sendmail', help='send log with', action='store_true')
# parse arguments
self._args = parser.parse_args()
if (self._args.config is None):
config = '.config'
else:
config = self._args.config
self._config = configparser.RawConfigParser(allow_no_value=True)
self._config.read_file(open(config))
self._username = self._config.get('mysql', 'username')
self._password = self._config.get('mysql', 'password')
self._host = self._config.get('mysql', 'host', fallback='localhost')
self._target = self._config.get('mysql', 'target')
self._logs = ''
#
# adding message to log if sendmail is True
#
# @param message
#
def _log(self, message):
if (self._args.sendmail == True):
self._logs += message
else:
print(message)
#
#
#
#
def _all(self, now):
date = now.strftime("%d_%m_%Y-%H_%M_%S")
sql = "SHOW DATABASES WHERE \`Database\` NOT REGEXP '(^mysql|_schema$)'"
command = 'mysql --user=' + self._username + ' --password=' + self._password + ' --host=' + self._host + ' -e "' + sql + '"'
process = subprocess.run(command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
check=True,
shell=True)
if (process.stderr != ''):
self._log(process.stderr)
exit()
for database in process.stdout.split('\n'):
if (database != '' and database != 'Database'):
self._single(database, date)
#
#
#
#
def _single(self, database, date):
filename = self._target + '/' + database + '-' + date + '.sql'
command = 'mysqldump --user=' + self._username + ' --password=' + self._password + ' --host=' + self._host + ' ' + database + ' > ' + filename
process = subprocess.run(command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
check=True,
shell=True)
if (process.stderr != ' '):
self._log('Dump ' + database)
else:
self._log(process.stderr)
#
#
#
#
def run(self):
now = datetime.now()
if (self._args.command == 'all'):
self._all(now)
# send mail
if (self._args.sendmail == True):
sendmail = Sendmail(self._logs, 'Mysql / ' + self._args.command + ' / ' + platform.uname()[1])
sendmail.run()
mysqldump = Mysqldump()
mysqldump.run()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment