在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
一、遇到的问题我们使用过mysqldump都知道,使用该命令后,需要我们手动输入 mysql的密码,那么我们就不能够直接在crontab中使用mysqldump实现周期备份。其实我们可以使用expect脚本自动输入密码,从而实现真正的周期备份。如果你不知道什么是expect,建议先请看这篇文章:https://www.ogeek.net/article/197865.htm 二、思路
思路如下: 三、代码3.1、单机冷备份 (1)mysqldump的shell脚本 #!/bin/bash mysql_username=$1 backup_databases=$2 backup_path=$3 mysqldump -u ${mysql_username} -p --databases ${backup_databases} > ${backup_path} (2)执行mysqldump的expect脚本,能帮助我们自动输入mysql代码 #!/usr/bin/expect set timeout 5 #设置本机信息 set mysql_username [lindex $argv 0] set backup_database [lindex $argv 1] set backup_path [lindex $argv 2] #utils路径 set utils_path /home/hadoop/backup_script/utils spawn bash ${utils_path}/backup.sh ${mysql_username} ${backup_database} ${backup_path} expect { "*assword*" {send "nimabidecao1\r"} #输入密码 } expect eof (3)驱动脚本,执行expect,这里可以传入需要的参数 #!/bin/bash #这里的数据就可以写死了 mysql_username=root backup_databases=school backup_path=$HOME/backup_data/${backup_databases}.sql #运行expect脚本 expect $HOME/backup_script/single_cold_backup/single_cold_backup_service.exp ${mysql_username} ${backup_databases} ${backup_path} 这里一定要十分注意自己的路径,强烈建议使用绝对路径来执行 (4)使用crontab周期执行驱动脚本 0 9 * * 1 bash /home/hadoop/backup_script/single_cold_backup/single_cold_backup_driver.sh 意思为:每周1早上9点执行一次备份
(1)复制远程文件,用于拷贝本地机的mysql备份文件 #!/bin/bash local_backup_path=$1 another_user=$2 another_ip=$3 another_backup_path=$4 scp ${local_backup_path} ${another_user}@${another_ip}:${another_backup_path} (2)执行mysqldump的expect脚本,能帮助我们自动输入mysql代码 #!/usr/bin/expect set timeout 5 #主机信息 set mysql_username [lindex $argv 0] set backup_database [lindex $argv 1] set backup_path [lindex $argv 2] #从机信息 set slave_user [lindex $argv 3] set slave_ip [lindex $argv 4] set slave_backup_path [lindex $argv 5] #utils路径 set utils_path /home/hadoop/backup_script/utils spawn bash ${utils_path}/backup.sh ${mysql_username} ${backup_database} ${backup_path} expect { "*assword*" {send "nimabidecao1\r"} #输入密码 } spawn bash ${utils_path}/scp.sh ${backup_path} ${slave_user} ${slave_ip} ${slave_backup_path} expect { "*assword*" {send "nimabidecao1\r"} #输入密码 } expect eof (3)驱动脚本,执行expect,这里可以传入需要的参数 #!/bin/bash #本机信息 mysql_username=root backup_databases=school backup_path=$HOME/backup_data/${backup_databases}.sql #从机信息 slave_user=meizhaowei slave_ip=172.20.10.14 slave_backup_path=backup_data/doule_cold_backup_data/${backup_databases}.sql #执行写绝对路径 expect $HOME/backup_script/double_cold_backup/double_cold_backup_service.exp ${mysql_username} ${backup_databases} ${backup_path} ${slave_user} ${slave_ip} ${slave_backup_path} (4)使用crontab周期执行驱动脚本 0 9 * * 1 bash /home/hadoop/backup_script/double_cold_backup/double_cold_backup_driver.sh 意思是:每周1早上9点执行一次备份 到此这篇关于linux使用mysqldump+expect+crontab实现mysql周期冷备份思路详解的文章就介绍到这了,更多相关mysql周期冷备份内容请搜索极客世界以前的文章或继续浏览下面的相关文章希望大家以后多多支持极客世界! |
请发表评论