4.1 Main Methods of Backup and Data Recovery
If you've ever lost important data even once, you don't need an explanation for why backups are important. In this lecture, we'll cover different methods and tools for creating backups and recovering data in Docker, including volumes and bind mounts.
Main Methods of Backup and Data Recovery
- Backing up Docker Volumes
- Using the tar utility for backups
- Backing up Databases Inside Containers
- Restoring Data from Backups
4.2 Docker Volume Backups
Docker volumes offer a convenient way to store data outside of containers. You can use different approaches to create volume backups.
Example of using the docker run command for volume backup
Creating a volume backup
Run a container that mounts the volume and use the tar utility to create a data archive:
docker run --rm -v my_volume:/data -v /path/to/backup:/backup busybox tar czf
/backup/my_volume_backup.tar.gz -C /data .
In this example, the contents of the my_volume
volume are archived into a file my_volume_backup.tar.gz
, which is saved in the /path/to/backup
directory on the host.
Example of restoring a volume from a backup
Restoring data into a volume
Run a container that mounts the volume and the backup directory, and use the tar
utility to extract the data:
docker run --rm -v my_volume:/data -v /path/to/backup:/backup busybox tar xzf
/backup/my_volume_backup.tar.gz -C /data
In this example, the data from the my_volume_backup.tar.gz
archive is extracted into the my_volume
volume.
4.3 Using the tar
utility for backups
The tar
utility is a powerful tool for creating backups and recovering data. It can be used both inside containers and on the host system.
Example of creating a backup inside a container
Run the container with the required data and create an archive using tar
:
docker exec -t my_container tar czf /backup/my_container_backup.tar.gz -C /data .
In this example, the data from the /data
directory inside the my_container
container is archived into the file my_container_backup.tar.gz
.
Example of restoring data from an archive
Run the container and use tar to extract the data from the archive:
docker exec -t my_container tar xzf /backup/my_container_backup.tar.gz -C /data
4.4 Database Backups Inside Containers
For databases, specialized tools are often used for backups and restoration.
Example of a PostgreSQL database backup
Use the pg_dump
utility to create a database backup:
docker exec -t my_postgres_container pg_dump -U myuser mydb > /path/to/backup/mydb_backup.sql
In this example, the database mydb
is exported to the file mydb_backup.sql
.
Example of restoring a PostgreSQL database
Use the psql
utility to restore the database from the backup:
cat /path/to/backup/mydb_backup.sql | docker exec -i my_postgres_container psql -U myuser mydb
4.5 Restoring Data from Backups
Data recovery is the process of retrieving data from previously created backups.
Example of restoring data from a volume backup
Use the docker run
command to extract data into the volume:
docker run --rm -v my_volume:/data -v /path/to/backup:/backup busybox tar xzf
/backup/my_volume_backup.tar.gz -C /data
Example of restoring a MySQL database
Use the mysqldump
utility to create a backup of the database:
docker exec my_mysql_container mysqldump -u myuser -p mypassword mydb >
/path/to/backup/mydb_backup.sql
Use the mysql utility to restore the database:
cat /path/to/backup/mydb_backup.sql | docker exec -i my_mysql_container mysql -u myuser -p
mypassword mydb
GO TO FULL VERSION