-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.py
47 lines (40 loc) · 1.44 KB
/
backup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from datetime import datetime
import os
import shutil
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
hoy = datetime.now().date().strftime("%d_%m_%Y")
shutil.make_archive(hoy, 'zip', "/home/guaurderia/web2py/applications/guaurderia/databases")
ruta_zip = "/home/guaurderia/" + hoy + ".zip"
username = "[email protected]"
password = "furlteaqbsagfryi"
mail_from = "[email protected]"
mail_to = "[email protected]"
mail_subject = "Backup de " + hoy
mail_body = "Este es el zip que contiene la base de datos"
mail_attachment=ruta_zip
mail_attachment_name=hoy + ".zip"
mimemsg = MIMEMultipart()
mimemsg['From']=mail_from
mimemsg['To']=mail_to
mimemsg['Subject']=mail_subject
mimemsg.attach(MIMEText(mail_body, 'plain'))
with open(mail_attachment, "rb") as attachment:
mimefile = MIMEBase('application', 'octet-stream')
mimefile.set_payload((attachment).read())
encoders.encode_base64(mimefile)
mimefile.add_header('Content-Disposition', "attachment; filename= %s" % mail_attachment_name)
mimemsg.attach(mimefile)
connection = smtplib.SMTP(host='smtp.gmail.com', port=587)
connection.starttls()
connection.login(username,password)
connection.send_message(mimemsg)
connection.quit()
if os.path.exists(hoy + ".zip"):
os.remove(hoy + ".zip")
print("Fichero borrado")
else:
print("The file does not exist")