We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Backup restore locks projects and shows no existing components
### Describe the issue

I was testing the borg backup feature in weblate and upon restoring the backup I noticed that the projects had the label that indicated that the project is locked (See Image). 

I see that the information is all there in the backup but it is not shown in the app. For example, after opening the projects, no components are found.

I have found little to no information online regarding such an issue.





### I already tried

- [X] I've read and searched [the documentation](https://docs.weblate.org/).
- [X] I've searched for similar filed issues in this repository.

### Steps to reproduce the behavior

The issue happened after following the directions for a borg backup and restore in the weblate documentation.

### Expected behavior

_No response_

### Screenshots

![Screenshot 2024-01-25 091318](https://github.com/WeblateOrg/weblate/assets/157685480/c19d28f1-b7f7-42e4-bb2d-6ec617733388)


### Exception traceback

_No response_

### How do you run Weblate?

Docker container

### Weblate versions

 weblate/weblate:edge
Hash: sha256:780310018d21eba2ef5e06b66acab7bf2e86b3ed1778640707ad47d0525cb213.

### Weblate deploy checks

_No response_

### Additional context

_No response_

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `weblate/utils/tasks.py`
Content:
```
1 # Copyright © Michal Čihař <michal@weblate.org>
2 #
3 # SPDX-License-Identifier: GPL-3.0-or-later
4 
5 import gzip
6 import os
7 import shutil
8 import subprocess
9 import sys
10 import time
11 from importlib import import_module
12 from shutil import copyfile
13 
14 from celery.schedules import crontab
15 from django.conf import settings
16 from django.core.cache import cache
17 from django.core.management.commands import diffsettings
18 from ruamel.yaml import YAML
19 
20 import weblate.utils.version
21 from weblate.formats.models import FILE_FORMATS
22 from weblate.logger import LOGGER
23 from weblate.machinery.models import MACHINERY
24 from weblate.trans.models import Component, Translation
25 from weblate.trans.util import get_clean_env
26 from weblate.utils.backup import backup_lock
27 from weblate.utils.celery import app
28 from weblate.utils.data import data_dir
29 from weblate.utils.db import using_postgresql
30 from weblate.utils.errors import add_breadcrumb, report_error
31 from weblate.utils.lock import WeblateLockTimeoutError
32 from weblate.vcs.models import VCS_REGISTRY
33 
34 
35 @app.task(trail=False)
36 def ping():
37     return {
38         "version": weblate.utils.version.GIT_VERSION,
39         "vcs": sorted(VCS_REGISTRY.keys()),
40         "formats": sorted(FILE_FORMATS.keys()),
41         "mt_services": sorted(MACHINERY.keys()),
42         "encoding": [sys.getfilesystemencoding(), sys.getdefaultencoding()],
43         "uid": os.getuid(),
44     }
45 
46 
47 @app.task(trail=False)
48 def heartbeat():
49     cache.set("celery_loaded", time.time())
50     cache.set("celery_heartbeat", time.time())
51     cache.set(
52         "celery_encoding", [sys.getfilesystemencoding(), sys.getdefaultencoding()]
53     )
54 
55 
56 @app.task(trail=False, autoretry_for=(WeblateLockTimeoutError,))
57 def settings_backup():
58     with backup_lock():
59         # Expand settings in case it contains non-trivial code
60         command = diffsettings.Command()
61         kwargs = {"default": None, "all": False, "output": "hash"}
62         with open(data_dir("backups", "settings-expanded.py"), "w") as handle:
63             handle.write(command.handle(**kwargs))
64 
65         # Backup original settings
66         if settings.SETTINGS_MODULE:
67             settings_mod = import_module(settings.SETTINGS_MODULE)
68             copyfile(settings_mod.__file__, data_dir("backups", "settings.py"))
69 
70         # Backup environment (to make restoring Docker easier)
71         with open(data_dir("backups", "environment.yml"), "w") as handle:
72             yaml = YAML()
73             yaml.dump(dict(os.environ), handle)
74 
75 
76 @app.task(trail=False)
77 def update_translation_stats_parents(pk: int):
78     translation = Translation.objects.get(pk=pk)
79     translation.stats.update_parents()
80 
81 
82 @app.task(trail=False)
83 def update_language_stats_parents(pk: int):
84     component = Component.objects.get(pk=pk)
85     component.stats.update_language_stats_parents()
86 
87 
88 @app.task(trail=False, autoretry_for=(WeblateLockTimeoutError,))
89 def database_backup():
90     if settings.DATABASE_BACKUP == "none":
91         return
92     with backup_lock():
93         database = settings.DATABASES["default"]
94         env = get_clean_env()
95         compress = settings.DATABASE_BACKUP == "compressed"
96 
97         out_compressed = data_dir("backups", "database.sql.gz")
98         out_text = data_dir("backups", "database.sql")
99 
100         if using_postgresql():
101             cmd = [
102                 "pg_dump",
103                 # Superuser only, crashes on Alibaba Cloud Database PolarDB
104                 "--no-subscriptions",
105                 "--dbname",
106                 database["NAME"],
107             ]
108 
109             if database["HOST"]:
110                 cmd.extend(["--host", database["HOST"]])
111             if database["PORT"]:
112                 cmd.extend(["--port", database["PORT"]])
113             if database["USER"]:
114                 cmd.extend(["--username", database["USER"]])
115             if settings.DATABASE_BACKUP == "compressed":
116                 cmd.extend(["--file", out_compressed])
117                 cmd.extend(["--compress", "6"])
118                 compress = False
119             else:
120                 cmd.extend(["--file", out_text])
121 
122             env["PGPASSWORD"] = database["PASSWORD"]
123         else:
124             cmd = [
125                 "mysqldump",
126                 "--result-file",
127                 out_text,
128                 "--single-transaction",
129                 "--skip-lock-tables",
130             ]
131 
132             if database["HOST"]:
133                 cmd.extend(["--host", database["HOST"]])
134             if database["PORT"]:
135                 cmd.extend(["--port", database["PORT"]])
136             if database["USER"]:
137                 cmd.extend(["--user", database["USER"]])
138 
139             cmd.extend(["--databases", database["NAME"]])
140 
141             env["MYSQL_PWD"] = database["PASSWORD"]
142 
143         try:
144             subprocess.run(
145                 cmd,
146                 env=env,
147                 capture_output=True,
148                 stdin=subprocess.DEVNULL,
149                 check=True,
150                 text=True,
151             )
152         except subprocess.CalledProcessError as error:
153             add_breadcrumb(
154                 category="backup",
155                 message="database dump output",
156                 stdout=error.stdout,
157                 stderr=error.stderr,
158             )
159             LOGGER.error("failed database backup: %s", error.stderr)
160             report_error()
161             raise
162 
163         if compress:
164             with open(out_text, "rb") as f_in, gzip.open(out_compressed, "wb") as f_out:
165                 shutil.copyfileobj(f_in, f_out)
166             os.unlink(out_text)
167 
168 
169 @app.on_after_finalize.connect
170 def setup_periodic_tasks(sender, **kwargs):
171     cache.set("celery_loaded", time.time())
172     sender.add_periodic_task(
173         crontab(hour=1, minute=0), settings_backup.s(), name="settings-backup"
174     )
175     sender.add_periodic_task(
176         crontab(hour=1, minute=30), database_backup.s(), name="database-backup"
177     )
178     sender.add_periodic_task(60, heartbeat.s(), name="heartbeat")
179
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/weblate/utils/tasks.py b/weblate/utils/tasks.py
--- a/weblate/utils/tasks.py
+++ b/weblate/utils/tasks.py
@@ -102,6 +102,8 @@
                 "pg_dump",
                 # Superuser only, crashes on Alibaba Cloud Database PolarDB
                 "--no-subscriptions",
+                "--clean",
+                "--if-exists",
                 "--dbname",
                 database["NAME"],
             ]

{"golden_diff": "diff --git a/weblate/utils/tasks.py b/weblate/utils/tasks.py\n--- a/weblate/utils/tasks.py\n+++ b/weblate/utils/tasks.py\n@@ -102,6 +102,8 @@\n                 \"pg_dump\",\n                 # Superuser only, crashes on Alibaba Cloud Database PolarDB\n                 \"--no-subscriptions\",\n+                \"--clean\",\n+                \"--if-exists\",\n                 \"--dbname\",\n                 database[\"NAME\"],\n             ]\n", "issue": "Backup restore locks projects and shows no existing components\n### Describe the issue\r\n\r\nI was testing the borg backup feature in weblate and upon restoring the backup I noticed that the projects had the label that indicated that the project is locked (See Image). \r\n\r\nI see that the information is all there in the backup but it is not shown in the app. For example, after opening the projects, no components are found.\r\n\r\nI have found little to no information online regarding such an issue.\r\n\r\n\r\n\r\n\r\n\r\n### I already tried\r\n\r\n- [X] I've read and searched [the documentation](https://docs.weblate.org/).\r\n- [X] I've searched for similar filed issues in this repository.\r\n\r\n### Steps to reproduce the behavior\r\n\r\nThe issue happened after following the directions for a borg backup and restore in the weblate documentation.\r\n\r\n### Expected behavior\r\n\r\n_No response_\r\n\r\n### Screenshots\r\n\r\n![Screenshot 2024-01-25 091318](https://github.com/WeblateOrg/weblate/assets/157685480/c19d28f1-b7f7-42e4-bb2d-6ec617733388)\r\n\r\n\r\n### Exception traceback\r\n\r\n_No response_\r\n\r\n### How do you run Weblate?\r\n\r\nDocker container\r\n\r\n### Weblate versions\r\n\r\n weblate/weblate:edge\r\nHash: sha256:780310018d21eba2ef5e06b66acab7bf2e86b3ed1778640707ad47d0525cb213.\r\n\r\n### Weblate deploy checks\r\n\r\n_No response_\r\n\r\n### Additional context\r\n\r\n_No response_\n", "before_files": [{"content": "# Copyright \u00a9 Michal \u010ciha\u0159 <michal@weblate.org>\n#\n# SPDX-License-Identifier: GPL-3.0-or-later\n\nimport gzip\nimport os\nimport shutil\nimport subprocess\nimport sys\nimport time\nfrom importlib import import_module\nfrom shutil import copyfile\n\nfrom celery.schedules import crontab\nfrom django.conf import settings\nfrom django.core.cache import cache\nfrom django.core.management.commands import diffsettings\nfrom ruamel.yaml import YAML\n\nimport weblate.utils.version\nfrom weblate.formats.models import FILE_FORMATS\nfrom weblate.logger import LOGGER\nfrom weblate.machinery.models import MACHINERY\nfrom weblate.trans.models import Component, Translation\nfrom weblate.trans.util import get_clean_env\nfrom weblate.utils.backup import backup_lock\nfrom weblate.utils.celery import app\nfrom weblate.utils.data import data_dir\nfrom weblate.utils.db import using_postgresql\nfrom weblate.utils.errors import add_breadcrumb, report_error\nfrom weblate.utils.lock import WeblateLockTimeoutError\nfrom weblate.vcs.models import VCS_REGISTRY\n\n\n@app.task(trail=False)\ndef ping():\n    return {\n        \"version\": weblate.utils.version.GIT_VERSION,\n        \"vcs\": sorted(VCS_REGISTRY.keys()),\n        \"formats\": sorted(FILE_FORMATS.keys()),\n        \"mt_services\": sorted(MACHINERY.keys()),\n        \"encoding\": [sys.getfilesystemencoding(), sys.getdefaultencoding()],\n        \"uid\": os.getuid(),\n    }\n\n\n@app.task(trail=False)\ndef heartbeat():\n    cache.set(\"celery_loaded\", time.time())\n    cache.set(\"celery_heartbeat\", time.time())\n    cache.set(\n        \"celery_encoding\", [sys.getfilesystemencoding(), sys.getdefaultencoding()]\n    )\n\n\n@app.task(trail=False, autoretry_for=(WeblateLockTimeoutError,))\ndef settings_backup():\n    with backup_lock():\n        # Expand settings in case it contains non-trivial code\n        command = diffsettings.Command()\n        kwargs = {\"default\": None, \"all\": False, \"output\": \"hash\"}\n        with open(data_dir(\"backups\", \"settings-expanded.py\"), \"w\") as handle:\n            handle.write(command.handle(**kwargs))\n\n        # Backup original settings\n        if settings.SETTINGS_MODULE:\n            settings_mod = import_module(settings.SETTINGS_MODULE)\n            copyfile(settings_mod.__file__, data_dir(\"backups\", \"settings.py\"))\n\n        # Backup environment (to make restoring Docker easier)\n        with open(data_dir(\"backups\", \"environment.yml\"), \"w\") as handle:\n            yaml = YAML()\n            yaml.dump(dict(os.environ), handle)\n\n\n@app.task(trail=False)\ndef update_translation_stats_parents(pk: int):\n    translation = Translation.objects.get(pk=pk)\n    translation.stats.update_parents()\n\n\n@app.task(trail=False)\ndef update_language_stats_parents(pk: int):\n    component = Component.objects.get(pk=pk)\n    component.stats.update_language_stats_parents()\n\n\n@app.task(trail=False, autoretry_for=(WeblateLockTimeoutError,))\ndef database_backup():\n    if settings.DATABASE_BACKUP == \"none\":\n        return\n    with backup_lock():\n        database = settings.DATABASES[\"default\"]\n        env = get_clean_env()\n        compress = settings.DATABASE_BACKUP == \"compressed\"\n\n        out_compressed = data_dir(\"backups\", \"database.sql.gz\")\n        out_text = data_dir(\"backups\", \"database.sql\")\n\n        if using_postgresql():\n            cmd = [\n                \"pg_dump\",\n                # Superuser only, crashes on Alibaba Cloud Database PolarDB\n                \"--no-subscriptions\",\n                \"--dbname\",\n                database[\"NAME\"],\n            ]\n\n            if database[\"HOST\"]:\n                cmd.extend([\"--host\", database[\"HOST\"]])\n            if database[\"PORT\"]:\n                cmd.extend([\"--port\", database[\"PORT\"]])\n            if database[\"USER\"]:\n                cmd.extend([\"--username\", database[\"USER\"]])\n            if settings.DATABASE_BACKUP == \"compressed\":\n                cmd.extend([\"--file\", out_compressed])\n                cmd.extend([\"--compress\", \"6\"])\n                compress = False\n            else:\n                cmd.extend([\"--file\", out_text])\n\n            env[\"PGPASSWORD\"] = database[\"PASSWORD\"]\n        else:\n            cmd = [\n                \"mysqldump\",\n                \"--result-file\",\n                out_text,\n                \"--single-transaction\",\n                \"--skip-lock-tables\",\n            ]\n\n            if database[\"HOST\"]:\n                cmd.extend([\"--host\", database[\"HOST\"]])\n            if database[\"PORT\"]:\n                cmd.extend([\"--port\", database[\"PORT\"]])\n            if database[\"USER\"]:\n                cmd.extend([\"--user\", database[\"USER\"]])\n\n            cmd.extend([\"--databases\", database[\"NAME\"]])\n\n            env[\"MYSQL_PWD\"] = database[\"PASSWORD\"]\n\n        try:\n            subprocess.run(\n                cmd,\n                env=env,\n                capture_output=True,\n                stdin=subprocess.DEVNULL,\n                check=True,\n                text=True,\n            )\n        except subprocess.CalledProcessError as error:\n            add_breadcrumb(\n                category=\"backup\",\n                message=\"database dump output\",\n                stdout=error.stdout,\n                stderr=error.stderr,\n            )\n            LOGGER.error(\"failed database backup: %s\", error.stderr)\n            report_error()\n            raise\n\n        if compress:\n            with open(out_text, \"rb\") as f_in, gzip.open(out_compressed, \"wb\") as f_out:\n                shutil.copyfileobj(f_in, f_out)\n            os.unlink(out_text)\n\n\n@app.on_after_finalize.connect\ndef setup_periodic_tasks(sender, **kwargs):\n    cache.set(\"celery_loaded\", time.time())\n    sender.add_periodic_task(\n        crontab(hour=1, minute=0), settings_backup.s(), name=\"settings-backup\"\n    )\n    sender.add_periodic_task(\n        crontab(hour=1, minute=30), database_backup.s(), name=\"database-backup\"\n    )\n    sender.add_periodic_task(60, heartbeat.s(), name=\"heartbeat\")\n", "path": "weblate/utils/tasks.py"}], "after_files": [{"content": "# Copyright \u00a9 Michal \u010ciha\u0159 <michal@weblate.org>\n#\n# SPDX-License-Identifier: GPL-3.0-or-later\n\nimport gzip\nimport os\nimport shutil\nimport subprocess\nimport sys\nimport time\nfrom importlib import import_module\nfrom shutil import copyfile\n\nfrom celery.schedules import crontab\nfrom django.conf import settings\nfrom django.core.cache import cache\nfrom django.core.management.commands import diffsettings\nfrom ruamel.yaml import YAML\n\nimport weblate.utils.version\nfrom weblate.formats.models import FILE_FORMATS\nfrom weblate.logger import LOGGER\nfrom weblate.machinery.models import MACHINERY\nfrom weblate.trans.models import Component, Translation\nfrom weblate.trans.util import get_clean_env\nfrom weblate.utils.backup import backup_lock\nfrom weblate.utils.celery import app\nfrom weblate.utils.data import data_dir\nfrom weblate.utils.db import using_postgresql\nfrom weblate.utils.errors import add_breadcrumb, report_error\nfrom weblate.utils.lock import WeblateLockTimeoutError\nfrom weblate.vcs.models import VCS_REGISTRY\n\n\n@app.task(trail=False)\ndef ping():\n    return {\n        \"version\": weblate.utils.version.GIT_VERSION,\n        \"vcs\": sorted(VCS_REGISTRY.keys()),\n        \"formats\": sorted(FILE_FORMATS.keys()),\n        \"mt_services\": sorted(MACHINERY.keys()),\n        \"encoding\": [sys.getfilesystemencoding(), sys.getdefaultencoding()],\n        \"uid\": os.getuid(),\n    }\n\n\n@app.task(trail=False)\ndef heartbeat():\n    cache.set(\"celery_loaded\", time.time())\n    cache.set(\"celery_heartbeat\", time.time())\n    cache.set(\n        \"celery_encoding\", [sys.getfilesystemencoding(), sys.getdefaultencoding()]\n    )\n\n\n@app.task(trail=False, autoretry_for=(WeblateLockTimeoutError,))\ndef settings_backup():\n    with backup_lock():\n        # Expand settings in case it contains non-trivial code\n        command = diffsettings.Command()\n        kwargs = {\"default\": None, \"all\": False, \"output\": \"hash\"}\n        with open(data_dir(\"backups\", \"settings-expanded.py\"), \"w\") as handle:\n            handle.write(command.handle(**kwargs))\n\n        # Backup original settings\n        if settings.SETTINGS_MODULE:\n            settings_mod = import_module(settings.SETTINGS_MODULE)\n            copyfile(settings_mod.__file__, data_dir(\"backups\", \"settings.py\"))\n\n        # Backup environment (to make restoring Docker easier)\n        with open(data_dir(\"backups\", \"environment.yml\"), \"w\") as handle:\n            yaml = YAML()\n            yaml.dump(dict(os.environ), handle)\n\n\n@app.task(trail=False)\ndef update_translation_stats_parents(pk: int):\n    translation = Translation.objects.get(pk=pk)\n    translation.stats.update_parents()\n\n\n@app.task(trail=False)\ndef update_language_stats_parents(pk: int):\n    component = Component.objects.get(pk=pk)\n    component.stats.update_language_stats_parents()\n\n\n@app.task(trail=False, autoretry_for=(WeblateLockTimeoutError,))\ndef database_backup():\n    if settings.DATABASE_BACKUP == \"none\":\n        return\n    with backup_lock():\n        database = settings.DATABASES[\"default\"]\n        env = get_clean_env()\n        compress = settings.DATABASE_BACKUP == \"compressed\"\n\n        out_compressed = data_dir(\"backups\", \"database.sql.gz\")\n        out_text = data_dir(\"backups\", \"database.sql\")\n\n        if using_postgresql():\n            cmd = [\n                \"pg_dump\",\n                # Superuser only, crashes on Alibaba Cloud Database PolarDB\n                \"--no-subscriptions\",\n                \"--clean\",\n                \"--if-exists\",\n                \"--dbname\",\n                database[\"NAME\"],\n            ]\n\n            if database[\"HOST\"]:\n                cmd.extend([\"--host\", database[\"HOST\"]])\n            if database[\"PORT\"]:\n                cmd.extend([\"--port\", database[\"PORT\"]])\n            if database[\"USER\"]:\n                cmd.extend([\"--username\", database[\"USER\"]])\n            if settings.DATABASE_BACKUP == \"compressed\":\n                cmd.extend([\"--file\", out_compressed])\n                cmd.extend([\"--compress\", \"6\"])\n                compress = False\n            else:\n                cmd.extend([\"--file\", out_text])\n\n            env[\"PGPASSWORD\"] = database[\"PASSWORD\"]\n        else:\n            cmd = [\n                \"mysqldump\",\n                \"--result-file\",\n                out_text,\n                \"--single-transaction\",\n                \"--skip-lock-tables\",\n            ]\n\n            if database[\"HOST\"]:\n                cmd.extend([\"--host\", database[\"HOST\"]])\n            if database[\"PORT\"]:\n                cmd.extend([\"--port\", database[\"PORT\"]])\n            if database[\"USER\"]:\n                cmd.extend([\"--user\", database[\"USER\"]])\n\n            cmd.extend([\"--databases\", database[\"NAME\"]])\n\n            env[\"MYSQL_PWD\"] = database[\"PASSWORD\"]\n\n        try:\n            subprocess.run(\n                cmd,\n                env=env,\n                capture_output=True,\n                stdin=subprocess.DEVNULL,\n                check=True,\n                text=True,\n            )\n        except subprocess.CalledProcessError as error:\n            add_breadcrumb(\n                category=\"backup\",\n                message=\"database dump output\",\n                stdout=error.stdout,\n                stderr=error.stderr,\n            )\n            LOGGER.error(\"failed database backup: %s\", error.stderr)\n            report_error()\n            raise\n\n        if compress:\n            with open(out_text, \"rb\") as f_in, gzip.open(out_compressed, \"wb\") as f_out:\n                shutil.copyfileobj(f_in, f_out)\n            os.unlink(out_text)\n\n\n@app.on_after_finalize.connect\ndef setup_periodic_tasks(sender, **kwargs):\n    cache.set(\"celery_loaded\", time.time())\n    sender.add_periodic_task(\n        crontab(hour=1, minute=0), settings_backup.s(), name=\"settings-backup\"\n    )\n    sender.add_periodic_task(\n        crontab(hour=1, minute=30), database_backup.s(), name=\"database-backup\"\n    )\n    sender.add_periodic_task(60, heartbeat.s(), name=\"heartbeat\")\n", "path": "weblate/utils/tasks.py"}]}