2019-01-16 03:17:33 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import os
|
2019-04-13 01:48:34 +02:00
|
|
|
import re
|
2019-01-16 03:17:33 +01:00
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import tempfile
|
|
|
|
|
2019-04-15 22:06:02 +02:00
|
|
|
if False:
|
|
|
|
from typing import IO
|
|
|
|
|
2019-01-16 03:17:33 +01:00
|
|
|
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
sys.path.append(BASE_DIR)
|
|
|
|
from scripts.lib.zulip_tools import su_to_zulip, run
|
|
|
|
|
|
|
|
POSTGRES_USER = "postgres"
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument("tarball", help="Filename of input tarball")
|
|
|
|
|
2019-04-15 22:06:02 +02:00
|
|
|
|
|
|
|
def restore_backup(tarball_file):
|
|
|
|
# type: (IO[bytes]) -> None
|
|
|
|
|
2019-01-16 03:17:33 +01:00
|
|
|
su_to_zulip(save_suid=True)
|
|
|
|
|
|
|
|
import scripts.lib.setup_path_on_import
|
|
|
|
|
|
|
|
# First, we unpack the /etc/zulip configuration, so we know how
|
|
|
|
# this server is supposed to be configured (and can import
|
|
|
|
# /etc/zulip/settings.py via `from zproject import settings`,
|
|
|
|
# next). Ignore errors if zulip-backup/settings is not present
|
|
|
|
# (E.g. because this is a development backup).
|
2019-04-15 22:06:02 +02:00
|
|
|
tarball_file.seek(0, 0)
|
2019-01-16 03:17:33 +01:00
|
|
|
subprocess.call(
|
|
|
|
[
|
|
|
|
"tar",
|
|
|
|
"-C",
|
|
|
|
"/etc/zulip",
|
|
|
|
"--strip-components=2",
|
2019-04-15 22:06:02 +02:00
|
|
|
"-xz",
|
2019-01-16 03:17:33 +01:00
|
|
|
"zulip-backup/settings",
|
2019-04-15 22:06:02 +02:00
|
|
|
],
|
|
|
|
stdin=tarball_file,
|
2019-01-16 03:17:33 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
from zproject import settings
|
|
|
|
|
|
|
|
paths = [
|
|
|
|
("settings", "/etc/zulip"),
|
|
|
|
# zproject will only be present for development environment backups.
|
|
|
|
("zproject", os.path.join(settings.DEPLOY_ROOT, "zproject")),
|
|
|
|
]
|
2019-06-05 02:56:47 +02:00
|
|
|
if settings.LOCAL_UPLOADS_DIR is not None:
|
|
|
|
# We only need to restore LOCAL_UPLOADS_DIR if the system is
|
|
|
|
# configured to locally host uploads.
|
|
|
|
paths.append(("uploads", os.path.join(settings.DEPLOY_ROOT, settings.LOCAL_UPLOADS_DIR)))
|
2019-01-16 03:17:33 +01:00
|
|
|
|
|
|
|
with tempfile.TemporaryDirectory(prefix="zulip-restore-backup-") as tmp:
|
|
|
|
uid = os.getuid()
|
|
|
|
gid = os.getgid()
|
|
|
|
os.setresuid(0, 0, 0)
|
|
|
|
for name, path in paths:
|
|
|
|
os.makedirs(path, exist_ok=True)
|
|
|
|
os.chown(path, uid, gid)
|
|
|
|
os.setresuid(uid, uid, 0)
|
|
|
|
|
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(name), path.replace("\\", r"\\")
|
|
|
|
)
|
|
|
|
for name, path in paths
|
|
|
|
]
|
|
|
|
|
2019-01-16 03:17:33 +01:00
|
|
|
os.mkdir(os.path.join(tmp, "zulip-backup"))
|
2019-04-15 22:06:02 +02:00
|
|
|
tarball_file.seek(0, 0)
|
|
|
|
run(["tar", "-C", tmp] + transform_args + ["-xPz"], stdin=tarball_file)
|
2019-01-16 03:17:33 +01:00
|
|
|
|
2019-05-21 00:02:19 +02:00
|
|
|
# Now, extract the the database backup, destroy the old
|
|
|
|
# database, and create a new, empty database.
|
2019-01-16 03:17:33 +01:00
|
|
|
db_name = settings.DATABASES["default"]["NAME"]
|
|
|
|
assert isinstance(db_name, str)
|
|
|
|
db_dir = os.path.join(tmp, "zulip-backup", "database")
|
|
|
|
os.setresuid(0, 0, 0)
|
|
|
|
run(["chown", "-R", POSTGRES_USER, "--", tmp])
|
|
|
|
run(
|
|
|
|
[
|
|
|
|
os.path.join(
|
|
|
|
settings.DEPLOY_ROOT, "scripts", "setup", "terminate-psql-sessions"
|
|
|
|
),
|
|
|
|
"zulip",
|
|
|
|
"zulip",
|
|
|
|
"zulip_base",
|
|
|
|
]
|
|
|
|
)
|
2019-04-15 21:46:06 +02:00
|
|
|
as_postgres = ["su", "-s", "/usr/bin/env", "-", "--", POSTGRES_USER]
|
2019-01-16 03:17:33 +01:00
|
|
|
run(as_postgres + ["dropdb", "--if-exists", "--", db_name])
|
2019-04-02 01:04:05 +02:00
|
|
|
run(as_postgres + ["createdb", "-O", "zulip", "-T", "template0", "--", db_name])
|
2019-01-16 03:17:33 +01:00
|
|
|
|
|
|
|
if settings.PRODUCTION:
|
2019-06-07 01:35:52 +02:00
|
|
|
# If there is a local rabbitmq, we need to reconfigure it
|
|
|
|
# to ensure the rabbitmq password matches the value in the
|
|
|
|
# restored zulip-secrets.conf. We need to be careful to
|
|
|
|
# only do this if rabbitmq is configured to run locally on
|
|
|
|
# the system.
|
|
|
|
rabbitmq_host = subprocess.check_output(
|
|
|
|
[os.path.join(settings.DEPLOY_ROOT,
|
|
|
|
"scripts", "get-django-setting"),
|
|
|
|
"RABBITMQ_HOST"]).strip().decode("utf-8")
|
|
|
|
if rabbitmq_host in ["127.0.0.1", "::1", "localhost", "localhost6"]:
|
|
|
|
run([os.path.join(settings.DEPLOY_ROOT,
|
|
|
|
"scripts", "setup", "configure-rabbitmq")])
|
|
|
|
|
|
|
|
# In production, we also need to do a `zulip-puppet-apply`
|
|
|
|
# in order to apply any configuration from
|
|
|
|
# /etc/zulip/zulip.conf to this system, since it was
|
|
|
|
# originally installed without the restored copy of that
|
|
|
|
# file.
|
2019-01-16 03:17:33 +01:00
|
|
|
run(
|
|
|
|
[
|
|
|
|
os.path.join(settings.DEPLOY_ROOT, "scripts", "zulip-puppet-apply"),
|
|
|
|
"-f",
|
|
|
|
]
|
|
|
|
)
|
2019-05-21 00:02:19 +02:00
|
|
|
|
|
|
|
# Now, restore the the database backup using pg_restore. This
|
|
|
|
# needs to run after zulip-puppet-apply to ensure full-text
|
|
|
|
# search extensions are available and installed.
|
|
|
|
run(as_postgres + ["pg_restore", "-d", db_name, "--", db_dir])
|
|
|
|
run(["chown", "-R", str(uid), "--", tmp])
|
|
|
|
os.setresuid(uid, uid, 0)
|
|
|
|
|
|
|
|
if settings.PRODUCTION:
|
2019-01-16 03:17:33 +01:00
|
|
|
run(["supervisorctl", "restart", "all"])
|
|
|
|
|
|
|
|
run([os.path.join(settings.DEPLOY_ROOT, "scripts", "setup", "flush-memcached")])
|
2019-04-15 22:06:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
with open(args.tarball, "rb") as tarball_file:
|
|
|
|
restore_backup(tarball_file)
|