Помогите пожалуйста поправить косяк в скрипте бекапирования, который читает из конфига параметры, и в случае неудачи шлет письмо через указанный сервер. Не могу до конца разобраться с smtplib:
#cat backup.conf
[Dirs]
dirs: /home/amihailov/back
backup_dir : /home/amihailov/backups
[Config]
from_mail: a.mikhailov@myhost.ru
admin_mail: amihailov@gmail.com
mail_server: mail.myhost.ru
delete_after_days: 5
pas: xxx
~$ cat backup.py
#!/usr/bin/python2
import ConfigParser
import sys, commands, os, datetime
import smtplib
from email.mime.text import MIMEText
def send_mail(from_mail, to_mail, subj, message, server, pas):
message = MIMEText(message)
message['Subject'] = subj
message['From'] = from_mail
message['To'] = to_mail
s = smtplib.SMTP(server)
s.connect(server)
s.login(from_mail, pas)
s.sendmail(from_mail, to_mail, message.as_string())
s.quit()
class BackupConfig:
def __init__(self, filename):
self.config = ConfigParser.ConfigParser()
self.config.read(filename)
def get(self, section, name):
return self.config.get(section, name, 0)
def main():
# Vars:
config = BackupConfig('backup.conf')
dirs = config.get('Dirs', 'dirs')
backup_dir = config.get('Dirs', 'backup_dir')
from_mail = config.get('Config', 'from_mail')
admin_mail = config.get('Config', 'admin_mail')
mail_server = config.get('Config', 'mail_server')
pas = config.get('Config', 'pas')
delete_after_days = config.get('Config', 'delete_after_days')
timestamp = str(datetime.date.today())
# ---
# Clean backup dir, delete old files
os.system("find " + backup_dir + " -mtime +" + delete_after_days + " -exec rm '{}' \;")
# ---
dirs = dirs.split(',')
for directory in dirs:
code = commands.getstatusoutput('7za a -t7z -mx0 ' + backup_dir + '/' + str(os.path.split(directory)[1]) + '_' + timestamp + '.7z "' + directory + '/"')
if code[0] != 1:
print str(code[1])
send_mail(
from_mail,
admin_mail,
'backup error',
'Error: 7Zip return error code. Directory: ' + directory + '\n\r Error: ' + str(code[1]),
mail_server,
pas
)
if __name__ == "__main__":
main()