Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

export: Add --remove-files-while-archiving option.

Mateusz Mandera99b26a368e95
diff --git a/zerver/lib/export.py b/zerver/lib/export.py
index 4134dabd996..4348b3c94de 100644
--- a/zerver/lib/export.py
+++ b/zerver/lib/export.py
@@ -2555,6 +2555,7 @@ def do_export_realm(
export_type: int,
exportable_user_ids: set[int] | None = None,
export_as_active: bool | None = None,
+ remove_files_while_archiving: bool = False,
) -> tuple[str, dict[str, int | dict[str, int]]]:
response: TableData = {}
if exportable_user_ids is not None:
@@ -2658,14 +2659,18 @@ def do_export_realm(
logging.info("Compressing tarball...")
tarball_path = output_dir.rstrip("/") + ".tar.gz"
- subprocess.check_call(
- [
- "tar",
- f"-czf{tarball_path}",
- f"-C{os.path.dirname(output_dir)}",
- os.path.basename(output_dir),
- ]
- )
+ tar_args = [
+ "tar",
+ f"-czf{tarball_path}",
+ f"-C{os.path.dirname(output_dir)}",
+ os.path.basename(output_dir),
+ ]
+ if remove_files_while_archiving:
+ # Deletes each file, and finally output_dir itself, once it
+ # has been added to the tarball, so that we don't need disk
+ # space for two copies of the export.
+ tar_args.append("--remove-files")
+ subprocess.check_call(tar_args)
return tarball_path, stats
@@ -3120,6 +3125,7 @@ def export_realm_wrapper(
upload: bool,
percent_callback: Callable[[Any], None] | None = None,
export_as_active: bool | None = None,
+ remove_files_while_archiving: bool = False,
) -> str | None:
try:
export_row.status = RealmExport.STARTED
@@ -3137,6 +3143,7 @@ def export_realm_wrapper(
export_type=export_row.type,
export_as_active=export_as_active,
exportable_user_ids=exportable_user_ids,
+ remove_files_while_archiving=remove_files_while_archiving,
)
RealmAuditLog.objects.create(
@@ -3147,7 +3154,8 @@ def export_realm_wrapper(
extra_data={"realm_export_id": export_row.id},
)
- shutil.rmtree(output_dir)
+ if not remove_files_while_archiving:
+ shutil.rmtree(output_dir)
print(f"Tarball written to {tarball_path}")
print("Calculating SHA-256 checksum of tarball...")
diff --git a/zerver/management/commands/export.py b/zerver/management/commands/export.py
index 0f8594d6745..16cce794f58 100644
--- a/zerver/management/commands/export.py
+++ b/zerver/management/commands/export.py
@@ -107,6 +107,15 @@ class Command(ZulipBaseCommand):
action="store_true",
help="Whether to upload resulting tarball to s3 or LOCAL_UPLOADS_DIR",
)
+ parser.add_argument(
+ "--remove-files-while-archiving",
+ action="store_true",
+ help=(
+ "Delete exported files as they are added to the tarball, rather than "
+ "afterwards, to avoid needing disk space for two copies of the export. "
+ "If archiving fails, the exported files are lost"
+ ),
+ )
self.add_realm_args(parser, required=True)
@override
@@ -187,4 +196,5 @@ class Command(ZulipBaseCommand):
upload=options["upload"],
percent_callback=percent_callback,
export_as_active=True if options["deactivate_realm"] else None,
+ remove_files_while_archiving=options["remove_files_while_archiving"],
)
diff --git a/zerver/tests/test_management_commands.py b/zerver/tests/test_management_commands.py
index 61e6613dcf2..52664098ad0 100644
--- a/zerver/tests/test_management_commands.py
+++ b/zerver/tests/test_management_commands.py
@@ -559,6 +559,7 @@ class TestExport(ZulipTestCase):
percent_callback=mock.ANY,
upload=False,
export_as_active=None,
+ remove_files_while_archiving=False,
)
self.assertEqual(
diff --git a/zerver/tests/test_realm_export.py b/zerver/tests/test_realm_export.py
index 1ec58a22470..d683d7a1ede 100644
--- a/zerver/tests/test_realm_export.py
+++ b/zerver/tests/test_realm_export.py
@@ -136,9 +136,11 @@ class RealmExportTest(ZulipTestCase):
export_type: int,
exportable_user_ids: set[int] | None = None,
export_as_active: bool | None = None,
+ remove_files_while_archiving: bool = False,
) -> tuple[str, dict[str, int | dict[str, int]]]:
self.assertEqual(realm, admin.realm)
self.assertEqual(export_type, RealmExport.EXPORT_PUBLIC)
+ self.assertFalse(remove_files_while_archiving)
self.assertTrue(
os.path.basename(output_dir).startswith("zulip-export-zulip-2026-05-25-10-11-16-")
)