MySQL定时自动备份与自动恢复
需求:主机A每日凌晨三点自动备份dump,然后发送的主机B上。主机B每日凌晨四点自动恢复该dump。
主机AIP:10.180.50.24
主机BIP:10.180.50.23
1、主机Ashell脚本:backup.sh
#!/bin/sh cd /bak/bakmysql echo "============MySQL自动备份开始============" BackFolder=/usr/bak/mysqldump Now=$(date +"%Y%m%d") file=_dump_$Now.sql.gz echo "[1/5]移动前一日备份文件开始..." mv *_dump_* $BackFolder echo "移动前一日备份文件完成" echo "---------------------------------" echo "[2/5]数据库scpnf备份开始..." mysqldump -udump -p'dump' scpnf | gzip > scpnf$file echo "数据库scpnf备份完成" echo "---------------------------------" echo "[3/5]数据库fecs备份开始..." mysqldump -udump -p'dump' fecs | gzip > fecs$file echo "数据库fecs备份完成" echo "---------------------------------" echo "[4/5]远程复制数据库备份文件开始..." scp *$file [email protected]:/usr/dbbackup echo "远程复制数据库备份文件完成" echo "---------------------------------" echo "[5/5]三天前备份文件清理开始..." RetainDay=$(date -d -3day +"%Y%m%d") RetainDayFile=_dump_$RetainDay.sql.gz if [ -f $BackFolder/scpnf$RetainDayFile ] then rm -rf $BackFolder/scpnf$RetainDayFile echo "成功删除备份文件scpnf$RetainDayFile" else echo "无法找到备份文件scpnf$RetainDayFile" fi if [ -f $BackFolder/fecs$RetainDayFile ] then rm -rf $BackFolder/fecs$RetainDayFile echo "成功删除备份文件fecs$RetainDayFile" else echo "无法找到备份文件fecs$RetainDayFile" fi echo "============MySQL自动备份完成============"
2、将主机Ashell脚本:backup.sh加入计划任务
vim /etc/crontab 0 3 * * * root /bak/bakmysql/backup.sh
3、主机B自动恢复shell脚本:mysql_restore.sh
#################################################################### # #说明:每天凌晨4点自动从备份脚本恢复数据库scpnf和fecs库,并删除三天前的备份文件。 # #################################################################### #!/bin/sh cd /usr/dbbackup echo "======================================" #备份文件名称 File=_dump_$(date +"%Y%m%d").sql.gz echo "1/3:数据库[fecs]开始恢复..." gunzip < fecs$File | mysql -uroot -p'password' fecs echo "数据库[fecs]恢复成功!" echo "------------------------------------" echo "2/3:数据库[scpnf]开始恢复..." gunzip < scpnf$File | mysql -uroot -p'password' scpnf echo "数据库[scpnf]恢复成功!" echo "------------------------------------" echo "3/3:准备删除三天前的数据备份文件..." RetainDays=$(date -d -3day +"%Y%m%d") #删除fecs备份文件 if [ -f ./fecs_dump_$RetainDays.sql.gz ] then rm -rf ./fecs_dump_$RetainDays.sql.gz echo "成功删除文件:fecs_dump_$RetainDays.sql.gz" else echo "没有找到备份文件:fecs_dump_$RetainDays.sql.gz" fi #删除scpnf备份文件 if [ -f ./scpnf_dump_$RetainDays.sql.gz ] then rm -rf ./scpnf_dump_$RetainDays.sql.gz echo "成功删除文件:scpnf_dump_$RetainDays.sql.gz" else echo "没有找到备份文件:scpnf_dump_$RetainDays.sql.gz" fi echo "======================================"
4、将主机Bshell脚本:backup.sh加入计划任务
vim /etc/crontab 0 4 * * * root /usr/dbbackup/mysql_restore.sh
相关推荐
laisean 2020-11-11
Julyth 2020-10-16
laisean 2020-09-27
flycappuccino 2020-09-27
liguojia 2020-09-27
87201442 2020-10-15
MXstudying 2020-09-05
WasteLand 2020-09-15
<?php. if (!empty($_POST)) {. $data1 = $_POST["data1"];$data2 = $_POST["data2"];$fuhao = $_POST["fuh
mathchao 2020-09-15
tvk 2020-07-30
Zaratustra 2020-07-29
zhaowj00 2020-07-26
Zaratustra 2020-06-26
ldcwang 2020-06-25
拿什么来拯救自己 2020-06-21
IsanaYashiro 2020-06-16
赵家小少爷 2020-06-14
大牛牛 2020-06-14