CodeGym /Courses /Docker SELF /Backups and Data Recovery

Backups and Data Recovery

Docker SELF
Level 19 , Lesson 3
Available

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

  1. Backing up Docker Volumes
  2. Using the tar utility for backups
  3. Backing up Databases Inside Containers
  4. 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:

Terminal

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:

Terminal

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:

Terminal

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:

Terminal

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:

Terminal

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:

Terminal

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:

Terminal

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:

Terminal

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:

Terminal

cat /path/to/backup/mydb_backup.sql | docker exec -i my_mysql_container mysql -u myuser -p
mypassword mydb
3
Task
Docker SELF, level 19, lesson 3
Locked
Creating a Backup of a Volume
Creating a Backup of a Volume
3
Task
Docker SELF, level 19, lesson 3
Locked
Restoring data in a volume
Restoring data in a volume
3
Task
Docker SELF, level 19, lesson 3
Locked
Backup of a PostgreSQL database
Backup of a PostgreSQL database
3
Task
Docker SELF, level 19, lesson 3
Locked
Automating Volume Backups
Automating Volume Backups
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION