26.9.06

Copias de seguridad por e-mail?

Hace unos días alguien me planteó la posibilidad de tener un script en el cron que enviase por correo electrónico los logs del servidor ftp. Sin lugar a dudas rsync es el rey de las copias de seguridad pero ... vamos a hacerlo con un poco de Python:

!/usr/bin/env python
# -*- coding: iso-8859-15 -*-

import zipfile
import glob, os, socket
import email
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders

rutaFicheirosLog="/var/log/vsftdp*"
logsComprimidos="/tmp/ficheiroslog.zip"
correoSuxeito="Logs vsftpd para " + socket.gethostname()
correoCorpo="Documento adxunto comprimido Zip"

def sendMail(to, subject, text, files=[], server="smtp.meuservidor.com"):
assert type(to) == list
assert type(files) == list
fro = "Administrador_Sistemas "

msg = MIMEMultipart()
msg['From'] = fro
msg['To'] = COMMASPACE.join(to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject

msg.attach( MIMEText(text) )

for file in files:
part = MIMEBase('application', "octect-stream")
part.set_payload( open(file,"rb").read() )
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(file))
msg.attach(part)

smtp = smtplib.SMTP(server)
smtp.sendmail(fro, to, msg.as_string())
smtp.close()


def atopaReportes(ruta):
lstReportes = []
lstReportes = glob.glob(ruta)
return lstReportes

if __name__=="__main__":

ficheiroZip = zipfile.ZipFile(logsComprimidos,"w")
listadoLogs = atopaReportes(rutaFicheirosLog)

for ficheiro in listadoLogs:
ficheiroZip.write(ficheiro, os.path.basename(ficheiro), zipfile.ZIP_DEFLATED)

ficheiroZip.close()

sendMail(["jose@meuservidor.com"], correoSuxeito, correoCorpo, ["/tmp/ficheiroslog.zip"])


Es un poco trivial, pero para echarlo a andar por casa viene siendo útil.

3 comentarios:

Anónimo dijo...

To be a noble benign being is to be enduring a make of openness to the in the seventh heaven, an cleverness to trust unsure things beyond your own control, that can take you to be shattered in hugely exceptionally circumstances on which you were not to blame. That says something uncommonly impressive relating to the get of the righteous passion: that it is based on a corporation in the unpredictable and on a willingness to be exposed; it's based on being more like a plant than like a prize, something rather tenuous, but whose extremely special handsomeness is inseparable from that fragility.

Anónimo dijo...

In everyone's time, at some occasion, our inner fire goes out. It is then bust into passion at hand an contend with with another magnanimous being. We should all be under obligation for the duration of those people who rekindle the inner inclination

Anónimo dijo...

In everyone's sustenance, at some occasion, our inner fire goes out. It is then burst into passion by an be faced with with another human being. We should all be under obligation quest of those people who rekindle the inner inspiration