Skip to content

Commit a9e63bb

Browse files
committed
add database backup and restore information to main readme
1 parent bbb0d4a commit a9e63bb

File tree

1 file changed

+68
-27
lines changed

1 file changed

+68
-27
lines changed

README.md

Lines changed: 68 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -70,56 +70,97 @@ $ docker-compose -f local.yml run --rm django python manage.py createsuperuser
7070

7171
Create additional users through the admin interface (/admin).
7272

73-
### Loading Fixtures
73+
### Database Backup and Restore
7474

75-
To load collections:
75+
COSMOS provides dedicated management commands for backing up and restoring your PostgreSQL database. These commands handle both compressed and uncompressed backups and automatically detect your server environment from your configuration.
7676

77-
```bash
78-
$ docker-compose -f local.yml run --rm django python manage.py loaddata sde_collections/fixtures/collections.json
79-
```
77+
#### Creating a Database Backup
8078

81-
### Manually Creating and Loading a ContentTypeless Backup
82-
Navigate to the server running prod, then to the project folder. Run the following command to create a backup:
79+
To create a backup of your database:
8380

8481
```bash
85-
docker-compose -f production.yml run --rm --user root django python manage.py dumpdata --natural-foreign --natural-primary --exclude=contenttypes --exclude=auth.Permission --indent 2 --output /app/backups/prod_backup-20241114.json
82+
# Create a compressed backup (recommended)
83+
docker-compose -f local.yml run --rm django python manage.py database_backup
84+
85+
# Create an uncompressed backup
86+
docker-compose -f local.yml run --rm django python manage.py database_backup --no-compress
87+
88+
# Specify custom output location
89+
docker-compose -f local.yml run --rm django python manage.py database_backup --output /path/to/output.sql
8690
```
87-
This will have saved the backup in a folder outside of the docker container. Now you can copy it to your local machine.
91+
92+
The backup command will automatically:
93+
- Detect your server environment (Production/Staging/Local)
94+
- Use database credentials from your environment settings
95+
- Generate a dated filename if no output path is specified
96+
- Compress the backup by default (can be disabled with --no-compress)
97+
98+
#### Restoring from a Database Backup
99+
100+
To restore your database from a backup:
88101

89102
```bash
90-
mv ~/prod_backup-20240812.json <project_path>/prod_backup-20240812.json
91-
scp sde:/home/ec2-user/sde_indexing_helper/backups/prod_backup-20240812.json prod_backup-20240812.json
103+
# Restore from a backup (handles both .sql and .sql.gz files)
104+
docker-compose -f local.yml run --rm django python manage.py database_restore path/to/backup.sql[.gz]
92105
```
93106

94-
Finally, load the backup into your local database:
107+
The restore command will:
108+
- Automatically detect if the backup is compressed (.gz)
109+
- Terminate existing database connections
110+
- Drop and recreate the database
111+
- Restore all data from the backup
112+
- Handle all database credentials from your environment settings
113+
114+
#### Working with Remote Servers
115+
116+
When working with production or staging servers:
95117

118+
1. First, SSH into the appropriate server:
96119
```bash
97-
docker-compose -f local.yml run --rm django python manage.py loaddata prod_backup-20240812.json
120+
# For production
121+
ssh user@production-server
122+
cd /path/to/project
123+
124+
# For staging
125+
ssh user@staging-server
126+
cd /path/to/project
98127
```
99128

100-
### Loading the Database from an Arbitrary Backup
129+
2. Then run the backup command with the production configuration:
130+
```bash
131+
docker-compose -f production.yml run --rm django python manage.py database_backup
132+
```
101133

102-
1. Build the project and run the necessary containers (as documented above).
103-
2. Clear out content types using the Django shell:
134+
3. Copy the backup to your local machine:
135+
```bash
136+
scp user@remote-server:/path/to/backup.sql.gz ./local-backup.sql.gz
137+
```
104138

139+
4. Finally, restore locally:
105140
```bash
106-
$ docker-compose -f local.yml run --rm django python manage.py shell
107-
>>> from django.contrib.contenttypes.models import ContentType
108-
>>> ContentType.objects.all().delete()
109-
>>> exit()
141+
docker-compose -f local.yml run --rm django python manage.py database_restore local-backup.sql.gz
110142
```
111143

112-
3. Load your backup database:
144+
#### Alternative Methods
145+
146+
While the database_backup and database_restore commands are the recommended approach, there are alternative methods available:
147+
148+
##### Using JSON Fixtures (for smaller datasets)
149+
If you're working with a smaller dataset, you can use Django's built-in fixtures:
113150

114151
```bash
115-
$ docker cp /path/to/your/backup.json container_name:/path/inside/container/backup.json
116-
$ docker-compose -f local.yml run --rm django python manage.py loaddata /path/inside/the/container/backup.json
117-
$ docker-compose -f local.yml run --rm django python manage.py migrate
152+
# Create a backup excluding content types
153+
docker-compose -f production.yml run --rm --user root django python manage.py dumpdata \
154+
--natural-foreign --natural-primary \
155+
--exclude=contenttypes --exclude=auth.Permission \
156+
--indent 2 \
157+
--output /app/backups/prod_backup-$(date +%Y%m%d).json
158+
159+
# Restore from a fixture
160+
docker-compose -f local.yml run --rm django python manage.py loaddata /path/to/backup.json
118161
```
119-
### Restoring the Database from a SQL Dump
120-
If the JSON file is particularly large (>1.5GB), Docker might struggle with this method. In such cases, you can use SQL dump and restore commands as an alternative, as described [here](./SQLDumpRestoration.md).
121-
122162

163+
Note: For large databases (>1.5GB), the database_backup and database_restore commands are strongly recommended over JSON fixtures as they handle large datasets more efficiently.
123164

124165
## Additional Commands
125166

0 commit comments

Comments
 (0)