用docker自动备份mysql数据库

/data/service/mysql_backup/docker-compose.yml文件内容

version: "2"
services:
    mysql_backup:
        image: xxfaxy/self-alpine-mysql-backup
        container_name: mysql_backup
        environment:
            - READ_DATABASE_HOST=主机
            - READ_DATABASE_PORT=端口
            - READ_DATABASE_NAME=数据库名称
            - READ_DATABASE_USERNAME=用户名
            - READ_DATABASE_PASSWORD=密码
        volumes:
            - /home/test/mysql_backup:/data/mysql
        restart: always

/data/service/mysql_backup/remote.sh文件内容

#!/bin/bash
echo 'please select:'
echo '1.docker images -a'
echo '2.docker ps -a'
echo '3.docker rm -f @container'
echo '4.docker rmi -f @image'
echo '5.docker pull @image'
echo '6.docker-compose up -d'
echo '7.docker exec -it @container sh'
image=xxfaxy/self-alpine-mysql-backup
container=mysql_backup
count=$#
if [ $count -gt 0 ]
    then
        name=$1
else
    read name
fi
case $name in
    '1')
        sudo docker images -a
        ;;
    '2')
        sudo docker ps -a
        ;;
    '3')
        sudo docker rm -f $container
        ;;
    '4')
        #sudo docker rmi -f $image
        ;;
    '5')
        sudo docker pull $image
        ;;
    '6')
        sudo docker-compose --file=/data/service/mysql_backup/docker-compose.yml up -d
        ;;
    '7')
        sudo docker exec -it $container sh
        ;;
    *)
        echo "not find"
        ;;
esac

xxfaxy/self-alpine-mysql-backup镜像大小为56.77 MB

你也可以自己制作镜像,决定备份的细节,下面是源码

https://github.com/xxfaxy/mysql_backup

我的首发博客

https://www.zhouchun.net/blog/show/340

转载于:https://my.oschina.net/u/1180033/blog/909294

你可能感兴趣的:(用docker自动备份mysql数据库)