码迷,mamicode.com
首页 > 数据库 > 详细

mysql(mariadb)定时自动热备份+增量+远程备份脚本

时间:2016-05-03 16:23:10      阅读:1131      评论:0      收藏:0      [点我收藏+]

标签:mysql自动热备

优点:热备份,不影响业务,增量备份,远程备份。

目的:自动打包备份到远程10.0.0.111备份服务器


前提:

1.安装xtrabackup

yum install http://www.percona.com/downloads/percona-release/redhat/0.1-3/percona-release-0.1-3.noarch.rpm

yum install percona-xtrabackup-22


2.创建目录

mkdir -p /data/backup

mkdir -p /data/backuptar/


3.添加mysql公钥到备份服务器(10.0.0.111),能免密码远程登录。

4.创建备份用户

GRANT RELOAD,LOCK TABLES,REPLICATION CLIENT ON *.* TO ‘bkuser‘@‘localhost‘ IDENTIFIED BY ‘passwd‘;

FLUSH PRIVILEGES;


------------------------

#!/bin/bash

backdir=/data/backup

backupbin=/usr/bin

begintime=`date +"%Y-%m-%d %H:%M:%S"`

format_time=`date +"%Y-%m-%d_%H_%M_%S"` 

time_cost=$backdir/xtrabackup_time.txt

week=`date +%w`

user_name=bkuser

password="passwd"

file_cnf=/etc/my.cnf.d/wsrep.cnf

backtar=/data/backuptar/

remote_host="10.0.0.111"

remote_dir=/data/mysqlbak_remote_23

remote_user="wanzi"

out_log=$backdir/xtrabackup_log_$format_time


  

if [ -d "$backdir/rec5" ];then

        echo "开始打包5次的备份"

        cd $backdir

        tar -zcf lastweek.gz ./*

        mv lastweek.gz $backtar/$format_time.lastweek.gz

        echo "完成5次备份打包"

        cd $backtar

        rm -rf `ls |grep -v "$format_time.lastweek.gz"`

        echo "删除本地上次备份完成"


        echo "开始删除远程备份....."

        ssh $remote_user@$remote_host "cd $remote_dir;rm -rf ./*"

        echo "删除远程多余备份完成"

        echo "开始拷贝本地备份到远程服务器..."

        scp $backtar/$format_time.lastweek.gz $remote_host:/$remote_dir

        echo "远程备份完成"

        rm -rf $backdir

        mkdir $backdir

fi


#fullbackup   

if [ ! -d "$backdir/fullbackup" ];then

        echo "在$begintime开始全量备份" >>$time_cost

        $backupbin/innobackupex --defaults-file=$file_cnf --user=$user_name --password=$password --no-timestamp --slave-info  $backdir/fullbackup 1>$out_log 2>&1

        echo "完成全备"

elif [ ! -d "$backdir/rec0" ];then   

        echo "#####start 0 incremental backup at $BEGINTIME to directory rec0" >>$time_cost   

        $backupbin/innobackupex --defaults-file=$file_cnf  --no-timestamp --user=$user_name --password=$password --slave-info  --incremental --incremental-basedir=$bac

kdir/fullbackup $backdir/rec0 1> $out_log 2>&1

        echo "完成第0次增量备份"

        

elif [ ! -d "$backdir/rec1" ];then  

        echo "#####start 1 incremental backup at $BEGINTIME to directory rec1" >>$time_cost  

        $backupbin/innobackupex --defaults-file=$file_cnf  --no-timestamp --user=$user_name --password=$password --slave-info  --incremental --incremental-basedir=$bac

kdir/rec0 $backdir/rec1 1> $out_log 2>&1

        echo "完成第1次增量备份"

            

elif [ ! -d "$backdir/rec2" ];then  

        echo "#####start 2 incremental backup at $BEGINTIME to directory rec2" >>$time_cost  

        $backupbin/innobackupex --defaults-file=$file_cnf  --no-timestamp --user=$user_name --password=$password --slave-info  --incremental --incremental-basedir=$bac

kdir/rec1 $backdir/rec2 1> $out_log 2>&1  

        echo "完成第2次增量备份"

         

elif [ ! -d "$backdir/rec3" ];then  

        echo "#####start 3 incremental backup at $BEGINTIME to directory rec3" >>$time_cost  

        $backupbin/innobackupex --defaults-file=$file_cnf  --no-timestamp --user=$user_name --password=$password --slave-info  --incremental --incremental-basedir=$bac

kdir/rec2 $backdir/rec3 1> $out_log 2>&1 

        echo "完成第3次增量备份"

         

elif [ ! -d "$backdir/rec4" ];then  

        echo "#####start 4 incremental backup at $BEGINTIME to directory rec4" >>$time_cost  

        $backupbin/innobackupex --defaults-file=$file_cnf  --no-timestamp --user=$user_name --password=$password --slave-info  --incremental --incremental-basedir=$bac

kdir/rec3 $backdir/rec4 1> $out_log 2>&1

        echo "完成第4次增量备份"

         

elif [ ! -d "$backdir/rec5" ];then  

        echo "#####start 5 incremental backup at $BEGINTIME to directory rec5" >>$time_cost  

        $backupbin/innobackupex --defaults-file=$file_cnf  --no-timestamp --user=$user_name --password=$password --slave-info  --incremental --incremental-basedir=$bac

kdir/rec4 $backdir/rec5 1> $out_log 2>&1  

         echo "完成第5次增量备份"

 fi  

 ENDTIME=`date +"%Y-%m-%d %H:%M:%S"`  

 begin_data=`date -d  "$BEGINTIME" +%s`  

 end_data=`date -d  "$ENDTIME" +%s`  

 spendtime=`expr $end_data - $begin_data`  

 echo "it takes $spendtime sec for packing the data directory" >>$time_cost  


echo "备份结束......"`date`


本文出自 “jorden博客” 博客,请务必保留此出处http://jin544642965.blog.51cto.com/1389736/1769710

mysql(mariadb)定时自动热备份+增量+远程备份脚本

标签:mysql自动热备

原文地址:http://jin544642965.blog.51cto.com/1389736/1769710

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!