main.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. from shara.smb_path_creator import smb_path_creator as spc
  2. from shara.works_with_smb import works_with_smb
  3. from datetime import date
  4. from dotenv import load_dotenv
  5. from email.message import EmailMessage
  6. from email.headerregistry import Address
  7. import smtplib
  8. import os
  9. import socket
  10. # Load variable from .env
  11. dotenv_path = os.path.join(os.path.dirname(__file__), '.env')
  12. if os.path.exists(dotenv_path):
  13. load_dotenv(dotenv_path=dotenv_path)
  14. SERVER_DOMAIN = os.getenv('SERVER_DOMAIN')
  15. PATH_DIR_LOCAL_BACKUP = os.getenv('PATH_LOCAL_BACKUP')
  16. # SMB_CONFIG
  17. wws = works_with_smb(os.getenv('SMB_USER'), os.getenv('SMB_PASSWORD'),
  18. remote_name_machine=os.getenv('SMB_NAME_MACHIME'),
  19. ip=os.getenv('SMB_SERVER'),
  20. share_name=os.getenv('SMB_SHARA_NAME'),
  21. my_hostname=socket.gethostname())
  22. wws.connect()
  23. def get_date():
  24. return date.today().strftime('%Y%m%d')
  25. def filter_files(list_files, filter):
  26. return [file for file in list_files if filter in file]
  27. def get_last_backups():
  28. list_files = sorted(os.listdir(PATH_DIR_LOCAL_BACKUP))
  29. last_db_backup = filter_files(list_files, '-db-')[-1]
  30. last_site_backup = filter_files(list_files, '-site-')[-1]
  31. last_data_backup = filter_files(list_files, '-data-')[-1]
  32. return [last_db_backup, last_site_backup, last_data_backup]
  33. def del_old_backup(file):
  34. if '-db-' in file:
  35. wws.delete_files(spc(SERVER_DOMAIN, '*-db-*'))
  36. elif '-site-' in file:
  37. wws.delete_files(spc(SERVER_DOMAIN, '*-site-*'))
  38. elif '-data-' in file:
  39. wws.delete_files(spc(SERVER_DOMAIN, '*-data-*'))
  40. def create_message(file):
  41. msg = EmailMessage()
  42. msg['Subject'] = F"Copy backup: {file}"
  43. from_addr = os.getenv('SMTP_USER').split("@")
  44. msg['From'] = Address("Program: open_kbsu", from_addr[0], from_addr[1])
  45. to_addr = os.getenv("ADDRESS_TO").split("@")
  46. msg['To'] = Address(os.getenv("NAME"), to_addr[0], to_addr[1])
  47. msg.set_content(F"Copy backup {file} from {SERVER_DOMAIN}")
  48. return msg
  49. def send_massage(msg):
  50. try:
  51. smtp = smtplib.SMTP_SSL(host=os.getenv(
  52. "SMTP_SERVER"), port=os.getenv("SMTP_PORT"), timeout=20)
  53. smtp.login(os.getenv("SMTP_USER"), os.getenv("SMTP_PASSWORD"))
  54. smtp.send_message(msg)
  55. except smtplib.SMTPSenderRefused:
  56. pass
  57. except smtplib.SMTPAuthenticationError:
  58. pass
  59. def main():
  60. wws.create_dir(SERVER_DOMAIN)
  61. last_backups = get_last_backups()
  62. for file in filter_files(last_backups, get_date()):
  63. if file not in wws.get_listfiles(spc(SERVER_DOMAIN)):
  64. del_old_backup(file)
  65. wws.copy_files_to_smb(
  66. spc(PATH_DIR_LOCAL_BACKUP, file), spc(SERVER_DOMAIN, file))
  67. send_massage(create_message(file))
  68. wws.close()
  69. if __name__ == "__main__":
  70. main()