Restore a named volume from an archive

Destructive
What do risk levels mean?
Read-only
Inspects state without changing anything.
Low risk
Reversible with a routine follow-up command.
Medium risk
Changes state; undo path documented.
Destructive
Deletes or overwrites; confirmation required.

Extracts a tar backup created by the volume backup recipe into a named volume, overwriting files that also exist in the archive. Used to recover data or move a volume between hosts.

dockervolumerestorebackup

Confirmation required

This recipe is destructive and requires confirmation — confirm you understand the impact before running any command below.

Parameters

Name of the Docker volume to restore into; created automatically if missing.

Path to the .tar.gz backup in the current directory, such as mydata-backup.tar.gz.

Commands

List the archive's contents so you can see what will be written into the volume

tar tzf <archive>

Extract the archive into the volume, overwriting files that exist in both

docker run --rm -v <volume>:/data -v "$PWD":/backup alpine tar xzf /backup/<archive> -C /data

Verification

docker run --rm -v <volume>:/data:ro alpine ls -la /data

The volume contains the files listed in the archive.

Undo

Not undoable

Files in the volume that shared a path with archive entries are overwritten and their previous contents cannot be recovered unless you made a fresh backup of the volume first.

Pitfalls

  • Take a backup of the volume's current state first if it holds anything valuable; restore overwrites in place.
  • Files present in the volume but absent from the archive are left behind, which can produce a mixed state; restore into a fresh volume for an exact copy.
  • Stop containers using the volume before restoring; a running application may read half-restored data or overwrite it.

Related