Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions website/docs/installation/docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ This setup is recommended only if you are running TeslaMate **on your home netwo
- POSTGRES_PASSWORD=password #insert your secure database password!
- POSTGRES_DB=teslamate
volumes:
- teslamate-db:/var/lib/postgresql/data
- teslamate-db-17:/var/lib/postgresql/data

grafana:
image: teslamate/grafana:latest
Expand All @@ -71,7 +71,7 @@ This setup is recommended only if you are running TeslaMate **on your home netwo
- mosquitto-data:/mosquitto/data

volumes:
teslamate-db:
teslamate-db-17:
teslamate-grafana-data:
mosquitto-conf:
mosquitto-data:
Expand Down
57 changes: 36 additions & 21 deletions website/docs/maintenance/backup_restore.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ If you are using `docker-compose`, you are using Docker Compose v1, which has be

## Backup

Create backup file `teslamate.bck`:
Create backup file:

```bash
docker compose exec -T database pg_dump -U teslamate teslamate > ./teslamate.bck
docker compose exec -T database pg_dump -U teslamate teslamate > teslamate_database_$(date +%F_%H-%M-%S).sql
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about using only a date (2025-05-16 here) - that way copy and paste of commands should still work (as users are most likely updating on the same day).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, not entirely sure what you are asking for?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

going with $(date +%Y-%m-%d) and not using %H-%M-%S will allow the restore command to be a copy / paste without users manually searching for the exact filename taken earlier (assuming that most upgrades happen at the same day).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question.

Was trying to avoid the situation here where you might have a good backup, but then you accidentally run the above command again after the database is shutdown/replaced and replace the good backup with a bad backup. And not notice the glaring error message saying it failed.

Yes, would like to think this wouldn't happen, but we did get a report from somebody who had a 0 byte backup file, and this is one way that could occur.

At least with the above, there is some increased chance you might still have the good backup file.

```

:::note
`-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`
:::

:::note
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.
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.
:::

:::note
Expand All @@ -36,21 +36,36 @@ If you changed `TM_DB_USER` in the .env file from one of the advanced guides, ma
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)
:::

```bash
# Stop the teslamate container to avoid write conflicts
docker compose stop teslamate

# Drop existing data and reinitialize (Don't forget to replace first teslamate if using different TM_DB_USER)
docker compose exec -T database psql -U teslamate teslamate << .
DROP SCHEMA public CASCADE;
CREATE SCHEMA public;
CREATE EXTENSION cube WITH SCHEMA public;
CREATE EXTENSION earthdistance WITH SCHEMA public;
.

# Restore
docker compose exec -T database psql -U teslamate -d teslamate < teslamate.bck

# Restart the teslamate container
docker compose start teslamate
```
1. Stop the teslamate container to avoid write conflicts

```bash
docker compose stop teslamate
```

2. Drop existing data and reinitialize (Don't forget to replace first teslamate if using different TM_DB_USER)

```bash
docker compose exec -T database psql -U teslamate teslamate << .
DROP SCHEMA public CASCADE;
CREATE SCHEMA public;
CREATE EXTENSION cube WITH SCHEMA public;
CREATE EXTENSION earthdistance WITH SCHEMA public;
.
```

3. Restore

Use the same filename that was used in the backup step.

Replace `teslamate_database_2025-05-07_07-33-48.sql` with the actual filename generated during the backup step.

```bash
DATABASE="teslamate_database_2025-05-07_07-33-48.sql"
docker compose exec -T database psql -U teslamate -d teslamate < "$DATABASE"
```

4. Restart the teslamate container

```bash
docker compose start teslamate
```
107 changes: 98 additions & 9 deletions website/docs/maintenance/upgrading_postgres.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,117 @@ title: Upgrading PostgreSQL to a new major version
sidebar_label: Upgrading PostgreSQL
---

1. Create a [backup](backup_restore.md)
2. Stop all TeslaMate containers
In these instructions it is assumed you are upgrading from postgresql 16 to postgresql 17. Please update
references to 16 to your start version and 17 to your desired version.

These instructions also assume you have the standard `docker-compose.yml` file. If this is not the case
you might need to make adjustments. For example, older installed might call the container "db" not "database".

:::note
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/)
:::

## Upgrade

1. Create a backup:

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

2. _Check to make sure there are no obvious errors in this backup file._ There should not be any errors appearing
on screen. The file should be more than 0 bytes long. Do not proceed if there is any doubt.

3. Stop all TeslaMate containers

```bash
docker compose down
```

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

```bash
docker volume rm "$(basename "$PWD")_teslamate-db"
cp docker-compose.yml docker-compose-16.yml
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cp is not OS independent

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any suggestions? Maybe just have text "Copy file docker-compose-16.yml over docker-compose.yml?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think cp should work on all OS we care about though, right?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why should users backup the compose file. We could say - in case of error please revert your changes to the compose file to get back to a working version (that should be fine to ask for).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On one hand, I note that there is a comment that some environments may automatically delete any unexpected files, such as this backup. Oops.

On the other hand, not really convinced our users are good at following written instructions... Think spelling it out might be required.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

those would also delete the postgres backup, right?

as long as the data is still accessible everything is alright. if someone isn't able to follow a "revert changes made to docker-compose.yaml" file they still can open up a discussion / ask in forums for help.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These will delete the updated docker-compose.yml for postgres. The postgres data itself should remain intact.

At least that is the theory. Possibly some platform might decide that since the data is no longer referenced it can be deleted. Hoping that doesn't actually happen...

```

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

```yml {2}
database:
image: postgres:xx
```yaml
services:
[...]
database:
image: postgres:17
[...]
volumes:
- teslamate-db-17:/var/lib/postgresql/data
[...]
volumes:
teslamate-db-17:
```

Here we updated the image to `postgres:17` and the volume to `teslamate-db-17`.

:::note
There are THREE changes. All are very important.
:::

:::note
Do not delete the old volume until you are sure that the database restore is
working correctly and has all your data.
:::

6. Start the container.

```bash
docker compose up -d database
```

5. [Restore](backup_restore.md) the backup
7. Restore the backup.

:::note
database should be empty. If database contains data, then check you updated the volume correctly in step 5.
:::

Replace `teslamate_database_2025-05-07_07-33-48.sql` with the actual filename generated during the backup step.

```bash
docker compose exec -T database psql -U teslamate teslamate << .
CREATE SCHEMA public;
CREATE EXTENSION cube WITH SCHEMA public;
CREATE EXTENSION earthdistance WITH SCHEMA public;
.
DATABASE="teslamate_database_2025-05-07_07-33-48.sql"
docker compose exec -T database psql -U teslamate -d teslamate < "$DATABASE"
```

There should not be any errors.

8. Start the container.

```bash
docker compose up -d teslamate
```

9. Make sure everything is working, and all the data is intact.

## Roll back on error

If you cannot restore the backup for any reason, you might have to roll back to your previous version.

1. Stop everything

```bash
docker compose stop
```

2. Restore the values in `docker-compose.yml` that you changed in step 5 and step 6.

```bash
cp docker-compose-16.yml docker-compose.yml
```

3. Start everything

```bash
docker compose up -d
```
Loading