Commit be32eb62 authored by Björn's avatar Björn

adding #16

parent 76290235
......@@ -13,3 +13,13 @@ remove_all_inc_of_but_n_full=
username=
password=
temp=
[email]
smpt_server=
smtp_port=
smpt_username=
smtp_password=
smtp_auth_mode=
smtp_encryption=
smtp_from=
smtp_to=
......@@ -6,27 +6,8 @@ supports rsync to upload and download.
## Install
```bash
apt install duplicity
pip3 install python-gnupg
pip3 install configparser
pip3 install paramiko
pip3 install termcolor
```
For sending output through E-Mails, you have to export variables.
**This will be integrated in the next version to .config**
```bash
nano ~/.bash_profile
export SMTP_SERVER=""
export SMTP_PORT=""
export SMTP_USERNAME=""
export SMTP_PASSWORD=""
export SMTP_AUTH_MODE=""
export SMTP_ENCRYPTION=""
export SMTP_FROM=""
export SMTP_TO=""
sudo apt install duplicity python3-pip
pip3 install python-gnupg configparser paramiko termcolor
```
## Config
......@@ -109,6 +90,12 @@ Count for backups that will be not deleted.
#### remove_all_inc_of_but_n_full**
Count for backups that will be not deleted.
### Email
#### smtp
Options to send a mail with smtp
### Storagebox
If you use a storagebox from Hetzner you can add and remove SSH-Keys with,
......
......@@ -75,7 +75,7 @@ class Backup(Config):
#
def _sendmail(self):
if (self._args.sendmail == True):
sendmail = Sendmail(self._logs, 'Backup / ' + self._args.command + ' / ' + platform.uname()[1])
sendmail = Sendmail(self._logs, 'Backup / ' + self._args.command + ' / ' + platform.uname()[1], self._config)
sendmail.run()
#
......
......@@ -34,26 +34,22 @@ class Sendmail:
# @param subject
# @param options { encrypt: True or False }
#
def __init__(self, message, subject):
self._config = {
'SMTP_PORT': 465
}
def __init__(self, message, subject, config):
self._message = message
self._subject = subject
self._config = config
self._config_required = [
'SMTP_SERVER',
'SMTP_PORT',
'SMTP_USERNAME',
'SMTP_PASSWORD',
'SMTP_AUTH_MODE',
'SMTP_ENCRYPTION',
'SMTP_FROM',
'SMTP_TO'
'smtp_server',
'smtp_port',
'smtp_username',
'smtp_password',
'smtp_auth_mode',
'smtp_encryption',
'smtp_from',
'smtp_to'
]
self._message = message
self._subject = subject
self._validate_config()
#
......@@ -75,13 +71,8 @@ class Sendmail:
# adding config values
for key in self._config_required:
# reading environment variables
if key in os.environ:
self._config[key] = os.environ[key]
else:
if self._config.has_option('email', key) == False:
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()
......@@ -98,8 +89,8 @@ class Sendmail:
multipart.attach(body)
multipart.add_header('Subject', self._subject)
multipart.add_header('From', self._config['SMTP_FROM'])
multipart.add_header('To', self._config['SMTP_TO'])
multipart.add_header('From', self._config.get('email', 'smtp_from'))
multipart.add_header('To', self._config.get('email', 'smtp_to'))
return multipart
......@@ -113,8 +104,8 @@ class Sendmail:
encrypted = MIMEBase('multipart', _subtype='encrypted', protocol='application/pgp-encrypted')
encrypted.add_header('Subject', self._subject)
encrypted.add_header('From', self._config['SMTP_FROM'])
encrypted.add_header('To', self._config['SMTP_TO'])
encrypted.add_header('From', self._config.get('email', 'smtp_from'))
encrypted.add_header('To', self._config.get('email', 'smtp_to'))
# adding headers
encrypted_header = email.message.Message()
......@@ -135,7 +126,7 @@ class Sendmail:
gpg = gnupg.GPG(gnupghome=gpgpath)
encrypted_gpg = gpg.encrypt(message.as_string(), self._config['SMTP_TO'])
encrypted_gpg = gpg.encrypt(message.as_string(), self._config.get('email', 'smtp_to'))
encrypted_string = str(encrypted_gpg)
# if string is empty, show status and end programm
......@@ -165,15 +156,15 @@ class Sendmail:
# Now send the message
try:
if self._config['SMTP_ENCRYPTION'] == 'ssl' or self._config['SMTP_ENCRYPTION'] == 'tls':
mailer = smtplib.SMTP_SSL(self._config['SMTP_SERVER'], self._config['SMTP_PORT'])
if self._config.get('email', 'smtp_encryption') == 'ssl' or self._config.get('email', 'smtp_encryption') == 'tls':
mailer = smtplib.SMTP_SSL(self._config.get('email', 'smtp_server'), self._config.get('email', 'smtp_port'))
else:
mailer = smtplib.SMTP(self._config['SMTP_SERVER'], self._config['SMTP_PORT'])
mailer = smtplib.SMTP(self._config.get('email', 'smtp_server'), self._config.get('email', 'smtp_port'))
except Exception as exception:
print(colored('SMTP Error!', 'yellow'))
print(colored('Backup Failed! Exit Script!', 'red'))
exit()
mailer.login(self._config['SMTP_USERNAME'], self._config['SMTP_PASSWORD'])
mailer.sendmail(self._config['SMTP_FROM'], self._config['SMTP_TO'], message.as_string())
mailer.login(self._config.get('email', 'smtp_username'), self._config.get('email', 'smtp_password'))
mailer.sendmail(self._config.get('email', 'smtp_from'), self._config.get('email', 'smtp_to'), message.as_string())
mailer.close()
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