303 words
2 minutes
Docker Backup and Restore in Two Simple Steps

Docker Backup and Restore in Two Simple Steps#

Learn how to create a compressed backup of your Docker environment, including containers, volumes, and Caddy configurations, and easily restore it when needed.

Backup#

  1. On your remote server, run:
(cd ~/ && sudo systemctl stop docker && sudo tar -cpf - ~/docker /var/lib/docker/volumes /etc/caddy | zstd -3 -o "backup-$(date +'%Y%m%d_%H%M%S').tar.zst" && sudo systemctl start docker)

This command stops Docker, creates a compressed backup of your Docker data (including containers, volumes, and Caddy configuration) in the home directory, and then restarts Docker.

  • Replace ~/docker with the path to the folder where your Docker containers are stored. You can also specify multiple folders by separating them with spaces, like ~/docker ~/astro.
  • If you don’t use Caddy for reverse proxying, remove /etc/caddy from the command.
  1. On your local computer, run:
rsync -ahPvz -e 'ssh -p 2222' [email protected]:~/backup-$(date +'%Y%m%d')*.tar.zst .

This command transfers the backup file from your remote server to your local computer.

  • Replace [email protected] with your server’s address.
  • If you don’t need to specify an SSH port, replace 'ssh -p 2222' with ssh.

Restore#

Backup your remote server before proceeding, in case the restore overwrites important data and you need to recover it.

  1. On your local computer, run:
sudo tar --use-compress-program=zstd -xvf backup-$(date +'%Y%m%d')*.tar.zst

This command extracts the backup file.

rsync -ahvzP -e 'ssh -p 2222' /local/path/ [email protected]:/remote/path/

This command copies the extracted files from your local computer to your remote server.

  • Replace /local/path/ with the folder on your local computer, and /remote/path/ with the folder on your remote server.

Quick Reference#

Backup#

# On VPS
(cd ~/ && sudo systemctl stop docker && sudo tar -cpf - ~/docker /var/lib/docker/volumes /etc/caddy | zstd -3 -o "backup-$(date +'%Y%m%d_%H%M%S').tar.zst" && sudo systemctl start docker)

# On local computer
rsync -ahPvz -e 'ssh -p 2222' [email protected]:~/backup-$(date +'%Y%m%d')*.tar.zst .

Restore#

# On local computer
sudo tar --use-compress-program=zstd -xvf backup-$(date +'%Y%m%d')*.tar.zst
rsync -ahvzP -e 'ssh -p 2222' docker/ [email protected]:~/docker/
rsync -ahvzP -e 'ssh -p 2222' docker/forgejo/ [email protected]:~/docker/forgejo/
Docker Backup and Restore in Two Simple Steps
https://blog.juyoun.gg/posts/en/2025/01/02/
Author
Jay
Published at
2025-01-02
License
CC BY