Skip to content

Instantly share code, notes, and snippets.

@spalladino
Created December 22, 2015 13:47
Show Gist options
  • Save spalladino/6d981f7b33f6e0afe6bb to your computer and use it in GitHub Desktop.
Save spalladino/6d981f7b33f6e0afe6bb to your computer and use it in GitHub Desktop.
Backup and restore a mysql database from a running Docker mysql container
# Backup
docker exec CONTAINER /usr/bin/mysqldump -u root --password=root DATABASE > backup.sql
# Restore
cat backup.sql | docker exec -i CONTAINER /usr/bin/mysql -u root --password=root DATABASE
@lnfel
Copy link

lnfel commented Mar 9, 2023

Still works in 2023!

@maksam07
Copy link

Still works in 2023!

I have this page in my bookmarks. I hope it stays up forever and never gets deleted

@muzzammil194
Copy link

muzzammil194 commented Aug 21, 2023

#!/bin/bash

read -p "Select your option number:
(1) BACKUP DB From Container
(2) RESTORE DB From Container
(3) BACKUP DB local
(4) RESTORE DB local
Enter your option: " option

read -p "Enter your database Name: " DATABASE
read -p "Enter your Host: " HOST
read -p "Enter your Port: " PORT
read -p "Enter your Username: " USERNAME
read -p "Enter your Password: " PASSWORD

MY_SQL_DUMP=/usr/bin/mysqldump

if [ "$option" == "1" ]; then
read -p "Enter your Container Name or ID: " Container
docker exec "$Container" "$MY_SQL_DUMP" -u "$USERNAME" --password="$PASSWORD" "$DATABASE" > "$DATABASE.sql"
elif [ "$option" == "2" ]; then
read -p "Enter your Container Name or ID: " Container
read -p "Enter your Restore-File-Path: " PATH
cat "$PATH.sql" | docker exec -i "$Container" "$MY_SQL_DUMP" -u "$USERNAME" --password="$PASSWORD" "$DATABASE"
elif [ "$option" == "3" ]; then
sudo "$MY_SQL_DUMP" -u "$USERNAME" -p"$PASSWORD" -P "$PORT" --host="$HOST" "$DATABASE" > "$DATABASE.sql"
elif [ "$option" == "4" ]; then
read -p "Enter your Restore-File-Path: " PATH
sudo "$MY_SQL_DUMP" -u "$USERNAME" -P "$PORT" -p"$PASSWORD" "$DATABASE" < "$PATH.sql"
else
echo "Invalid option selected."
fi

@maksam07
Copy link

sudo docker

sudo docker?

@muzzammil194
Copy link

sudo docker

sudo docker?

if docker does not add in a group of Linux so it does not execute with full permission

@carlosjuanco
Copy link

Gracias por el script de respaldo @muzzammil194

@icanitam
Copy link

icanitam commented Oct 2, 2023

thanks a lot! still work

@saidbakr
Copy link

You should get Nobel prize for these two lines of code!

@wac-max
Copy link

wac-max commented Feb 9, 2024

Works perfectly. thank you!

@dangduytung
Copy link

It works, thanks.

@rajat3604
Copy link

It works, for me. Thanks

@ricardogranafirst
Copy link

Thanks!

@mehrabcz
Copy link

I've visited this link countless times! Thank you for providing such a helpful sample.

@ivanzigoni
Copy link

beautiful piece of code

@Carlo-F
Copy link

Carlo-F commented Oct 30, 2024

Perfect!

@rajkumarsakthivel
Copy link

good one @muzzammil194

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