Skip to content

Commit 5e73738

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 c31c22d commit 5e73738

3 files changed

Lines changed: 136 additions & 32 deletions

File tree

website/docs/installation/docker.md

Lines changed: 2 additions & 2 deletions
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
@@ -71,7 +71,7 @@ This setup is recommended only if you are running TeslaMate **on your home netwo
7171
- mosquitto-data:/mosquitto/data
7272

7373
volumes:
74-
teslamate-db:
74+
teslamate-db-17:
7575
teslamate-grafana-data:
7676
mosquitto-conf:
7777
mosquitto-data:

website/docs/maintenance/backup_restore.md

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ If you are using `docker-compose`, you are using Docker Compose v1, which has be
88

99
## Backup
1010

11-
Create backup file `teslamate.bck`:
11+
Create backup file:
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
1818
`-T` is important if you add this line a crontab or the backup will not work because docker will generate this error `the input device is not a TTY`
1919
:::
2020

2121
:::note
22-
Be absolutely certain to move the `teslamate.bck` file to another safe location, as you may lose that backup file if you use a docker-compose GUI to upgrade your teslamate configuration. Some GUIs delete the folder that holds the `docker-compose.yml` when updating.
22+
Be absolutely certain to move the backup file to another safe location, as you may lose that backup file if you use a docker-compose GUI to upgrade your teslamate configuration. Some GUIs delete the folder that holds the `docker-compose.yml` when updating.
2323
:::
2424

2525
:::note
@@ -36,21 +36,36 @@ 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-
```bash
40-
# Stop the teslamate container to avoid write conflicts
41-
docker compose stop teslamate
42-
43-
# Drop existing data and reinitialize (Don't forget to replace first teslamate if using different TM_DB_USER)
44-
docker compose exec -T database psql -U teslamate teslamate << .
45-
DROP SCHEMA public CASCADE;
46-
CREATE SCHEMA public;
47-
CREATE EXTENSION cube WITH SCHEMA public;
48-
CREATE EXTENSION earthdistance WITH SCHEMA public;
49-
.
50-
51-
# Restore
52-
docker compose exec -T database psql -U teslamate -d teslamate < teslamate.bck
53-
54-
# Restart the teslamate container
55-
docker compose start teslamate
56-
```
39+
1. Stop the teslamate container to avoid write conflicts
40+
41+
```bash
42+
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)
46+
47+
```bash
48+
docker compose exec -T database psql -U teslamate teslamate << .
49+
DROP SCHEMA public CASCADE;
50+
CREATE SCHEMA public;
51+
CREATE EXTENSION cube WITH SCHEMA public;
52+
CREATE EXTENSION earthdistance WITH SCHEMA public;
53+
.
54+
```
55+
56+
3. Restore
57+
58+
Use the same filename that was used in the backup step.
59+
60+
Replace `teslamate_database_2025-05-07_07-33-48.sql` with the actual filename generated during the backup step.
61+
62+
```bash
63+
DATABASE="teslamate_database_2025-05-07_07-33-48.sql"
64+
docker compose exec -T database psql -U teslamate -d teslamate < "$DATABASE"
65+
```
66+
67+
4. Restart the teslamate container
68+
69+
```bash
70+
docker compose start teslamate
71+
```

website/docs/maintenance/upgrading_postgres.md

Lines changed: 98 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,117 @@ 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+
:::note
13+
If you are using `docker-compose`, you are using Docker Compose v1, which has been deprecated. Docker Compose commands refer to Docker Compose v2. Consider upgrading your docker setup, see [Migrate to Compose V2](https://docs.docker.com/compose/migrate/)
14+
:::
15+
16+
## Upgrade
17+
18+
1. Create a backup:
19+
20+
```bash
21+
docker compose exec -T database pg_dump -U teslamate teslamate > teslamate_database_$(date +%F_%H-%M-%S).sql
22+
```
23+
24+
2. _Check to make sure there are no obvious errors in this backup file._ There should not be any errors appearing
25+
on screen. The file should be more than 0 bytes long. Do not proceed if there is any doubt.
26+
27+
3. Stop all TeslaMate containers
828

929
```bash
1030
docker compose down
1131
```
1232

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.
33+
4. Make a copy of your current `docker-compose.yml`
1434

1535
```bash
16-
docker volume rm "$(basename "$PWD")_teslamate-db"
36+
cp docker-compose.yml docker-compose-16.yml
1737
```
1838

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

21-
```yml {2}
22-
database:
23-
image: postgres:xx
41+
```yaml
42+
services:
43+
[...]
44+
database:
45+
image: postgres:17
46+
[...]
47+
volumes:
48+
- teslamate-db-17:/var/lib/postgresql/data
49+
[...]
50+
volumes:
51+
teslamate-db-17:
2452
```
2553
54+
Here we updated the image to `postgres:17` and the volume to `teslamate-db-17`.
55+
56+
:::note
57+
There are THREE changes. All are very important.
58+
:::
59+
60+
:::note
61+
Do not delete the old volume until you are sure that the database restore is
62+
working correctly and has all your data.
63+
:::
64+
65+
6. Start the container.
66+
2667
```bash
2768
docker compose up -d database
2869
```
2970

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

0 commit comments

Comments
 (0)