Commit 14619b81 authored by Björn's avatar Björn

adding #11 #12

parent 4ddbd5ff
......@@ -64,18 +64,24 @@ Exports all Databases of a User.
./mysql.py all
```
Delete all sql-files in temp-directory.
```bash
./mysql.py clean
```
The script uses the same Config-File as **backup.py**.
```bash
[mysql]
username=
password=
target=
temp=
```
**username** username for mysql-user
**password** password for mysql-user
**target** path to save files, will create if n
**temp** path to export sql-files
## GPG-Keys
......
......@@ -43,7 +43,7 @@ class Backup(Config):
parser = argparse.ArgumentParser()
# define arguments
parser.add_argument('command', help='full|incremental|restore|status|files')
parser.add_argument('command', choices=['full', 'incremental', 'restore', 'status', 'files'], help='full|incremental|restore|status|files')
# define options
parser.add_argument('-c', '--config', help='full path to config-file')
......@@ -63,22 +63,6 @@ class Backup(Config):
# init parent
super().__init__()
self._logs = ''
#
# adding message to log if sendmail is True
#
# @param message
#
def _log(self, message, color = None):
if (self._args.sendmail == True):
self._logs += str(message) + '\n'
else:
if (color):
message = colored(message, color)
print(message)
#
#
#
......@@ -147,20 +131,6 @@ class Backup(Config):
def _sftp_command(self, command):
return "echo \"" + command + "\" | sftp -P " + self._remote_port + " " + self._config.get('default', 'remote_host')
#
# call subprocess
#
# @param command
#
def _command(self, command):
process = subprocess.run(command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
shell=True)
return process
#
# create directory of target if not exists with sftp
#
......@@ -208,7 +178,7 @@ class Backup(Config):
exit()
self._log(process.stdout)
if (process.stderr):
self._log(process.stderr)
......
#!/usr/bin/env python3
#
# dump mysql-databases
#
# usage: mysql.py [-h] [-c CONFIG] [-s] {all,clean}
#
# positional arguments:
# {all,clean} all|clean
#
# optional arguments:
# -h, --help show this help message and exit
# -c CONFIG, --config CONFIG
# full path to config-file
# - s, --sendmail send log with
#
#
# https://gitlab.tentakelfabrik.de/tentakelfabrik/backup
#
import argparse
import configparser
import subprocess
import os
from termcolor import colored
from datetime import datetime
from src.sendmail import Sendmail
from src.config import Config
class Mysqldump:
class Mysqldump(Config):
#
#
......@@ -22,7 +40,7 @@ class Mysqldump:
parser = argparse.ArgumentParser()
# define arguments
parser.add_argument('command', help='all')
parser.add_argument('command', choices=['all', 'clean'], help='all|clean')
# define options
parser.add_argument('-c', '--config', help='full path to config-file')
......@@ -33,33 +51,32 @@ class Mysqldump:
# 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))
# init parent
super().__init__()
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._temp = self._config.get('mysql', 'temp', fallback=None)
self._logs = ''
if (not self._temp):
print(colored('Temp-Directory not set!', 'yellow'))
print(colored('Mysql Failed! Exit Script!', 'red'))
exit()
#
# 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)
if (os.path.exists(self._temp) == False):
print(colored(self._temp + ' not exists!', 'yellow'))
print(colored('Mysql Failed! Exit Script!', 'red'))
exit()
if (os.path.isdir(self._temp) == False):
print(colored(self._temp + ' is not a directory!', 'yellow'))
print(colored('Mysql Failed! Exit Script!', 'red'))
exit()
#
# search for all databases,
# ignore databases with mysql or shema
#
# @param now
#
......@@ -70,14 +87,9 @@ class Mysqldump:
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)
process = self._command(command)
if (process.stderr != ''):
if (process.stderr):
self._log(process.stderr)
else:
for database in process.stdout.split('\n'):
......@@ -85,21 +97,17 @@ class Mysqldump:
self._single(database, date)
#
# dump single database with mysqldump and save it in target-directory
#
# @param database
# @param date
#
def _single(self, database, date):
filename = self._target + '/' + database + '-' + date + '.sql'
filename = self._temp + '/' + 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)
process = self._command(command)
if (process.stderr != ' '):
self._log('Dump ' + database)
......@@ -107,31 +115,16 @@ class Mysqldump:
self._log(process.stderr)
#
#
#
#
def _create_target:
if os.path.exists(self._args.target):
command = 'rm -R ' + self._args.target
#
#
# delete all sql-files from temp-directory
#
#
def _clean(self):
if not os.path.exists(self._args.target):
command = 'mkdir ' + self._args.target
process = subprocess.run(command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
check=True,
shell=True)
command = 'rm ' + self._temp + '/*.sql'
process = self._command(command)
if (process.stderr != ''):
self._log(process.stderr)
if (process.stdout):
self._log(process.stdout)
#
#
......@@ -143,8 +136,10 @@ class Mysqldump:
if (self._args.command == 'all'):
self._all(now)
self._log('Mysql dump ' + self._args.command + ' completed!', 'green')
elif (self._args.command == 'clean'):
self._clean()
self._log('Mysql clean completed!', 'green')
# send mail
if (self._args.sendmail == True):
......
import configparser
import os
import subprocess
from termcolor import colored
......@@ -39,3 +40,32 @@ class Config:
self._config.read_file(open(config))
self._remote_port = self._config.get('default', 'remote_port', fallback='22')
self._logs = ''
#
# call subprocess
#
# @param command
#
def _command(self, command):
process = subprocess.run(command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
shell=True)
return process
#
# adding message to log if sendmail is True
#
# @param message
#
def _log(self, message, color = None):
if (self._args.sendmail == True):
self._logs += str(message) + '\n'
else:
if (color):
message = colored(message, color)
print(message)
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