1.在 /etc/my.cnf 下面设置开启bin-log
编辑
vim /etc/my.cnf
[mysqld]
binlog_format = MIXED //binlog日志格式
log_bin =目录/mysql-bin.log //binlog日志名
expire_logs_days = 7 //binlog过期清理时间
#max_binlog_size 100m //binlog每个日志文件大小 最大值和默认是1个G
binlog-do-db=game #需要备份的数据库名,如果备份多个数据库,重复设置这个选项即可
binlog-do-db=platform #
#binlog-ignore-db=不需要备份的数据库,如果备份多个数据库,重复设置这个选项即可
2.创建远程登陆数据库帐号
grant all privileges on game.* to 'jack'@'192.168.10.56' identified by 'admin'
grant all privileges on platform.* to 'jack'@'192.168.10.56' identified by 'admin'
3.将服务器上的数据,导入一份进远程服务器中
mysqldump -ujack -padmin -h192.168.10.38 > 备份数据库地址(/home/mysql/db/mysql.sql);
4.写Linux 计划任务:
vim /etc/crontab
在底部加入
0 3 * * * root sh /home/wwwroot/sh/mysqlbackup.sh #每天3点以root帐号的权限执行目录下的sh
重启服务
/etc/init.d/crond restart
---------------------------------------------------
如果遇到了问题,请查看一下
select super_priv,user from user where host='192.168.10.39';
update user set super_priv='Y' where user='game1';
#! /bin/Bash
##################
# -- #
# author jackluo #
##################
db_user= #远程mysql用户名
db_pwd= #远程mysql密码
db_host= #远程ip
mysql_bin=/usr/local/mysql/bin/ #mysql 执行【招待】目录
mysqlbindir=/usr/local/mysql/var/ #binlog日志存放目录
dailyBackupDir=/root/mysql/ #存放,导入成功的日志
binlogname="mysql-bin" #binlog日志前缀
cd $mysqlbindir
fileList=`cat mysql-bin.index`
iCounter=
for file in $fileList
do
iCounter=`expr $iCounter + `
echo ${file}
done
nextNum=
for file in $fileList
do
binLogName=`basename $file `
nextNum=`expr $nextNum + `
if [[ $nextNum == $iCounter ]]; then
echo "Skip lastest!" > /dev/null
else
dest=$dailyBackupDir/$binLogName
if [[ -e $dest ]]; then #检查是否已导入过了
echo "Skip exist $binLogName!" > /dev/null
else
${mysql_bin}mysqlbinlog $binLogName|mysql -h${db_host} -u${db_user} -p${db_pwd} #写入远程数据库
if [[ ! -d ${dailyBackupDir} ]];then
mkdir -p ${dailyBackupDir}
fi
touch ${dest} #创建日志文件
find . -type f -ctime + -exec rm {} \; #删除6天后的日志文件
fi
fi
done