Skip to content

Instantly share code, notes, and snippets.

@lukeurban
Created August 4, 2021 15:58
Show Gist options
  • Select an option

  • Save lukeurban/c6492f65ae1c25b4aa64e3276c6cc62f to your computer and use it in GitHub Desktop.

Select an option

Save lukeurban/c6492f65ae1c25b4aa64e3276c6cc62f to your computer and use it in GitHub Desktop.
Importing database backup with progress bar to running docker container

Importing database backup with progress bar to running docker container.

Use a MySQL image with pv installed.

FROM mysql:8.0

RUN apt-get update 
RUN apt-get install -y pv

COPY my.cnf /etc/mysql/conf.d/my.cnf

I share a backup volume(/app/backup/) with a database container. then I have a sh script that looks like this:

# load .env
export $(cat .env | sed 's/#.*//g' | xargs)

restore_database() {
  container_id=$(docker ps -f name=$MYSQL_HOST -q | tail -n1)
  docker exec -it $container_id sh -c 'pv /app/backup/backup.sql | /usr/bin/mysql -u $MYSQL_ROOT_USER --password=$MYSQL_ROOT_PASSWORD $MYSQL_DATABASE'
}

# anable running different fuctions from console
$@

backup.sql I swapped for the backup I want to restore. The result*: screenshot * I use makefile as a wrapper and sh scripts as my fuctions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment