# Backing Up and Resetting Data — Docker Compose

Source: https://www.skillbyai.com/en/docker-compose/d-backup

> Data in volumes needs a plan.

## Dump, copy, restore

Back up databases with their own tools (`pg_dump`, `mysqldump`) run through `docker compose exec`, or copy a volume's contents with a temporary container. Test restores. For development, seed scripts and fixtures make it easy to reset data with `down -v` followed by `up`. Never rely on a single local volume for data you cannot lose.

## Dump and restore a database (sketch)

Commands that start containers need a running Docker engine; not run here.

```bash
# backup
docker compose exec -T db pg_dump -U shop shop > backup.sql
# restore into a fresh database
docker compose down -v && docker compose up -d db
docker compose exec -T db psql -U shop shop < backup.sql
```

## Script your resets

A make reset or npm run db:reset command that recreates and seeds the database saves everyone time.

**Quiz:** What is the safest way to back up PostgreSQL data in Compose?

- [ ] Rely on docker compose down
- [ ] Copy the container image
- [x] Use pg_dump through docker compose exec and test the restore
- [ ] Take a screenshot of the data

*Answer:* Use pg_dump through docker compose exec and test the restore. Use database-aware tools.
