Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.53 KB | None | 0 0
  1. import helpers
  2.  
  3. logger = helpers.init_logger("AutoBackup", debug=DEBUG)
  4.  
  5. import os
  6. import host
  7. import zipfile
  8. from ftp import FTPClient
  9. from schedule import ScheduleObject
  10. from email_sender import EmailSender
  11. from time import strftime, localtime
  12.  
  13. LOGDIR = (
  14.     host.settings("system_wide_options")["screenshots_folder"]
  15.     if LOGDIR in ["default", ""]
  16.     else LOGDIR
  17. )
  18. if not os.path.isdir(LOGDIR):
  19.     os.mkdir(LOGDIR)
  20.  
  21. assert os.access(LOGDIR, os.W_OK), "No access to {}".format(LOGDIR)
  22.  
  23. if not SCHEDULE:
  24.     raise ValueError("Необходимо выбрать расписание!")
  25.  
  26. backup_path = LOGDIR.decode("utf8")
  27. settings_dir = os.getcwd() if os.name == "nt" else "/home/trassir"
  28. if host.settings("").guid == "client":
  29.     settings_path = os.path.join(settings_dir, "_t1client.settings")
  30.     license_path = None
  31. else:
  32.     settings_path = os.path.join(settings_dir, "_t1server.settings")
  33.     license_path = os.path.join(settings_dir, " Trassir 3 License.txt")
  34.  
  35. ftp = None
  36. if SEND_FTP:
  37.     ftp = FTPClient(
  38.         FTP_HOST, port=FTP_PORT, user=FTP_USER, passwd=FTP_PASSWD, work_dir=FTP_PATH
  39.     )
  40.  
  41. mail = None
  42. if SEND_MAIL:
  43.     EmailSender.parse_mails(MAILS)
  44.     mail = EmailSender(MAIL_ACCOUNT)
  45.  
  46.  
  47. def create_backup():
  48.     filename = (
  49.         host.settings("")["name"]
  50.         + strftime("_%d%m%Y_%H%M%S_", localtime())
  51.         + "backup.zip"
  52.     )
  53.     path_to_zip = os.path.join(os.path.join(backup_path, filename))
  54.     with zipfile.ZipFile(path_to_zip, "w") as f:
  55.         if license_path:
  56.             f.write(settings_path, arcname="_t1server.settings")
  57.             f.write(license_path, arcname=" Trassir 3 License.txt")
  58.         else:
  59.             f.write(settings_path, arcname="_t1client.settings")
  60.     logger.debug("backup saved %s" % path_to_zip)
  61.     host.stats()["run_count"] += 1
  62.     return path_to_zip
  63.  
  64.  
  65. def color_change_handler(sched):
  66.     if sched.color == "Blue":
  67.         backup = create_backup()
  68.         if mail:
  69.             logger.debug("send mail")
  70.             mail.send(MAILS, attachments=[backup])
  71.         if ftp:
  72.             logger.debug("send backup on ftp")
  73.             ftp.send(backup)
  74.         if REMOVE:
  75.             host.timeout(60000, lambda: remove_file(backup))
  76.  
  77.  
  78. schedule = ScheduleObject(SCHEDULE, color_change_handler=color_change_handler)
  79.  
  80.  
  81. def remove_file(backup):
  82.     logger.debug("removing %s", backup)
  83.     if os.path.isfile(backup):
  84.         os.remove(backup)
  85.         logger.debug("remove ok")
  86.     else:
  87.         logger.debug("%s is not exists", backup)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement