Stage website files from DirectAdmin backups
This commit is contained in:
1 parent
c6310b1525
commit
2b4895df4f
5 files changed
+322
-13
No files matched your search
+42
-12
@@ -15,6 +15,7 @@ from .mail_node import MailboxSpec
|
||||
MAX_METADATA_SIZE = 8 * 1024 * 1024
|
||||
ZSTD_MAGIC = b"\x28\xb5\x2f\xfd"
|
||||
HOME_ARCHIVES = {"home.tar", "home.tar.gz", "home.tar.zst"}
|
||||
DOCUMENT_ROOTS = ("public_html", "private_html")
|
||||
|
||||
|
||||
class BackupTarInfo(tarfile.TarInfo):
|
||||
@@ -64,10 +65,7 @@ class BackupInventory:
|
||||
return inventory
|
||||
|
||||
def visit(self, archive, member):
|
||||
path = PurePosixPath(member.name)
|
||||
parts = path.parts
|
||||
if path.is_absolute() or ".." in parts:
|
||||
raise ValueError("The archive contains an unsafe member path.")
|
||||
parts = member_path(member)
|
||||
if not member.isreg():
|
||||
if not member.isdir():
|
||||
self.skipped_non_regular_entries += 1
|
||||
@@ -213,22 +211,40 @@ def check_tar_padding(archive):
|
||||
raise ValueError("The tar archive is missing its end marker.")
|
||||
|
||||
|
||||
def inspect_backup(path):
|
||||
inventory = BackupInventory()
|
||||
def member_path(member):
|
||||
path = PurePosixPath(member.name)
|
||||
if path.is_absolute() or ".." in path.parts:
|
||||
raise ValueError("The archive contains an unsafe member path.")
|
||||
return path.parts
|
||||
|
||||
|
||||
@contextmanager
|
||||
def open_backup(path):
|
||||
try:
|
||||
with (
|
||||
archive_stream(path) as stream,
|
||||
tarfile.open(fileobj=stream, mode="r|", tarinfo=BackupTarInfo) as archive,
|
||||
):
|
||||
while (member := archive.next()) is not None:
|
||||
inventory.visit(archive, member)
|
||||
# The reader only walks forward and does not need tarfile's member cache.
|
||||
archive.members.clear()
|
||||
check_tar_padding(archive)
|
||||
yield archive
|
||||
except (EOFError, tarfile.TarError, zlib.error, gzip.BadGzipFile):
|
||||
raise ValueError(
|
||||
"The backup is not a readable or complete tar archive."
|
||||
) from None
|
||||
|
||||
|
||||
def backup_members(archive):
|
||||
while (member := archive.next()) is not None:
|
||||
yield member
|
||||
# The reader only walks forward and does not need tarfile's member cache.
|
||||
archive.members.clear()
|
||||
check_tar_padding(archive)
|
||||
|
||||
|
||||
def inspect_backup(path):
|
||||
inventory = BackupInventory()
|
||||
with open_backup(path) as archive:
|
||||
for member in backup_members(archive):
|
||||
inventory.visit(archive, member)
|
||||
return inventory.report()
|
||||
|
||||
|
||||
@@ -239,9 +255,23 @@ def main(argv=None):
|
||||
"inspect", help="Inventory a DirectAdmin account backup."
|
||||
)
|
||||
inspect.add_argument("backup", type=Path)
|
||||
stage = commands.add_parser(
|
||||
"stage-website", help="Stage one DirectAdmin website in a new local directory."
|
||||
)
|
||||
stage.add_argument("backup", type=Path)
|
||||
stage.add_argument("domain")
|
||||
stage.add_argument("destination", type=Path)
|
||||
stage.add_argument("--source-root", choices=DOCUMENT_ROOTS, default="public_html")
|
||||
args = parser.parse_args(argv)
|
||||
try:
|
||||
report = inspect_backup(args.backup)
|
||||
if args.command == "stage-website":
|
||||
from .directadmin_website import stage_website
|
||||
|
||||
report = stage_website(
|
||||
args.backup, args.domain, args.destination, source_root=args.source_root
|
||||
)
|
||||
else:
|
||||
report = inspect_backup(args.backup)
|
||||
except (OSError, ValueError) as error:
|
||||
parser.exit(1, f"coulobre-directadmin: {error}\n")
|
||||
print(json.dumps(report, indent=2))
|
||||
|
||||
Reference in new issue
Block a user