Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import helpers
- logger = helpers.init_logger("AutoBackup", debug=DEBUG)
- import os
- import host
- import zipfile
- from ftp import FTPClient
- from schedule import ScheduleObject
- from email_sender import EmailSender
- from time import strftime, localtime
- LOGDIR = (
- host.settings("system_wide_options")["screenshots_folder"]
- if LOGDIR in ["default", ""]
- else LOGDIR
- )
- if not os.path.isdir(LOGDIR):
- os.mkdir(LOGDIR)
- assert os.access(LOGDIR, os.W_OK), "No access to {}".format(LOGDIR)
- if not SCHEDULE:
- raise ValueError("Необходимо выбрать расписание!")
- backup_path = LOGDIR.decode("utf8")
- settings_dir = os.getcwd() if os.name == "nt" else "/home/trassir"
- if host.settings("").guid == "client":
- settings_path = os.path.join(settings_dir, "_t1client.settings")
- license_path = None
- else:
- settings_path = os.path.join(settings_dir, "_t1server.settings")
- license_path = os.path.join(settings_dir, " Trassir 3 License.txt")
- ftp = None
- if SEND_FTP:
- ftp = FTPClient(
- FTP_HOST, port=FTP_PORT, user=FTP_USER, passwd=FTP_PASSWD, work_dir=FTP_PATH
- )
- mail = None
- if SEND_MAIL:
- EmailSender.parse_mails(MAILS)
- mail = EmailSender(MAIL_ACCOUNT)
- def create_backup():
- filename = (
- host.settings("")["name"]
- + strftime("_%d%m%Y_%H%M%S_", localtime())
- + "backup.zip"
- )
- path_to_zip = os.path.join(os.path.join(backup_path, filename))
- with zipfile.ZipFile(path_to_zip, "w") as f:
- if license_path:
- f.write(settings_path, arcname="_t1server.settings")
- f.write(license_path, arcname=" Trassir 3 License.txt")
- else:
- f.write(settings_path, arcname="_t1client.settings")
- logger.debug("backup saved %s" % path_to_zip)
- host.stats()["run_count"] += 1
- return path_to_zip
- def color_change_handler(sched):
- if sched.color == "Blue":
- backup = create_backup()
- if mail:
- logger.debug("send mail")
- mail.send(MAILS, attachments=[backup])
- if ftp:
- logger.debug("send backup on ftp")
- ftp.send(backup)
- if REMOVE:
- host.timeout(60000, lambda: remove_file(backup))
- schedule = ScheduleObject(SCHEDULE, color_change_handler=color_change_handler)
- def remove_file(backup):
- logger.debug("removing %s", backup)
- if os.path.isfile(backup):
- os.remove(backup)
- logger.debug("remove ok")
- else:
- logger.debug("%s is not exists", backup)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement