OpenSlides/server/openslides/core/management/commands/backupdb.py
FinnStutzenstein 2bcab5d098
Repository restructure
- moved all server related things into the folder `server`, so this
configuration is parallel to the client.
- All main "services" are now folders in the root directory
- Added Dockerfiles to each service (currently server and client)
- Added a docker compose configuration to start everything together.
Currently there are heavy dependencies into https://github.com/OpenSlides/openslides-docker-compose
- Resturctured the .gitignore. If someone needs something excluded,
please add it to the right section.
- Added initial build setup with Docker and docker-compose.
- removed setup.py. We won't deliver OpenSlides via pip anymore.
2020-08-21 08:11:13 +02:00

53 lines
1.7 KiB
Python

import shutil
from django.core.management.base import BaseCommand, CommandError
from django.db import connection, transaction
from openslides.utils.main import get_database_path_from_settings
class Command(BaseCommand):
"""
Command to backup the SQLite3 database.
"""
help = "Backups the SQLite3 database."
def add_arguments(self, parser):
parser.add_argument(
"--path",
default="database_backup.sqlite",
help="Path for the backup file (Default: database_backup.sqlite).",
)
def handle(self, *args, **options):
path = options.get("path")
@transaction.atomic
def do_backup(src_path, dest_path):
# perform a simple file-copy backup of the database
# first we need a shared lock on the database, issuing a select()
# will do this for us
cursor = connection.cursor()
cursor.execute("SELECT count(*) from sqlite_master")
# now copy the file
try:
shutil.copy(src_path, dest_path)
except IOError:
# TODO: use the IOError message as message for the user
raise CommandError("Database backup failed.")
database_path = get_database_path_from_settings()
if database_path:
do_backup(database_path, path)
self.stdout.write(
self.style.SUCCESS(
f"Database {database_path} successfully stored at {path}."
)
)
else:
raise CommandError(
"Default database is not SQLite3. Only SQLite3 databases"
"can currently be backuped."
)