数据库备份并同步到minio脚本

#!/bin/bash

# 定义host、账号、密码,需要自定义
mysql_host='80.32.19.1'
mysql_user='root'
mysql_passwd='123456'
mysql_port='3306'

# 定义备份目录、备份库、文件格式,需要自定义
bak_dir='/tmp/mysql-bak'
mkdir -p $bak_dir
date_day=`date +%F`
xsky_prefix='minio/db-bak'
# 数组,需要备份的库都写在里面,需要自定义
bak_db_name=('test_apply1','test_apply2','test_apply3')
#bak_db_name=('nacos_config')
# 开始备份
#db_num=`echo ${#bak_db_name[@]}`
for db_name in ${bak_db_name[@]};do
    [[ -z $db_name ]] && exit 1
    mysqldump -u$mysql_user -p$mysql_passwd -h $mysql_host -P $mysql_port $db_name > $bak_dir/$db_name.$date_day
    if [[ $? -eq 0 ]];then
    	cd $bak_dir && tar zcf $db_name.$date_day.tar  $db_name.$date_day
    	# 备份至minio,备份后会删除本机文件
    	mc cp $db_name.$date_day.tar $xsky_prefix/$db_name.$date_day >> /dev/null
        if [[ $? -eq 0 ]];then
            rm -f $bak_dir/$db_name.$date_day.tar
            rm -f $bak_dir/$db_name.$date_day
        fi
    fi
done


0 1 * * * /usr/bin/sh /root/soft/mysql-bak/mysql-bak.sh
posted @ 2023-10-20 14:07  村尚chun叔  阅读(163)  评论(0编辑  收藏  举报