Skip to content

Commit da83b50

Browse files
committed
Added docs for django-dbbackup #51
1 parent 0aa8efb commit da83b50

File tree

1 file changed

+96
-3
lines changed

1 file changed

+96
-3
lines changed

docs/technologies/django/backup-restore.mdx

Lines changed: 96 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,102 @@ import SubHeading from "@site/src/components/SubHeading";
77

88
<SubHeading color="#25c2a0">Learn how to manage Backups & DB Restore in Django</SubHeading>
99

10-
Input:
10+
## Why is it important to back up your Django database?
11+
- Data loss prevention: A database backup can help you recover your data in the event of a hardware failure, software corruption, or human error. This can save you a lot of time and money, as you will not have to recreate your data from scratch.
12+
- Disaster recovery: A database backup can help you recover your data in the event of a natural disaster, such as a flood or fire. This can help you keep your business running even in the event of a major disruption.
13+
- Compliance: In some industries, some regulations require businesses to keep backups of their data. For example, the financial services industry is required to keep backups of their data for seven years.
1114

12-
- Issue: https://github.com/app-generator/docs/issues/51
13-
- Coding Sample: https://github.com/app-generator/sample-django-backup-restore
15+
## Using `django-dbbackup` to backup databases
16+
`django-dbbackup` is a Django app that can be used to back up your database to a variety of storage locations, including Amazon S3, Dropbox, and the local file system. It also supports compression and encryption, so you can be sure that your backups are secure.
1417

18+
`django-dbbackup` provides management commands to help you back up and restore your project database and media files. It is made to:
19+
- Allow you to secure your backup with GPG signature and encryption.
20+
- Archive with compression.
21+
- Deal easily with remote archiving.
22+
- Keep your development database up to date.
23+
- Use Crontab or Celery to setup automated backups
1524

25+
### Setting up the Django project
26+
For this tutorial, we will be using [`django-material-kit`](https://github.com/app-generator/sample-django-backup-restore)
27+
28+
- Clone the repository
29+
```bash
30+
$ git clone https://github.com/app-generator/sample-django-backup-restore
31+
$ cd sample-django-backup-restore
32+
sample-django-backup-restore$
33+
```
34+
- Create a virtual environment and activate it
35+
```bash
36+
sample-django-backup-restore$ virtualenv venv
37+
sample-django-backup-restore$
38+
sample-django-backup-restore$ source venv/bin/activate # On Linux/Mac
39+
sample-django-backup-restore$
40+
sample-django-backup-restore$ .\venv\Scripts\activate # On Windows
41+
```
42+
43+
### Configuring `django-dbbackup` for the Django project
44+
45+
- Install project dependencies
46+
```bash
47+
(venv) sample-django-backup-restore$ pip install -r requirements.txt
48+
```
49+
50+
- Migrate database tables and create a superuser
51+
```bash
52+
(venv) sample-django-backup-restore$ python manage.py migrate
53+
(venv) sample-django-backup-restore$ python manage.py createsuperuser
54+
```
55+
56+
- Install `django-dbbackup` using pip
57+
```
58+
(venv) sample-django-backup-restore$ pip install django-dbbackup
59+
```
60+
61+
- Now that `django-dbbackup` has been installed, we will be configuring the Django project to recognize the application. Make the following changes to `core/settings.py`
62+
```py
63+
# core/settings.py
64+
...
65+
INSTALLED_APPS = (
66+
...
67+
'dbbackup', # django-dbbackup
68+
)
69+
...
70+
DBBACKUP_STORAGE = 'django.core.files.storage.FileSystemStorage'
71+
DBBACKUP_STORAGE_OPTIONS = {'location': 'backup/'}
72+
...
73+
```
74+
By default, `django-dbbackup` uses the built-in file system storage to manage files in a local directory. This creates a `dump` file in your project directory or the specified directory. `DBBACKUP_STORAGE` is used to specify the storage system to be used and `DBBACKUP_STORAGE_OPTIONS` is a dictionary containing the configuration for the storage system.
75+
`django-dbbackup` is designed to use the right tool to create dump files when working with any database engine, an example is `psql` for Postgresql databases and `mysqldump` for MySQL databases.
76+
77+
### Creating backup files
78+
- Execute the command below on your terminal to create backup files for your database
79+
```bash
80+
(venv) sample-django-backup-restore$ python manage.py dbbackup
81+
```
82+
This command creates a dump file in the location specified in `DBBACKUP_STORAGE_OPTIONS`.
83+
84+
This command can also be executed to create compressed files by adding an optional flag
85+
```bash
86+
(venv) sample-django-backup-restore$ python manage.py dbbackup -z # create compressed dump files
87+
```
88+
89+
## Restoring database from backup files
90+
Restoring the database from backup files can be done by executing the command below on your terminal
91+
```bash
92+
(venv) sample-django-backup-restore$ python manage.py dbrestore
93+
(venv) sample-django-backup-restore$
94+
(venv) sample-django-backup-restore$ python manage.py dbrestore -z # when restoring from a compressed file
95+
```
96+
97+
After running the command above, your database would be restored to the state of the last backup made using `django-dbbackup`
98+
99+
## Conclusion
100+
In conclusion, mastering the art of managing backups and database restores in Django is an essential skill for any Django developer. By understanding the importance of backing up your Django database and learning how to utilize tools like `django-dbbackup`, you can ensure the safety and integrity of your data.
101+
102+
By applying the knowledge gained from this tutorial, you now possess the necessary skills to effectively manage backups and database restores in Django. Remember to consistently back up your database to safeguard your valuable information and be prepared for any unforeseen events.
103+
104+
## Resources
105+
- 👉 Django Dbbackup [Documentation](https://django-dbbackup.readthedocs.io/en/master/installation.html)
106+
- 👉 [Code sample](https://github.com/app-generator/sample-django-backup-restore)
107+
- 👉 Free [Support](https://appseed.us/support/) via Email & Discord
108+
- 👉 [Custom Development Services](https://appseed.us/custom-development/) provided by experts

0 commit comments

Comments
 (0)