Skip to content

Instantly share code, notes, and snippets.

@pdonorio
Last active August 29, 2015 14:21
Show Gist options
  • Save pdonorio/36218941631b03c065af to your computer and use it in GitHub Desktop.
Save pdonorio/36218941631b03c065af to your computer and use it in GitHub Desktop.
Launch a docker mysql container to mount an existing database folder and dump it into a sql.gz file

Launch a docker mysql official container to mount an existing database folder. You can choose the mysql version.

Then use mysqldump inside the container to save it into a sql.gz file.

Note: tmp dir, data dir and output dir will be all the same: the database folder.

###############################
# You can change these
dir='/path/to/localhost/dbfolder'
mysql_version='5.5'
# No need to change here
mypwd='test'
dname='dumpmysqldb'
hdir=`basename $dir`
mdir="/var/lib/mysql"
dbdir="$mdir/$hdir"
###############################
secs=5
check=$(docker ps -fq name=$dname)
if [ ! -z $check ]; then
echo 'Removing existing container in $secs seconds'
sleep $secs
docker stop $dname && docker rm -f $dname
fi
echo 'Running the container'
docker run \
-v $dir:$dbdir \
-e MYSQL_ROOT_PASSWORD=$mypwd \
--name $dname \
-d mysql:5.5
###############################
echo 'Dumping'
launchcmd="docker exec -it $dname bash -c"
$launchcmd "echo 'tmpdir=$dbdir' >> /etc/mysql/my.cnf"
$launchcmd "mysqldump -h localhost -u root -ptest --quick $hdir | gzip > $dbdir/$hdir.sql.gz "
###############################
echo "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment