mysql每天定时自动全库备份、灾备、docker

原文链接https://www.aiprose.com/blog/120

之前没有意识,在ECS上自己安装的mysql没有自动备份,偶然发现,服务器被黑客入侵,把我的mysql数据库全部删除后,勒索我要比特币,当时也很无语,因为完全么有备份,也没有开启日志,所以只能认倒霉了,然后就研究了怎么进行定时备份,有的是docker环境下运行的,有的是宿主机直接安装。

1.创建几个目录**

data 存放备份数据,logs存放每天的日志

注意:目录可以自己随意改,但是一定要修改脚本里的路劲
[图片上传失败...(image-5f04db-1597990028667)]

一.直接安装,非docker模式

2.然后编写脚本 vi backup.sh

#!/bin/bash
#备份目录
BACKUP_ROOT=/data/mysqlbak
BACKUP_FILEDIR=$BACKUP_ROOT/data
#当前日期
DATE=$(date +%Y%m%d)

#查询所有数据库
DATABASES=$(mysql  -uroot -p'zgmsoft$123' -e "show databases" | grep -Ev "Database|sys|information_schema|performance_schema|mysql")
#循环数据库进行备份
for db in $DATABASES
do
echo
# 过滤有些不需要备份的数据库
if [ "${db}" = "move_oct09" ] || [ "${db}" = "wisdomtourism" ];then
echo "jump over ${db}"
else
echo ----------$BACKUP_FILEDIR/${db}_$DATE.sql.gz BEGIN----------
mysqldump -uroot -p'zgmsoft$123' --default-character-set=utf8 -q --lock-all-tables --flush-logs -E -R --triggers -B ${db} | gzip > $BACKUP_FILEDIR/${db}_$DATE.sql.gz
echo ${db}
echo ----------$BACKUP_FILEDIR/${db}_$DATE.sql.gz COMPLETE----------
fi
done
echo "done"

如果需要进行灾备,就是吧备份文件复制到其他的服务器上,就这样写,利用scp远程复制,可能服务器需要安装一下expect

#!/bin/bash
#备份目录
BACKUP_ROOT=/data/mysqlbak
BACKUP_FILEDIR=$BACKUP_ROOT/data
#当前日期
DATE=$(date +%Y%m%d)

#查询所有数据库
DATABASES=$(mysql  -uroot -p'zgmsoft$123' -e "show databases" | grep -Ev "Database|sys|information_schema|performance_schema|mysql")
#循环数据库进行备份
for db in $DATABASES
do
echo
if [ "${db}" = "move_oct09" ] || [ "${db}" = "wisdomtourism" ];then
echo "jump over ${db}"
else
echo ----------$BACKUP_FILEDIR/${db}_$DATE.sql.gz BEGIN----------
mysqldump -uroot -p'zgmsoft$123' --default-character-set=utf8 -q --lock-all-tables --flush-logs -E -R --triggers -B ${db} | gzip > $BACKUP_FILEDIR/${db}_$DATE.sql.gz
echo ${db}
echo ----------$BACKUP_FILEDIR/${db}_$DATE.sql.gz COMPLETE----------
echo
echo ----------scp 226  begin----------
expect -c "
    spawn scp -r /data/mysqlbak/data/${db}_$DATE.sql.gz root@你的服务器地址:/data/mysqlbak/118mysql/
    expect {
        \"*assword\" {set timeout 300; send \"你的服务器密码\r\"; exp_continue;}
        \"yes/no\" {send \"yes\r\";}
    }
expect eof"
echo ----------scp 226  end---------
fi
done

echo "done"

docker 环境

2.创建脚本文件 vi backup.sh

注意一点:docker exec -i ,这里不能加t,因为么有tty输入,所以会报错 the input device is not a TTY

#!/bin/bash
#备份目录
BACKUP_ROOT=/data/mysqlbak
BACKUP_FILEDIR=$BACKUP_ROOT/data
#当前日期
DATE=$(date +%Y%m%d)

#查询所有数据库
DATABASES=$(docker exec -i root_mysql_1_8c6548532b81 mysql  -uroot -pyasaka.00 -e "show databases" | grep -Ev "Database|sys|information_schema|performance_schema|mysql")
#循环数据库进行备份
for db in $DATABASES
do
echo
if [[ "${db}" =~ "+" ]] || [[ "${db}" =~ "|" ]];then
echo "jump over ${db}"
else
echo ----------$BACKUP_FILEDIR/${db}_$DATE.sql.gz BEGIN----------
docker exec -i root_mysql_1_8c6548532b81  mysqldump -uroot -pyasaka.00 --default-character-set=utf8 -q --lock-all-tables --flush-logs -E -R --triggers -B ${db} | gzip > $BACKUP_FILEDIR/${db}_$DATE.sql.gz
echo ${db}
echo ----------$BACKUP_FILEDIR/${db}_$DATE.sql.gz COMPLETE----------
echo
fi
done

echo "done"

如果需要进行灾备,就是吧备份文件复制到其他的服务器上,就这样写,利用scp远程复制,可能服务器需要安装一下expect

#!/bin/bash
#备份目录
BACKUP_ROOT=/data/mysqlbak
BACKUP_FILEDIR=$BACKUP_ROOT/data
#当前日期
DATE=$(date +%Y%m%d)

#查询所有数据库
DATABASES=$(docker exec -i root_mysql_1_8c6548532b81 mysql  -uroot -pyasaka.00 -e "show databases" | grep -Ev "Database|sys|information_schema|performance_schema|mysql")
#循环数据库进行备份
for db in $DATABASES
do
echo
if [[ "${db}" =~ "+" ]] || [[ "${db}" =~ "|" ]];then
echo "jump over ${db}"
else
echo ----------$BACKUP_FILEDIR/${db}_$DATE.sql.gz BEGIN----------
docker exec -i root_mysql_1_8c6548532b81  mysqldump -uroot -pyasaka.00 --default-character-set=utf8 -q --lock-all-tables --flush-logs -E -R --triggers -B ${db} | gzip > $BACKUP_FILEDIR/${db}_$DATE.sql.gz
echo ${db}
echo ----------$BACKUP_FILEDIR/${db}_$DATE.sql.gz COMPLETE----------
echo
echo ----------scp 109.5  begin----------
expect -c "
    spawn scp -P 22 -r /data/mysqlbak/data/${db}_$DATE.sql.gz root@你的服务器地址:/data/mysqlbak/aiprose/
    expect {
        \"*assword\" {set timeout 300; send \"你的服务器密码\r\"; exp_continue;}
        \"yes/no\" {send \"yes\r\";}
    }
expect eof"
echo ----------scp 109.5  end---------
fi
done

echo "done"

3.编写清除旧数据脚本 backup_clean.sh

#!/bin/bash
echo ----------CLEAN BEGIN----------
find /data/mysqlbak/data -mtime +7 -name "*.gz" -exec rm -rf {} \;
echo ----------CLEAN COMPLETE---------

4.设置定时任务

脚本编写好了,需要加到系统的定时任务里,负责需要手动执行

查看定时任务
crontab -l

修改定时任务
crontab -e

修改定时任务的时候增加如下,记得修改为自己的脚本路径,下面是凌晨2点自动清理数据,凌晨3点自动备份数据

00 2 * * * /data/mysqlbak/mysql_back/backup_clean.sh > /data/mysqlbak/logs/backup_full_clean.log 2>&1
00 3 * * * /data/mysqlbak/mysql_back/aiprose.sh > /data/mysqlbak/logs/backup.log 2>&1

推薦閱讀更多精彩內容