Debian Codegraph @

docs/src/integration.md

raw
1 # Integration tutorials
2
3 !!! note
4
5 If you have a custom and/or interesting way of using DBBackup, do not
6 hesitate to make a pull request.
7
8 ## Django-Cron
9
10 Example recurring job using [django-cron](https://github.com/Tivix/django-cron) with filesystem storage:
11
12 ```python
13 import os
14 from django.core import management
15 from django.conf import settings
16 from django_cron import CronJobBase, Schedule
17
18
19 class Backup(CronJobBase):
20 RUN_AT_TIMES = ['6:00', '18:00']
21 schedule = Schedule(run_at_times=RUN_AT_TIMES)
22 code = 'my_app.Backup'
23
24 def do(self):
25 management.call_command('dbbackup')
26 ```
27
28 ## Django-Crontab
29
30 Example crontab entry using [django-crontab](https://github.com/kraiz/django-crontab) and filesystem storage:
31
32 In `settings.py`:
33
34 ```python
35 CRONTAB_COMMAND_SUFFIX = '2>&1'
36 CRONJOBS = [
37 ('0 5 * * *', 'core.backup.backup_job', '>> ' + os.path.join(CORE_DIR, 'backup/backup.log'))
38 ]
39 ```
40
41 In `backup.py`:
42
43 ```python
44 from datetime import datetime
45 from django.core import management
46
47 def backup_job():
48 print("[{}] Backing up database and media files...".format(datetime.now()))
49 management.call_command('dbbackup', '--clean')
50 management.call_command('mediabackup', '--clean')
51 print("[{}] Backup done!".format(datetime.now()))
52 ```
53
54 Add the cron job:
55
56 ```bash
57 python manage.py crontab add
58 ```
59
60 ## Django-Celery-Beat
61
62 Example setup using [Celery](https://docs.celeryq.dev/) with [django-celery-beat](https://django-celery-beat.readthedocs.io/) for scheduled backups:
63
64 First, create a `tasks.py` file in your app:
65
66 ```python
67 from celery import Celery
68 from django.core import management
69
70 app = Celery()
71
72
73 @app.task
74 def backup_db():
75 management.call_command('dbbackup')
76
77
78 @app.task
79 def backup_media():
80 management.call_command('mediabackup')
81 ```
82
83 Then, create a Django management command to set up periodic tasks (e.g., `management/commands/setup_backup_schedule.py`):
84
85 ```python
86 from django.core.management.base import BaseCommand
87 from django_celery_beat.models import CrontabSchedule, PeriodicTask
88 import pytz
89
90
91 class Command(BaseCommand):
92 help = 'Creates crontab schedule objects and periodic tasks that use them'
93
94 def handle(self, *args, **options):
95 # Schedule for daily backups at midnight UTC
96 every_day, _ = CrontabSchedule.objects.get_or_create(
97 minute='0',
98 hour='0',
99 day_of_week='*',
100 day_of_month='*',
101 month_of_year='*',
102 timezone=pytz.timezone('UTC')
103 )
104
105 # Create periodic tasks
106 PeriodicTask.objects.get_or_create(
107 crontab=every_day,
108 name='Backup DB',
109 task='myapp.tasks.backup_db',
110 )
111
112 PeriodicTask.objects.get_or_create(
113 crontab=every_day,
114 name='Backup Media',
115 task='myapp.tasks.backup_media',
116 )
117
118 self.stdout.write(
119 self.style.SUCCESS('Successfully created backup schedule')
120 )
121 ```
122
123 Run the setup command:
124
125 ```bash
126 python manage.py setup_backup_schedule
127 ```
128
129 Make sure your Celery worker and beat scheduler are running:
130
131 ```bash
132 celery -A myproject worker --loglevel=info
133 celery -A myproject beat --loglevel=info
134 ```