2019-01-16 03:17:33 +01:00
|
|
|
import os
|
2019-04-13 01:48:34 +02:00
|
|
|
import re
|
2019-01-16 03:17:33 +01:00
|
|
|
import tempfile
|
|
|
|
from argparse import ArgumentParser, RawTextHelpFormatter
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from django.conf import settings
|
2019-02-14 02:03:26 +01:00
|
|
|
from django.db import connection
|
2019-01-16 03:17:33 +01:00
|
|
|
from django.utils.timezone import now as timezone_now
|
|
|
|
|
2020-01-14 21:59:46 +01:00
|
|
|
from scripts.lib.zulip_tools import TIMESTAMP_FORMAT, parse_os_release, run
|
2019-02-14 02:03:26 +01:00
|
|
|
from version import ZULIP_VERSION
|
2019-01-16 03:17:33 +01:00
|
|
|
from zerver.lib.management import ZulipBaseCommand
|
2019-02-14 02:03:26 +01:00
|
|
|
from zerver.logging_handlers import try_git_describe
|
2019-01-16 03:17:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Command(ZulipBaseCommand):
|
|
|
|
# Fix support for multi-line usage strings
|
|
|
|
def create_parser(self, *args: Any, **kwargs: Any) -> ArgumentParser:
|
|
|
|
parser = super().create_parser(*args, **kwargs)
|
|
|
|
parser.formatter_class = RawTextHelpFormatter
|
|
|
|
return parser
|
|
|
|
|
|
|
|
def add_arguments(self, parser: ArgumentParser) -> None:
|
|
|
|
parser.add_argument(
|
2019-04-17 10:00:07 +02:00
|
|
|
"--output", default=None, nargs="?", help="Filename of output tarball"
|
2019-01-16 03:17:33 +01:00
|
|
|
)
|
2019-04-17 09:16:18 +02:00
|
|
|
parser.add_argument("--skip-db", action='store_true', help="Skip database backup")
|
2019-04-17 09:50:11 +02:00
|
|
|
parser.add_argument("--skip-uploads", action='store_true', help="Skip uploads backup")
|
2019-01-16 03:17:33 +01:00
|
|
|
|
|
|
|
def handle(self, *args: Any, **options: Any) -> None:
|
|
|
|
timestamp = timezone_now().strftime(TIMESTAMP_FORMAT)
|
|
|
|
with tempfile.TemporaryDirectory(
|
2020-06-10 06:41:04 +02:00
|
|
|
prefix=f"zulip-backup-{timestamp}-"
|
2019-01-16 03:17:33 +01:00
|
|
|
) as tmp:
|
|
|
|
os.mkdir(os.path.join(tmp, "zulip-backup"))
|
|
|
|
members = []
|
2019-04-13 01:48:34 +02:00
|
|
|
paths = []
|
2019-01-16 03:17:33 +01:00
|
|
|
|
2019-02-14 02:03:26 +01:00
|
|
|
with open(os.path.join(tmp, "zulip-backup", "zulip-version"), "w") as f:
|
|
|
|
print(ZULIP_VERSION, file=f)
|
|
|
|
git = try_git_describe()
|
|
|
|
if git:
|
|
|
|
print(git, file=f)
|
|
|
|
members.append("zulip-backup/zulip-version")
|
|
|
|
|
|
|
|
with open(os.path.join(tmp, "zulip-backup", "os-version"), "w") as f:
|
|
|
|
print(
|
2019-08-25 01:23:14 +02:00
|
|
|
"{ID} {VERSION_ID}".format(**parse_os_release()),
|
2019-02-14 02:03:26 +01:00
|
|
|
file=f,
|
|
|
|
)
|
|
|
|
members.append("zulip-backup/os-version")
|
|
|
|
|
|
|
|
with open(os.path.join(tmp, "zulip-backup", "postgres-version"), "w") as f:
|
|
|
|
print(connection.pg_version, file=f)
|
|
|
|
members.append("zulip-backup/postgres-version")
|
|
|
|
|
2019-01-16 03:17:33 +01:00
|
|
|
if settings.DEVELOPMENT:
|
2019-04-13 01:48:34 +02:00
|
|
|
members.append(
|
|
|
|
os.path.join(settings.DEPLOY_ROOT, "zproject", "dev-secrets.conf")
|
|
|
|
)
|
|
|
|
paths.append(
|
|
|
|
("zproject", os.path.join(settings.DEPLOY_ROOT, "zproject"))
|
2019-01-16 03:17:33 +01:00
|
|
|
)
|
|
|
|
else:
|
2019-04-13 01:48:34 +02:00
|
|
|
members.append("/etc/zulip")
|
|
|
|
paths.append(("settings", "/etc/zulip"))
|
2019-01-16 03:17:33 +01:00
|
|
|
|
2019-04-17 09:16:18 +02:00
|
|
|
if not options['skip_db']:
|
2020-02-03 09:31:12 +01:00
|
|
|
pg_dump_command = [
|
|
|
|
"pg_dump",
|
|
|
|
"--format=directory",
|
|
|
|
"--file", os.path.join(tmp, "zulip-backup", "database"),
|
|
|
|
"--host", settings.DATABASES["default"]["HOST"],
|
|
|
|
"--port", settings.DATABASES["default"]["PORT"],
|
|
|
|
"--username", settings.DATABASES["default"]["USER"],
|
|
|
|
"--dbname", settings.DATABASES["default"]["NAME"],
|
|
|
|
"--no-password",
|
|
|
|
]
|
|
|
|
os.environ["PGPASSWORD"] = settings.DATABASES["default"]["PASSWORD"]
|
|
|
|
|
2019-04-17 09:16:18 +02:00
|
|
|
run(
|
2020-02-03 09:31:12 +01:00
|
|
|
pg_dump_command,
|
2019-04-17 09:16:18 +02:00
|
|
|
cwd=tmp,
|
|
|
|
)
|
|
|
|
members.append("zulip-backup/database")
|
2019-01-16 03:17:33 +01:00
|
|
|
|
2019-04-17 09:50:11 +02:00
|
|
|
if not options['skip_uploads'] and settings.LOCAL_UPLOADS_DIR is not None and os.path.exists(
|
2019-01-16 03:17:33 +01:00
|
|
|
os.path.join(settings.DEPLOY_ROOT, settings.LOCAL_UPLOADS_DIR)
|
|
|
|
):
|
2019-04-13 01:48:34 +02:00
|
|
|
members.append(
|
|
|
|
os.path.join(settings.DEPLOY_ROOT, settings.LOCAL_UPLOADS_DIR)
|
|
|
|
)
|
|
|
|
paths.append(
|
|
|
|
(
|
|
|
|
"uploads",
|
|
|
|
os.path.join(settings.DEPLOY_ROOT, settings.LOCAL_UPLOADS_DIR),
|
|
|
|
)
|
2019-01-16 03:17:33 +01:00
|
|
|
)
|
2019-04-13 01:48:34 +02:00
|
|
|
|
|
|
|
assert not any("|" in name or "|" in path for name, path in paths)
|
|
|
|
transform_args = [
|
|
|
|
r"--transform=s|^{}(/.*)?$|zulip-backup/{}\1|x".format(
|
|
|
|
re.escape(path), name.replace("\\", r"\\")
|
|
|
|
)
|
|
|
|
for name, path in paths
|
|
|
|
]
|
2019-01-16 03:17:33 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
if options["output"] is None:
|
|
|
|
tarball_path = tempfile.NamedTemporaryFile(
|
2020-06-10 06:41:04 +02:00
|
|
|
prefix=f"zulip-backup-{timestamp}-",
|
2019-01-16 03:17:33 +01:00
|
|
|
suffix=".tar.gz",
|
|
|
|
delete=False,
|
|
|
|
).name
|
|
|
|
else:
|
|
|
|
tarball_path = options["output"]
|
|
|
|
|
2019-04-13 01:48:34 +02:00
|
|
|
run(
|
|
|
|
["tar", "-C", tmp, "-cPzf", tarball_path]
|
|
|
|
+ transform_args
|
|
|
|
+ ["--"]
|
|
|
|
+ members
|
|
|
|
)
|
2020-06-10 06:41:04 +02:00
|
|
|
print(f"Backup tarball written to {tarball_path}")
|
2019-01-16 03:17:33 +01:00
|
|
|
except BaseException:
|
|
|
|
if options["output"] is None:
|
|
|
|
os.unlink(tarball_path)
|
|
|
|
raise
|