时间:2021-07-01 10:21:17 帮助过:41人阅读
服务器介绍:centos 7.0
1. 单数据库备份脚本文件
#vi mysql-backup.sh
db_user="root" #本服务器用户名密码 db_passwd="root" db_host="192.168.64.137" #本服务器地址 db_name="whp" #需要备份数据库名称 # the directory for story your backup file. backup_dir="/backup" #备份以后放入的文件路径 # date format for backup file (dd-mm-yyyy) time="$(date +"%Y-%m-%d-%H-%M-%S")" # mysql, mysqldump and some other bin's path MYSQL="/application/mysql/bin/mysql" MYSQLDUMP="/application/mysql/bin/mysqldump" GZIP="/bin/gzip" $MYSQLDUMP -u $db_user -h $db_host -p$db_passwd $db_name | $GZIP -9 > "$backup_dir/$db_name"_"$time.gz"
2. 保留7天历史数据的全备份
db_user="root" db_passwd="root" db_host="localhost" # the directory for story your backup file. backup_dir="/application/backup/" # date format for backup file (dd-mm-yyyy) time="$(date +"%H-%M-%S-%m-%d-%y")" # mysql, mysqldump and some other bin's path MYSQL="/application/mysql/bin/mysql" MYSQLDUMP="/application/mysql/bin/mysqldump" MKDIR="/bin/mkdir" RM="/bin/rm" MV="/bin/mv" GZIP="/bin/gzip" # check the directory for store backup is writeable test ! -w $backup_dir && echo "Error: $backup_dir is un-writeable." && exit # the directory for story the newest backup test ! -d "$backup_dir/backup.0/" && $MKDIR "$backup_dir/backup.0/" echo "Start to Backup..."; # get all databases # don't backup information_schema、performance_schema all_db="$($MYSQL -u $db_user -h $db_host -p$db_passwd -Bse 'show databases')" all_db=${all_db//information_schema/}; all_db=${all_db//performance_schema/}; for db in $all_db do $MYSQLDUMP -u $db_user -h $db_host -p$db_passwd $db | $GZIP -9 > "$backup_dir/backup.0/$time.$db.gz" done # delete the oldest backup test -d "$backup_dir/backup.7/" && $RM -rf "$backup_dir/backup.7" # rotate backup directory for int in 6 5 4 3 2 1 0 do if(test -d "$backup_dir"/backup."$int") then next_int=`expr $int + 1` $MV "$backup_dir"/backup."$int" "$backup_dir"/backup."$next_int" fi done echo "BackUp Success!" exit 0;
3. 修改shell脚本权限并执行
# chmod 700 mysql-backup.sh // 只允许管理员运行此脚本 #./mysql-backup.sh //执行脚本,测试一次
1. 建立脚本文件:
#vi incre-backup.sh
#执行mysqladmin执行刷新日志文件 /application/mysql/bin/mysqladmin -uroot -proot flush-logs #DATADIR=/var/lib/mysql DATADIR=/application/data #获取数据文件路径 BAKDIR=/backup #获取要备份的数据目标文件路径 ###如果mysql bin log你做了特殊设置,请修改此处或者修改应用此变量的行:缺省取机器名,mysql缺省也是取机器名 #HOSTNAME=`uname -n` cd $DATADIR #转入到/application/data 查询mysql-bin.index文件 #FILELIST=`cat $HOSTNAME-bin.index` FILELIST=`cat mysql-bin.index` ## COUNTER number COUNTER=0 for file in $FILELIST do COUNTER=`expr $COUNTER + 1 ` done NextNum=0 for file in $FILELIST do base=`basename $file` NextNum=`expr $NextNum + 1` if [ $NextNum -eq $COUNTER ] then echo "skip lastest" else dest=$BAKDIR/$base if(test -e $dest) then echo "skip exist $base" else echo "copying $base" cp $base $BAKDIR fi fi done echo "backup mysql binlog ok"
2. 修改文件权限并执行
# chmod 700 incre-backup.sh // 只允许管理员运行此脚本 #./mysql-backup.sh //执行脚本,测试一次
【自动备份】
自动备份是站在以上编写脚本的基础上,利用linux系统crontab命令,定时自动执行shell文件:
# crontab –e 添加: 00 01 * * */mysql-backup.sh // 每天凌晨1点执行
【远程局域网备份】
局域网备份利用NFS服务器实现数据在局域网共享的方式实现备份,利用在NFS服务器挂载的方式实现,就像我们平时共享文件,通过配置网络连接,让自己的文件得到共享。实现方式如下:
1. NFS服务器端安装
a)安装 NFS 服务器所需的软件包:
# yuminstall -y nfs-utils b)编辑exports文件 #vim/etc/exports #/home/nfs/192.168.248.0/24(rw,sync)
同192.168.248.0/24一个网络号的主机可以挂载NFS服务器上的/home/nfs/目录到自己的文件系统中
rw表示可读写;sync表示同步写
c)启动nfs服务
#systemctlstart rpcbind.service
#systemctlstart nfs-server.service
d)确认NFS服务器启动成功:
#rpcinfo –p
e)若没有启动成功,重新启动rpcbind和nfs-server服务:
#systemctl restart rpcbind.service
#systemctl restart nfs-server.service
客户端安装
a)安装nfs,启动rpcbind服务
# yuminstall -y nfs-utils
b)先为rpcbind做开机启动
#systemctlenable rpcbind.service
c)然后启动rpcbind服务:
systemctlstart rpcbind.service
d)检查NFS服务器端是否有共享目录:
#showmount -e 192.168.64.138
e)在从机上使用mount挂载服务器端目录到客户端某个目录下(这里将上面本地备份的文件路径挂载如服务器里面):
#mount -tnfs -o nolock,nfsvers=1,vers=3 192.168.64.138:/home/nfs /backup
问题:mount.nfs: Stale NFS file handle
解决方法:在client端把挂载文档去掉重新挂载:
# umount –a
【备份恢复】
a.将gzip数据库恢复到Mysq
#gunzip <dbname.gz | mysql -u Username -p dbname
b.二进制文件恢复数据库:
#/mysql/bin/mysqlbinlog--database=fox --start-date="2013-01-22 5:00:00" --stop-date="2013-01-22 9:00:00"/mysql/data/mysql-bin.000001 | /mysql/bin/mysql -u root -p123456 –f
database:指定具体的数据库
start-date:开始时间
stop-date:结束时间
/mysql/data/mysql-bin.000001:指定二进制文件
【总结】
基本的备份方法已经完成,但是我们要根据具体的应用场景采用不同的备份策略,保证安全的前提下又不消耗过多内存。
以上就是Centos7 实现MySQL局域网备份(包含全备份,日志增量备份)的内容,更多相关内容请关注PHP中文网(www.gxlcms.com)!