Commit 47fe0fec authored by Björn's avatar Björn

adding #12 #11

parent 30d25a1c
......@@ -21,7 +21,8 @@ import os
import subprocess
import paramiko
import platform
import termcolor
from termcolor import colored
from src.sendmail import Sendmail
from src.config import Config
......@@ -69,10 +70,13 @@ class Backup(Config):
#
# @param message
#
def _log(self, message):
def _log(self, message, color = None):
if (self._args.sendmail == True):
self._logs += message + '\n'
self._logs += str(message) + '\n'
else:
if (color):
message = colored(message, color)
print(message)
#
......@@ -103,12 +107,12 @@ class Backup(Config):
else:
includes = source
else:
self._log('Error: "' + source + '" not found!')
self._log('Error: "' + source + '" not found!', 'yellow')
# if includes is empty
if (includes == ''):
self._log('Error: no sources are found!')
self._log('No Backup! Exit Script!')
self._log('Error: no sources are found!', 'yellow')
self._log('Backup Failed! Exit Script!', 'red')
self._sendmail()
exit()
......@@ -151,9 +155,8 @@ class Backup(Config):
def _command(self, command):
process = subprocess.run(command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
check=True,
shell=True)
return process
......@@ -184,7 +187,7 @@ class Backup(Config):
error = process.stderr
# if directory not found, create with path new
if (error.find('No such file or directory') > -1):
if (error and error.find('No such file or directory') > -1):
self._command(self._sftp_command('mkdir ' + directory))
#
......@@ -198,6 +201,12 @@ class Backup(Config):
includes = self._includes()
process = self._command(self._gpg_passphrase() + ' duplicity ' + self._args.command + ' ' + includes + ' ' + self._gpg_key_id() + ' ' + self._target())
if (process.stdout.find('GPGError') > -1):
print(colored(process.stdout, 'yellow'))
print(colored('Tip: gpg --edit-key <email> and change trust to 5', 'yellow'))
print(colored('Backup Failed! Exit Script!', 'red'))
exit()
self._log(process.stdout)
self._log(process.stderr)
......
......@@ -15,6 +15,8 @@ from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from termcolor import colored
#
#
#
......@@ -74,7 +76,9 @@ class Sendmail:
if key in os.environ:
self._config[key] = os.environ[key]
else:
print('Environment variable "' + key + '" not found!')
print(colored('Environment variable "' + key + '" not found!', 'yellow'))
print(colored('Tip: show ~/.bash_profile if Environment Variables are set', 'yellow'))
print(colored('Sendmail Failed! Exit Script!', 'red'))
exit()
#
......@@ -132,7 +136,12 @@ class Sendmail:
# if string is empty, show status and end programm
if encrypted_string == '':
print('Error! ' + encrypted_gpg.status)
print(colored('Error! ' + encrypted_gpg.status, 'yellow'))
if (encrypted_gpg.status.find('invalid recipient') > -1):
print(colored('Tip: gpg --edit-key <email> and change trust to 5', 'yellow'))
print(colored('Sendmail Failed! Exit Script!', 'red'))
exit()
encrypted_message.set_payload(encrypted_string)
......@@ -157,8 +166,8 @@ class Sendmail:
else:
mailer = smtplib.SMTP(self._config['SMTP_SERVER'], self._config['SMTP_PORT'])
except Exception as exception:
print('SMTP Error!')
print(exception)
print(colored('SMTP Error!', 'yellow'))
print(colored('Backup Failed! Exit Script!', 'red'))
exit()
mailer.login(self._config['SMTP_USERNAME'], self._config['SMTP_PASSWORD'])
......
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