2022-03-01 01:02:30 +00:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import logging
|
|
|
|
import pathlib
|
2022-06-20 19:26:21 +00:00
|
|
|
import tempfile
|
|
|
|
import zipfile
|
2022-03-08 04:40:44 +00:00
|
|
|
from zipfile import ZipFile, ZIP_DEFLATED
|
2022-03-01 01:02:30 +00:00
|
|
|
|
2022-06-20 19:26:21 +00:00
|
|
|
from app.classes.shared.helpers import Helpers
|
|
|
|
from app.classes.shared.console import Console
|
2023-08-09 21:47:53 +00:00
|
|
|
from app.classes.shared.websocket_manager import WebSocketManager
|
2022-06-20 19:26:21 +00:00
|
|
|
|
2022-03-01 01:02:30 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
|
2022-03-01 01:02:30 +00:00
|
|
|
class FileHelpers:
|
2022-03-23 02:50:12 +00:00
|
|
|
allowed_quotes = ['"', "'", "`"]
|
2022-03-01 01:02:30 +00:00
|
|
|
|
2022-06-20 19:26:21 +00:00
|
|
|
def __init__(self, helper):
|
|
|
|
self.helper: Helpers = helper
|
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
@staticmethod
|
|
|
|
def del_dirs(path):
|
2022-03-01 01:02:30 +00:00
|
|
|
path = pathlib.Path(path)
|
|
|
|
for sub in path.iterdir():
|
|
|
|
if sub.is_dir():
|
|
|
|
# Delete folder if it is a folder
|
2022-04-11 05:23:55 +00:00
|
|
|
FileHelpers.del_dirs(sub)
|
2022-03-01 01:02:30 +00:00
|
|
|
else:
|
|
|
|
# Delete file if it is a file:
|
2022-08-17 21:41:44 +00:00
|
|
|
try:
|
|
|
|
sub.unlink()
|
|
|
|
except:
|
|
|
|
logger.error(f"Unable to delete file {sub}")
|
2022-08-17 22:14:16 +00:00
|
|
|
try:
|
|
|
|
# This removes the top-level folder:
|
|
|
|
path.rmdir()
|
2024-01-28 21:21:27 +00:00
|
|
|
except Exception as e:
|
2022-08-17 22:14:16 +00:00
|
|
|
logger.error("Unable to remove top level")
|
2024-01-28 21:21:27 +00:00
|
|
|
return e
|
2022-03-01 01:02:30 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def del_file(path):
|
|
|
|
path = pathlib.Path(path)
|
|
|
|
try:
|
|
|
|
logger.debug(f"Deleting file: {path}")
|
2022-03-23 02:50:12 +00:00
|
|
|
# Remove the file
|
2022-03-01 01:02:30 +00:00
|
|
|
os.remove(path)
|
|
|
|
return True
|
2024-01-28 21:21:27 +00:00
|
|
|
except (FileNotFoundError, PermissionError) as e:
|
2022-03-01 01:02:30 +00:00
|
|
|
logger.error(f"Path specified is not a file or does not exist. {path}")
|
2024-01-28 21:21:27 +00:00
|
|
|
return e
|
2022-03-01 01:02:30 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def copy_dir(src_path, dest_path, dirs_exist_ok=False):
|
|
|
|
# pylint: disable=unexpected-keyword-arg
|
|
|
|
shutil.copytree(src_path, dest_path, dirs_exist_ok=dirs_exist_ok)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def copy_file(src_path, dest_path):
|
|
|
|
shutil.copy(src_path, dest_path)
|
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
@staticmethod
|
|
|
|
def move_dir(src_path, dest_path):
|
|
|
|
FileHelpers.copy_dir(src_path, dest_path)
|
|
|
|
FileHelpers.del_dirs(src_path)
|
2022-03-01 01:02:30 +00:00
|
|
|
|
2022-08-31 15:59:33 +00:00
|
|
|
@staticmethod
|
|
|
|
def move_dir_exist(src_path, dest_path):
|
|
|
|
FileHelpers.copy_dir(src_path, dest_path, True)
|
|
|
|
FileHelpers.del_dirs(src_path)
|
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
@staticmethod
|
|
|
|
def move_file(src_path, dest_path):
|
|
|
|
FileHelpers.copy_file(src_path, dest_path)
|
|
|
|
FileHelpers.del_file(src_path)
|
2022-03-01 01:02:30 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2022-08-27 16:05:13 +00:00
|
|
|
def make_archive(path_to_destination, path_to_zip, comment=""):
|
2022-03-01 01:02:30 +00:00
|
|
|
# create a ZipFile object
|
2022-03-23 02:50:12 +00:00
|
|
|
path_to_destination += ".zip"
|
2022-04-14 02:10:25 +00:00
|
|
|
with ZipFile(path_to_destination, "w") as zip_file:
|
2022-08-27 16:05:13 +00:00
|
|
|
zip_file.comment = bytes(
|
|
|
|
comment, "utf-8"
|
|
|
|
) # comments over 65535 bytes will be truncated
|
2022-03-01 01:02:30 +00:00
|
|
|
for root, _dirs, files in os.walk(path_to_zip, topdown=True):
|
|
|
|
ziproot = path_to_zip
|
|
|
|
for file in files:
|
|
|
|
try:
|
|
|
|
logger.info(f"backing up: {os.path.join(root, file)}")
|
|
|
|
if os.name == "nt":
|
2022-04-14 02:10:25 +00:00
|
|
|
zip_file.write(
|
2022-03-23 02:50:12 +00:00
|
|
|
os.path.join(root, file),
|
|
|
|
os.path.join(root.replace(ziproot, ""), file),
|
|
|
|
)
|
2022-03-01 01:02:30 +00:00
|
|
|
else:
|
2022-04-14 02:10:25 +00:00
|
|
|
zip_file.write(
|
2022-03-23 02:50:12 +00:00
|
|
|
os.path.join(root, file),
|
|
|
|
os.path.join(root.replace(ziproot, "/"), file),
|
|
|
|
)
|
2022-03-01 01:02:30 +00:00
|
|
|
|
|
|
|
except Exception as e:
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.warning(
|
2022-03-23 06:06:13 +00:00
|
|
|
f"Error backing up: {os.path.join(root, file)}!"
|
|
|
|
f" - Error was: {e}"
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-03-01 01:02:30 +00:00
|
|
|
return True
|
|
|
|
|
2022-03-02 16:34:26 +00:00
|
|
|
@staticmethod
|
2022-08-26 14:56:31 +00:00
|
|
|
def make_compressed_archive(path_to_destination, path_to_zip, comment=""):
|
2022-03-02 16:34:26 +00:00
|
|
|
# create a ZipFile object
|
2022-03-23 02:50:12 +00:00
|
|
|
path_to_destination += ".zip"
|
2022-04-14 02:10:25 +00:00
|
|
|
with ZipFile(path_to_destination, "w", ZIP_DEFLATED) as zip_file:
|
2022-08-26 14:56:31 +00:00
|
|
|
zip_file.comment = bytes(
|
|
|
|
comment, "utf-8"
|
|
|
|
) # comments over 65535 bytes will be truncated
|
2022-03-02 16:34:26 +00:00
|
|
|
for root, _dirs, files in os.walk(path_to_zip, topdown=True):
|
|
|
|
ziproot = path_to_zip
|
|
|
|
for file in files:
|
|
|
|
try:
|
2022-07-06 01:35:43 +00:00
|
|
|
logger.info(f"packaging: {os.path.join(root, file)}")
|
2022-03-02 16:34:26 +00:00
|
|
|
if os.name == "nt":
|
2022-04-14 02:10:25 +00:00
|
|
|
zip_file.write(
|
2022-03-23 02:50:12 +00:00
|
|
|
os.path.join(root, file),
|
|
|
|
os.path.join(root.replace(ziproot, ""), file),
|
|
|
|
)
|
2022-03-02 16:34:26 +00:00
|
|
|
else:
|
2022-04-14 02:10:25 +00:00
|
|
|
zip_file.write(
|
2022-03-23 02:50:12 +00:00
|
|
|
os.path.join(root, file),
|
|
|
|
os.path.join(root.replace(ziproot, "/"), file),
|
|
|
|
)
|
2022-03-02 16:34:26 +00:00
|
|
|
|
|
|
|
except Exception as e:
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.warning(
|
2022-07-06 01:35:43 +00:00
|
|
|
f"Error packaging: {os.path.join(root, file)}!"
|
2022-03-23 06:06:13 +00:00
|
|
|
f" - Error was: {e}"
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-03-02 16:34:26 +00:00
|
|
|
|
2022-04-11 10:08:36 +00:00
|
|
|
return True
|
2022-06-20 19:26:21 +00:00
|
|
|
|
|
|
|
def make_compressed_backup(
|
2022-08-27 16:05:13 +00:00
|
|
|
self, path_to_destination, path_to_zip, excluded_dirs, server_id, comment=""
|
2022-06-20 19:26:21 +00:00
|
|
|
):
|
|
|
|
# create a ZipFile object
|
|
|
|
path_to_destination += ".zip"
|
|
|
|
ex_replace = [p.replace("\\", "/") for p in excluded_dirs]
|
|
|
|
total_bytes = 0
|
|
|
|
dir_bytes = Helpers.get_dir_size(path_to_zip)
|
|
|
|
results = {
|
|
|
|
"percent": 0,
|
|
|
|
"total_files": self.helper.human_readable_file_size(dir_bytes),
|
|
|
|
}
|
2023-08-09 21:47:53 +00:00
|
|
|
WebSocketManager().broadcast_page_params(
|
2022-06-20 19:26:21 +00:00
|
|
|
"/panel/server_detail",
|
|
|
|
{"id": str(server_id)},
|
|
|
|
"backup_status",
|
|
|
|
results,
|
|
|
|
)
|
|
|
|
with ZipFile(path_to_destination, "w", ZIP_DEFLATED) as zip_file:
|
2022-08-27 16:05:13 +00:00
|
|
|
zip_file.comment = bytes(
|
|
|
|
comment, "utf-8"
|
|
|
|
) # comments over 65535 bytes will be truncated
|
2022-06-20 19:26:21 +00:00
|
|
|
for root, dirs, files in os.walk(path_to_zip, topdown=True):
|
|
|
|
for l_dir in dirs:
|
|
|
|
if str(os.path.join(root, l_dir)).replace("\\", "/") in ex_replace:
|
|
|
|
dirs.remove(l_dir)
|
|
|
|
ziproot = path_to_zip
|
|
|
|
for file in files:
|
|
|
|
if (
|
|
|
|
str(os.path.join(root, file)).replace("\\", "/")
|
|
|
|
not in ex_replace
|
|
|
|
and file != "crafty.sqlite"
|
|
|
|
):
|
|
|
|
try:
|
|
|
|
logger.info(f"backing up: {os.path.join(root, file)}")
|
|
|
|
if os.name == "nt":
|
|
|
|
zip_file.write(
|
|
|
|
os.path.join(root, file),
|
|
|
|
os.path.join(root.replace(ziproot, ""), file),
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
zip_file.write(
|
|
|
|
os.path.join(root, file),
|
|
|
|
os.path.join(root.replace(ziproot, "/"), file),
|
|
|
|
)
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
logger.warning(
|
|
|
|
f"Error backing up: {os.path.join(root, file)}!"
|
|
|
|
f" - Error was: {e}"
|
|
|
|
)
|
|
|
|
total_bytes += os.path.getsize(os.path.join(root, file))
|
|
|
|
percent = round((total_bytes / dir_bytes) * 100, 2)
|
|
|
|
results = {
|
|
|
|
"percent": percent,
|
|
|
|
"total_files": self.helper.human_readable_file_size(dir_bytes),
|
|
|
|
}
|
2023-08-09 21:47:53 +00:00
|
|
|
WebSocketManager().broadcast_page_params(
|
2022-06-20 19:26:21 +00:00
|
|
|
"/panel/server_detail",
|
|
|
|
{"id": str(server_id)},
|
|
|
|
"backup_status",
|
|
|
|
results,
|
|
|
|
)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2022-08-27 16:05:13 +00:00
|
|
|
def make_backup(
|
|
|
|
self, path_to_destination, path_to_zip, excluded_dirs, server_id, comment=""
|
|
|
|
):
|
2022-06-20 19:26:21 +00:00
|
|
|
# create a ZipFile object
|
|
|
|
path_to_destination += ".zip"
|
|
|
|
ex_replace = [p.replace("\\", "/") for p in excluded_dirs]
|
|
|
|
total_bytes = 0
|
|
|
|
dir_bytes = Helpers.get_dir_size(path_to_zip)
|
|
|
|
results = {
|
|
|
|
"percent": 0,
|
|
|
|
"total_files": self.helper.human_readable_file_size(dir_bytes),
|
|
|
|
}
|
2023-08-09 21:47:53 +00:00
|
|
|
WebSocketManager().broadcast_page_params(
|
2022-06-20 19:26:21 +00:00
|
|
|
"/panel/server_detail",
|
|
|
|
{"id": str(server_id)},
|
|
|
|
"backup_status",
|
|
|
|
results,
|
|
|
|
)
|
|
|
|
with ZipFile(path_to_destination, "w") as zip_file:
|
2022-08-27 16:05:13 +00:00
|
|
|
zip_file.comment = bytes(
|
|
|
|
comment, "utf-8"
|
|
|
|
) # comments over 65535 bytes will be truncated
|
2022-06-20 19:26:21 +00:00
|
|
|
for root, dirs, files in os.walk(path_to_zip, topdown=True):
|
2022-09-07 20:51:41 +00:00
|
|
|
for l_dir in dirs[:]:
|
2022-09-07 20:52:27 +00:00
|
|
|
# make all paths in exclusions a unix style slash
|
|
|
|
# to match directories.
|
2022-06-20 19:26:21 +00:00
|
|
|
if str(os.path.join(root, l_dir)).replace("\\", "/") in ex_replace:
|
|
|
|
dirs.remove(l_dir)
|
|
|
|
ziproot = path_to_zip
|
2022-09-07 20:01:42 +00:00
|
|
|
# iterate through list of files
|
2022-06-20 19:26:21 +00:00
|
|
|
for file in files:
|
2022-09-07 20:52:27 +00:00
|
|
|
# check if file/dir is in exclusions list.
|
|
|
|
# Only proceed if not exluded.
|
2022-06-20 19:26:21 +00:00
|
|
|
if (
|
|
|
|
str(os.path.join(root, file)).replace("\\", "/")
|
|
|
|
not in ex_replace
|
|
|
|
and file != "crafty.sqlite"
|
|
|
|
):
|
|
|
|
try:
|
2022-09-07 20:01:42 +00:00
|
|
|
logger.debug(f"backing up: {os.path.join(root, file)}")
|
|
|
|
# add trailing slash to zip root dir if not windows.
|
2022-06-20 19:26:21 +00:00
|
|
|
if os.name == "nt":
|
|
|
|
zip_file.write(
|
|
|
|
os.path.join(root, file),
|
|
|
|
os.path.join(root.replace(ziproot, ""), file),
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
zip_file.write(
|
|
|
|
os.path.join(root, file),
|
|
|
|
os.path.join(root.replace(ziproot, "/"), file),
|
|
|
|
)
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
logger.warning(
|
|
|
|
f"Error backing up: {os.path.join(root, file)}!"
|
|
|
|
f" - Error was: {e}"
|
|
|
|
)
|
2022-09-07 20:01:42 +00:00
|
|
|
# debug logging for exlusions list
|
|
|
|
else:
|
|
|
|
logger.debug(f"Found {file} in exclusion list. Skipping...")
|
|
|
|
|
|
|
|
# add current file bytes to total bytes.
|
2022-06-20 19:26:21 +00:00
|
|
|
total_bytes += os.path.getsize(os.path.join(root, file))
|
2022-09-07 20:01:42 +00:00
|
|
|
# calcualte percentage based off total size and current archive size
|
2022-06-20 19:26:21 +00:00
|
|
|
percent = round((total_bytes / dir_bytes) * 100, 2)
|
2022-09-07 20:01:42 +00:00
|
|
|
# package results
|
2022-06-20 19:26:21 +00:00
|
|
|
results = {
|
|
|
|
"percent": percent,
|
|
|
|
"total_files": self.helper.human_readable_file_size(dir_bytes),
|
|
|
|
}
|
2022-09-07 20:01:42 +00:00
|
|
|
# send status results to page.
|
2023-08-09 21:47:53 +00:00
|
|
|
WebSocketManager().broadcast_page_params(
|
2022-06-20 19:26:21 +00:00
|
|
|
"/panel/server_detail",
|
|
|
|
{"id": str(server_id)},
|
|
|
|
"backup_status",
|
|
|
|
results,
|
|
|
|
)
|
|
|
|
return True
|
|
|
|
|
|
|
|
@staticmethod
|
2023-02-25 21:15:50 +00:00
|
|
|
def unzip_file(zip_path, server_update=False):
|
|
|
|
ignored_names = ["server.properties", "permissions.json", "allowlist.json"]
|
2023-02-25 20:40:38 +00:00
|
|
|
# Get directory without zipfile name
|
|
|
|
new_dir = pathlib.Path(zip_path).parents[0]
|
|
|
|
# make sure we're able to access the zip file
|
2022-06-20 19:26:21 +00:00
|
|
|
if Helpers.check_file_perms(zip_path) and os.path.isfile(zip_path):
|
2023-02-25 20:40:38 +00:00
|
|
|
# make sure the directory we're unzipping this to exists
|
2022-06-20 19:26:21 +00:00
|
|
|
Helpers.ensure_dir_exists(new_dir)
|
2023-02-25 20:40:38 +00:00
|
|
|
# we'll make a temporary directory to unzip this to.
|
2022-06-20 19:26:21 +00:00
|
|
|
temp_dir = tempfile.mkdtemp()
|
|
|
|
try:
|
|
|
|
with zipfile.ZipFile(zip_path, "r") as zip_ref:
|
2023-02-25 20:40:38 +00:00
|
|
|
# we'll extract this to the temp dir using zipfile module
|
2022-06-20 19:26:21 +00:00
|
|
|
zip_ref.extractall(temp_dir)
|
2023-02-25 21:15:50 +00:00
|
|
|
# we'll iterate through the top level directory moving everything
|
|
|
|
# out of the temp directory and into it's final home.
|
|
|
|
for item in os.listdir(temp_dir):
|
|
|
|
# if the file is one of our ignored names we'll skip it
|
|
|
|
if item in ignored_names and server_update:
|
|
|
|
continue
|
|
|
|
# we handle files and dirs differently or we'll crash out.
|
|
|
|
if os.path.isdir(os.path.join(temp_dir, item)):
|
|
|
|
try:
|
|
|
|
FileHelpers.move_dir_exist(
|
|
|
|
os.path.join(temp_dir, item),
|
|
|
|
os.path.join(new_dir, item),
|
|
|
|
)
|
|
|
|
except Exception as ex:
|
|
|
|
logger.error(f"ERROR IN ZIP IMPORT: {ex}")
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
FileHelpers.move_file(
|
|
|
|
os.path.join(temp_dir, item),
|
|
|
|
os.path.join(new_dir, item),
|
|
|
|
)
|
|
|
|
except Exception as ex:
|
|
|
|
logger.error(f"ERROR IN ZIP IMPORT: {ex}")
|
2022-06-20 19:26:21 +00:00
|
|
|
except Exception as ex:
|
|
|
|
Console.error(ex)
|
|
|
|
else:
|
|
|
|
return "false"
|
|
|
|
return
|
2023-08-09 21:47:53 +00:00
|
|
|
|
2023-09-01 01:35:30 +00:00
|
|
|
def unzip_server(self, zip_path, user_id):
|
2023-08-09 21:47:53 +00:00
|
|
|
if Helpers.check_file_perms(zip_path):
|
|
|
|
temp_dir = tempfile.mkdtemp()
|
|
|
|
with zipfile.ZipFile(zip_path, "r") as zip_ref:
|
|
|
|
# extracts archive to temp directory
|
|
|
|
zip_ref.extractall(temp_dir)
|
|
|
|
if user_id:
|
2023-09-01 01:35:30 +00:00
|
|
|
return temp_dir
|