126 lines
4.2 KiB
Markdown
126 lines
4.2 KiB
Markdown
# ep-docker-backup
|
|
|
|
Scripts to back up and restore all Docker containers on a host, including
|
|
everything needed to recreate them (config, volumes, bind mounts, and
|
|
database dumps), while avoiding container downtime wherever possible.
|
|
|
|
## Requirements
|
|
|
|
- Linux Docker host with `bash`, `docker`, `tar`
|
|
- `jq` (optional but recommended — without it, `recreate.sh` generation and
|
|
volume/bind-mount copying are skipped, and only `inspect.json` is saved)
|
|
|
|
## Files
|
|
|
|
| File | Purpose |
|
|
|---|---|
|
|
| `docker-backup.sh` | Backs up all (or selected) containers into `.tar.bz2` archives |
|
|
| `docker-restore.sh` | Restores a container from an archive produced by `docker-backup.sh` |
|
|
|
|
## Backing up
|
|
|
|
```bash
|
|
chmod +x docker-backup.sh docker-restore.sh
|
|
./docker-backup.sh
|
|
```
|
|
|
|
### Options
|
|
|
|
| Option | Description | Default |
|
|
|---|---|---|
|
|
| `-o, --output-dir DIR` | Where archives are written | `/var/backups/docker` |
|
|
| `-c, --containers LIST` | Comma separated container names to back up | all containers |
|
|
| `-k, --keep N` | Keep only the N newest archives per container | `0` (keep all) |
|
|
| `-n, --dry-run` | Print the actions that would be taken, without doing them | off |
|
|
| `-h, --help` | Show usage | |
|
|
|
|
### Examples
|
|
|
|
```bash
|
|
# Back up every container, keep the 5 most recent archives each
|
|
./docker-backup.sh -k 5
|
|
|
|
# Back up only specific containers to a custom location
|
|
./docker-backup.sh -o /mnt/nas/docker-backups -c webapp,redis-cache
|
|
|
|
# Preview what would happen without touching anything
|
|
./docker-backup.sh --dry-run
|
|
```
|
|
|
|
Each run produces one archive per container:
|
|
|
|
```
|
|
<output-dir>/<container>_backup_<timestamp>.tar.bz2
|
|
```
|
|
|
|
### Archive contents
|
|
|
|
```
|
|
<container>/
|
|
├── inspect.json # full `docker inspect` output
|
|
├── recreate.sh # generated `docker run` equivalent (requires jq)
|
|
├── db/ # native database dump, if a known DB engine was detected
|
|
│ └── dump.sql | dump.archive.gz | dump.rdb
|
|
├── volumes/<name>/ # copy of each named volume's contents
|
|
└── binds/<path>/ # copy of each bind-mounted directory
|
|
```
|
|
|
|
## How downtime is avoided
|
|
|
|
| Container type | Strategy | Downtime |
|
|
|---|---|---|
|
|
| MySQL / MariaDB | `mysqldump --all-databases` via `docker exec` | None |
|
|
| PostgreSQL / PostGIS | `pg_dumpall` via `docker exec` | None |
|
|
| MongoDB | `mongodump --archive --gzip` via `docker exec` | None |
|
|
| Redis | `BGSAVE`, then copy the resulting `dump.rdb` | None |
|
|
| Everything else | Live copy of volumes/bind mounts while the container keeps running | None |
|
|
|
|
Database credentials are read automatically from the container's environment
|
|
variables (`MYSQL_ROOT_PASSWORD`, `MARIADB_ROOT_PASSWORD`, `POSTGRES_USER`).
|
|
|
|
### Forcing a container to stop during backup
|
|
|
|
Some containers (e.g. file-based databases with no native dump tool) need to
|
|
be stopped briefly for a consistent backup. Opt in per-container with a
|
|
label — nothing is stopped unless you explicitly ask for it:
|
|
|
|
```bash
|
|
docker run -d --label docker-backup.mode=stop ...
|
|
```
|
|
|
|
The script stops the container, takes the backup, and restarts it
|
|
automatically afterwards (even on failure, via the archive step still
|
|
running after restart).
|
|
|
|
### Skipping a container
|
|
|
|
```bash
|
|
docker run -d --label docker-backup.mode=skip ...
|
|
```
|
|
|
|
## Restoring
|
|
|
|
```bash
|
|
./docker-restore.sh /var/backups/docker/webapp_backup_20261001-120000.tar.bz2 --start
|
|
```
|
|
|
|
This will:
|
|
|
|
1. Extract the archive.
|
|
2. Recreate any named volumes and copy their data back in.
|
|
3. Print the original host paths for bind mounts so you can copy that data
|
|
back manually (host paths are never overwritten automatically).
|
|
4. Run `recreate.sh` to recreate the container from its image/config.
|
|
5. Print the exact command to restore a database dump if one is present
|
|
(DB restores are intentionally left as a manual, reviewed step).
|
|
6. Start the container if `--start` was passed.
|
|
|
|
## Notes / limitations
|
|
|
|
- Designed for a single Docker host (not Swarm/Kubernetes).
|
|
- `recreate.sh` covers image, env vars, published ports, restart policy,
|
|
network mode, and mounts — review it before running against a different
|
|
host, since paths/ports may need adjusting.
|
|
- Bind-mount restores are deliberately manual to avoid silently overwriting
|
|
unrelated host data.
|