-
-
Save johnnybui/67ee8b9f9d172f0daf15d22608778ecd to your computer and use it in GitHub Desktop.
Backup and restore a MySQL database from a running Docker MySQL container
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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