Skip to content

Commit b020e45

Browse files
committed
fix(docs): revise database procedures
The database instructions, while technically correct, are not idiot proof. There are so many different ways you can stuff up and end up losing all your data. For example, the current docs say: docker compose exec -T database pg_dump -U teslamate teslamate > ./teslamate.bck If anything goes wrong with this command, it might overwrite any potentially good backup with a 0 byte file. And some of the users won't notice until after they have deleted the current database and tried to restore. By this stage it is too late to recover. These instructions attempt to make the process more resilient by not deleting the current data until after it is confirmed that the restore is good. The instructions also use a unique filename for every backup. So you are not overwriting the same backup file every time.
1 parent a7d4e26 commit b020e45

File tree

3 files changed

+111
-16
lines changed

3 files changed

+111
-16
lines changed

website/docs/installation/docker.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ This setup is recommended only if you are running TeslaMate **on your home netwo
4545
- POSTGRES_PASSWORD=password #insert your secure database password!
4646
- POSTGRES_DB=teslamate
4747
volumes:
48-
- teslamate-db:/var/lib/postgresql/data
48+
- teslamate-db-17:/var/lib/postgresql/data
4949

5050
grafana:
5151
image: teslamate/grafana:latest

website/docs/maintenance/backup_restore.md

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ If you are using `docker-compose`, you are using Docker Compose v1, which has be
1111
Create backup file `teslamate.bck`:
1212

1313
```bash
14-
docker compose exec -T database pg_dump -U teslamate teslamate > ./teslamate.bck
14+
docker compose exec -T database pg_dump -U teslamate teslamate > teslamate_database_$(date +%F_%H-%M-%S).sql
1515
```
1616

1717
:::note
@@ -36,21 +36,34 @@ If you changed `TM_DB_USER` in the .env file from one of the advanced guides, ma
3636
Replace the default `teslamate` value below with the value defined in the .env file if you have one (TM_DB_USER and TM_DB_NAME)
3737
:::
3838

39+
1. Stop the teslamate container to avoid write conflicts
40+
3941
```bash
40-
# Stop the teslamate container to avoid write conflicts
4142
docker compose stop teslamate
43+
```
44+
45+
2. Drop existing data and reinitialize (Don't forget to replace first teslamate if using different TM_DB_USER)
4246

43-
# Drop existing data and reinitialize (Don't forget to replace first teslamate if using different TM_DB_USER)
47+
```bash
4448
docker compose exec -T database psql -U teslamate teslamate << .
4549
DROP SCHEMA public CASCADE;
4650
CREATE SCHEMA public;
4751
CREATE EXTENSION cube WITH SCHEMA public;
4852
CREATE EXTENSION earthdistance WITH SCHEMA public;
4953
.
54+
```
55+
56+
3. Restore
5057

51-
# Restore
52-
docker compose exec -T database psql -U teslamate -d teslamate < teslamate.bck
58+
Use the same filename that was used in the backup step.
59+
Replace `<backup_filename>` with the actual filename generated during the backup step (e.g., `teslamate_database_2025-05-07_07-33-48.sql`).
5360

54-
# Restart the teslamate container
61+
```bash
62+
docker compose exec -T database psql -U teslamate -d teslamate < <backup_filename>
63+
```
64+
65+
4. Restart the teslamate container
66+
67+
```bash
5568
docker compose start teslamate
5669
```

website/docs/maintenance/upgrading_postgres.md

Lines changed: 91 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,110 @@ title: Upgrading PostgreSQL to a new major version
33
sidebar_label: Upgrading PostgreSQL
44
---
55

6-
1. Create a [backup](backup_restore.md)
7-
2. Stop all TeslaMate containers
6+
In these instructions it is assumed you are upgrading from postgresql 16 to postgresql 17. Please update
7+
references to 16 to your start version and 17 to your desired version.
8+
9+
These instructions also assume you have the standard `docker-compose.yml` file. If this is not the case
10+
you might need to make adjustments. For example, older installed might call the container "db" not "database".
11+
12+
## Upgrade
13+
14+
1. Create a backup:
15+
16+
```bash
17+
docker compose exec -T database pg_dump -U teslamate teslamate > teslamate_database_$(date +%F_%H-%M-%S).sql
18+
```
19+
20+
2. Check to make sure there are no obvious errors in this backup file. There should not be any errors appearing
21+
on screen. The file should be more than 0 bytes long. Do not proceed if there is any doubt.
22+
23+
3. Stop all TeslaMate containers
824

925
```bash
1026
docker compose down
1127
```
1228

13-
3. Delete the database volume. **Be careful**, this will delete all your previously recorded data! Make sure that your backup can be restored before you start.
29+
4. Make a copy of your current `docker-compose.yml`
1430

1531
```bash
16-
docker volume rm "$(basename "$PWD")_teslamate-db"
32+
cp docker-compose.yml docker-compose-16.yml
1733
```
1834

19-
4. Change the postgres version in docker-compose.yml and start the container
35+
5. Update the source to point to a new location that doesn't exist already and update the postgres version.
2036

21-
```yml {2}
22-
database:
23-
image: postgres:xx
37+
```yaml
38+
services:
39+
[...]
40+
database:
41+
image: postgres:17
42+
[...]
43+
volumes:
44+
- teslamate-db-17:/var/lib/postgresql/data
2445
```
2546
47+
Here we updated the image to `postgres:17` and the volume to `teslamate-db-17`.
48+
49+
:::note
50+
There are TWO changes. Both are very important.
51+
:::
52+
53+
:::note
54+
Do not delete the old volume until you are sure that the database restore is
55+
working correctly and has all your data.
56+
:::
57+
58+
6. Start the container.
59+
2660
```bash
2761
docker compose up -d database
2862
```
2963

30-
5. [Restore](backup_restore.md) the backup
64+
7. Restore the backup.
65+
66+
:::note
67+
database should be empty. If database contains data, then check you updated the volume correctly in step 5.
68+
:::
69+
70+
Replace `<backup_filename>` with the actual filename generated during the backup step (e.g., `teslamate_database_2025-05-07_07-33-48.sql`).
71+
72+
```bash
73+
docker compose exec -T database psql -U teslamate teslamate << .
74+
CREATE SCHEMA public;
75+
CREATE EXTENSION cube WITH SCHEMA public;
76+
CREATE EXTENSION earthdistance WITH SCHEMA public;
77+
.
78+
docker exec -i teslamate-database-1 psql -U teslamate -d teslamate -f <backup_filename>
79+
```
80+
81+
There should not be any errors.
82+
83+
84+
8. Start the container.
85+
86+
```bash
87+
docker compose up -d teslamate
88+
```
89+
90+
9. Make sure everything is working, and all the data is intact.
91+
92+
## Roll back on error
93+
94+
If you cannot restore the backup for any reason, you might have to roll back to your previous version.
95+
96+
1. Stop everything
97+
98+
```bash
99+
docker compose stop
100+
```
101+
102+
2. restore the values in `docker-compose.yml` that you changed in step 5 and step 6.
103+
104+
```bash
105+
cp docker-compose-16.yml docker-compose.yml
106+
```
107+
108+
3. Start everything
109+
110+
```bash
111+
docker compose up -d
112+
```

0 commit comments

Comments
 (0)