Skip to content

Commit

Permalink
add db backup function
Browse files Browse the repository at this point in the history
  • Loading branch information
versun committed May 18, 2024
1 parent 4b778ec commit 2dd8641
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
3 changes: 2 additions & 1 deletion translator/migrations/0031_translated_content_new_hash.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Generated by Django 5.0.4 on 2024-05-18 00:00

from django.db import migrations, models

from . import backup_db

class Migration(migrations.Migration):

Expand All @@ -15,4 +15,5 @@ class Migration(migrations.Migration):
name='new_hash',
field=models.CharField(default='new', editable=False, max_length=39),
),
migrations.RunPython(backup_db),
]
17 changes: 17 additions & 0 deletions translator/migrations/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import os
import logging
import shutil
from django.conf import settings
def backup_db(apps, schema_editor):
db_path = settings.DATABASES['default']['NAME']
backup_path = f'{db_path}.bak'
try:
if os.path.exists(backup_path):
os.remove(backup_path)
shutil.copyfile(db_path, backup_path)
except FileNotFoundError:
logging.error(f"Database file {db_path} not found.")
except PermissionError:
logging.error(f"Permission denied when accessing {db_path} or {backup_path}.")
except Exception as e:
logging.error(f"Error occurred during database backup: {str(e)}")

0 comments on commit 2dd8641

Please sign in to comment.