예제 #1
0
    def list_backups_for_server(self, server_id):
        # Grab our backup path from the DB
        backup_list = Backups.get(Backups.server_id == int(server_id))

        # Join into a full path
        backup_folder = self.get_backup_folder_for_server(server_id)
        server_backup_dir = os.path.join(backup_list.storage_location,
                                         backup_folder)

        #helper.ensure_dir_exists(server_backup_dir)

        svr_obj = multi.get_server_obj(server_id)
        return svr_obj.list_backups()
예제 #2
0
    def list_backups(self):
        backup_folder = "{}-{}".format(self.server_id, self.name)
        backup_list = Backups.get(Backups.server_id == int(self.server_id))
        backup_path = os.path.join(backup_list.storage_location, backup_folder)
        #helper.ensure_dir_exists(backup_path)

        results = []

        for dirpath, dirnames, filenames in os.walk(backup_path):
            for f in filenames:
                fp = os.path.join(dirpath, f)
                # skip if it is symbolic link
                if not os.path.islink(fp):
                    size = helper.human_readable_file_size(os.path.getsize(fp))
                    results.append({'path': f, 'size': size})

        return results
예제 #3
0
    def list_all_backups(self):
        # Grab our backup path from the DB
        backup_list = Backups.get()
        backup_data = model_to_dict(backup_list)

        # List all MC Servers
        servers = multi.list_servers()
        backup_files = []

        # Iterate over all the servers
        for server in servers:
            server_id = server['id']

            # Create path
            path = os.path.join(backup_data['storage_location'], server_id)

            # Search and concat
            file_names, relative_files = helper.list_backups(path)
            backup_files = backup_data + relative_files
        return backup_files
예제 #4
0
    def backup_server(self, announce=True):

        # backup path is saved in the db
        # Load initial backup config
        backup_list = Backups.get_by_id(self.server_id)
        backup_data = model_to_dict(backup_list)

        logger.debug("Using default path defined in database")
        backup_folder = "{}-{}".format(self.server_id, self.name)
        backup_path = os.path.join(backup_data['storage_location'],
                                   backup_folder)
        helper.ensure_dir_exists(backup_path)

        logger.info('Starting Backup Process')

        logger.info('Checking Backup Path Exists')

        if helper.check_directory_exist(backup_path):

            # if server is running
            if announce:
                if self.check_running():
                    self.send_command(
                        "say [Crafty Controller] Starting Backup of Server")

            try:
                # make sure we have a backup for this date
                backup_filename = "{}.zip".format(
                    datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S'))
                backup_full_path = os.path.join(backup_path, backup_filename)

                logger.info("Backing up server directory to %s",
                            backup_filename)
                logger.debug("Full path is %s", backup_full_path)

                backup_dirs = json.loads(backup_data['directories'])

                helper.zippath(backup_dirs, backup_full_path,
                               ['crafty_backups'])

                logger.info("Backup Completed")

                if announce:
                    if self.check_running():
                        self.send_command(
                            "say [Crafty Controller] Backup Complete")

            except Exception as e:
                logger.exception(
                    "Unable to create backups! Traceback:".format(e))

                if announce:
                    if self.check_running():
                        self.send_command(
                            'say [Crafty Controller] Unable to create backups - check the logs'
                        )

            # remove any extra backups
            max_backups = backup_data['max_backups']
            logger.info("Checking for backups older than %s days", max_backups)
            helper.del_files_older_than_x_days(max_backups, backup_path)

        else:
            logger.error("Unable to find or create backup path!")
            return False