Skip to content

Instantly share code, notes, and snippets.

@johnnybui
Forked from spalladino/mysql-docker.sh
Last active November 1, 2018 03:40
Show Gist options
  • Select an option

  • Save johnnybui/67ee8b9f9d172f0daf15d22608778ecd to your computer and use it in GitHub Desktop.

Select an option

Save johnnybui/67ee8b9f9d172f0daf15d22608778ecd to your computer and use it in GitHub Desktop.
Backup and restore a MySQL database from a running Docker MySQL container
#!/bin/sh
CONTAINER="CONTAINER"
USER="USER"
PASSWORD="PASSWORD"
DATABASE="DATABASE"
FILE="backup.sql"
echo "User: $USER"
if [ -n "$PASSWORD" ]
then
echo "Using password: YES"
else
echo "Using password: NO"
fi
echo "Database: $DATABASE"
echo "Backing up database..."
if
docker exec $CONTAINER /usr/bin/mysqldump -u $USER --password=$PASSWORD $DATABASE > $FILE
then
echo "Successfully backed-up the database into $FILE."
else
echo "Database backup failed."
fi
#!/bin/sh
CONTAINER="CONTAINER"
USER="USER"
PASSWORD="PASSWORD"
DATABASE="DATABASE"
FILE="backup.sql"
echo "Container: $CONTAINER"
echo "User: $USER"
if [ -n "$PASSWORD" ]
then
echo "Using password: YES"
else
echo "Using password: NO"
fi
echo "Database: $DATABASE"
echo "Restoring database..."
if
cat $FILE | docker exec -i $CONTAINER /usr/bin/mysql -u $USER --password=$PASSWORD $DATABASE
then
echo "Successfully restored the database from $FILE."
else
echo "Database restore failed."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment