部署xtrabckup备份_附带shell增量脚本

来源:互联网 发布:照片审核工具 mac 编辑:程序博客网 时间:2024/05/29 15:25
需要的安装包:
percona-xtrabackup-2.1.4-656.rhel5.x86_64.rpm
注意,该安装包不适合rhel4或CentOS4的操作系统,安装升级perl系统lib库会引起OS崩溃

操作系统环境介绍:
[root@im-web local]# lsb_release -aLSB Version:    :core-3.1-amd64:core-3.1-ia32:core-3.1-noarch:graphics-3.1-amd64:graphics-3.1-ia32:graphics-3.1-noarchDistributor ID: CentOSDescription:    CentOS release 5.2 (Final)Release:        5.2Codename:       Final[root@sf1 ~]# mv percona-xtrabackup-2.1.4-656.rhel5.x86_64.rpm  /usr/local/[root@sf1 ~]# cd /usr/local/安装[root@sf1 local]# rpm -ivh percona-xtrabackup-2.1.4-656.rhel5.x86_64.rpm warning: percona-xtrabackup-2.1.4-656.rhel5.x86_64.rpm: Header V4 DSA signature: NOKEY, key ID cd2efd2aPreparing...                ########################################### [100%]   1:percona-xtrabackup     ########################################### [100%]
[root@sf1 local]# inno
innobackupex        innobackupex-1.5.1  innochecksum        
表示安装正确

#cd /database/studyfun
# mkdir backup 
创建脚本
#touch xtrabackup.sh  
需要根据数据库具体配置修改如下
backdir=/database/xmobile/backupredun=/database/xmobile/redundencyfile_cnf=/etc/my_xmobile.cnfuser_name=backupuserpassword="123456"socket="/tmp/mysql_xmobile.sock"if [ ! -d "/database/xmobile/redundency" ]; thenmkdir -p /database/xmobile/redundencyfi[root@sf1 studyfun]#  lltotal 20drwxr-xr-x 2 root  root  4096 Oct  8 16:44 backupdrwxr-xr-x 2 mysql mysql 4096 Oct  8 15:53 binlogdrwxr-xr-x 5 mysql mysql 4096 Oct  8 15:53 datadrwxr-xr-x 2 mysql mysql 4096 Oct  8 15:53 relaylog-rwxr-xr-x 1 root  root  3528 Oct  8 16:45 xtrabackup.sh
给脚本赋执行权限
# chmod +x xtrabackup.sh 

创建备份用户
GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO 'backupuser'@'localhost' identified by '123456';
FLUSH PRIVILEGES;

加入crontab定时任务
# crontab -l
10 02 * * * /database/studyfun/xtrabackup.sh

xtrbackup.sh脚本

[root@sf1 data]# cat /database/studyfun/xtrabackup.sh#!/bin/bashBEGINTIME=`date +"%Y-%m-%d %H:%M:%S"`format_time=`date +"%Y-%m-%d_%H:%M:%S"`week=`date +%Y-%m-%d`backupbin=/usr/binbackdir=/database/studyfun/backupredun=/database/studyfun/backup/redundencyfile_cnf=/etc/my_studyfun.cnfuser_name=backupuserpassword="123456"socket="/tmp/mysql_studyfun.sock"out_log=$backdir/xtrabackup_log_$format_timetime_cost=$backdir/xtrabackup_time.txtif [ ! -d "/database/studyfun/backup/redundency" ]; then mkdir -p /database/studyfun/backup/redundencyfiif [ -d "$backdir/incr5" ];thentar -czvf ${redun}\/redundency_${week}.gz $backdir >/dev/null 2>&1        rm -rf $backdir        mkdir $backdir# del backupDEL_UNTIL_DATE=`date --date='14 day ago' +%Y-%m-%d`/bin/rm -f /${redun}/*${DEL_UNTIL_DATE}.gz >/dev/null 2>&1fi #fullif [ ! -d "$backdir/full" ];then        echo "#####start full backup at $BEGINTIME to directory full" >>$time_cost        $backupbin/innobackupex --defaults-file=$file_cnf --no-timestamp --user=$user_name --password=$password --socket=$socket --slave-info  $backdir/full 1> $out_log 2>&1        break;elif [ ! -d "$backdir/incr0" ];then        echo "#####start 0 incremental backup at $BEGINTIME to directory incr0" >>$time_cost        $backupbin/innobackupex --defaults-file=$file_cnf  --no-timestamp --user=$user_name --password=$password --socket=$socket --slave-info  --incremental --incremental-basedir=$backdir/full $backdir/incr0 1> $out_log 2>&1        break;elif [ ! -d "$backdir/incr1" ];then        echo "#####start 1 incremental backup at $BEGINTIME to directory incr1" >>$time_cost        $backupbin/innobackupex --defaults-file=$file_cnf  --no-timestamp --user=$user_name --password=$password --socket=$socket --slave-info  --incremental --incremental-basedir=$backdir/incr0 $backdir/incr1 1> $out_log 2>&1        break;elif [ ! -d "$backdir/incr2" ];then        echo "#####start 2 incremental backup at $BEGINTIME to directory incr2" >>$time_cost        $backupbin/innobackupex --defaults-file=$file_cnf  --no-timestamp --user=$user_name --password=$password --socket=$socket --slave-info  --incremental --incremental-basedir=$backdir/incr1 $backdir/incr2 1> $out_log 2>&1        break;elif [ ! -d "$backdir/incr3" ];then        echo "#####start 3 incremental backup at $BEGINTIME to directory incr3" >>$time_cost        $backupbin/innobackupex --defaults-file=$file_cnf  --no-timestamp --user=$user_name --password=$password --socket=$socket --slave-info  --incremental --incremental-basedir$backdir/incr2 $backdir/incr3 1> $out_log 2>&1        break;elif [ ! -d "$backdir/incr4" ];then        echo "#####start 4 incremental backup at $BEGINTIME to directory incr4" >>$time_cost        $backupbin/innobackupex --defaults-file=$file_cnf  --no-timestamp --user=$user_name --password=$password --socket=$socket --slave-info  --incremental --incremental-basedir=$backdir/incr3 $backdir/incr4 1> $out_log 2>&1        break;elif [ ! -d "$backdir/incr5" ];then        echo "#####start 5 incremental backup at $BEGINTIME to directory incr5" >>$time_cost        $backupbin/innobackupex --defaults-file=$file_cnf  --no-timestamp --user=$user_name --password=$password --socket=$socket --slave-info  --incremental --incremental-basedir=$backdir/incr4 $backdir/incr5 1> $out_log 2>&1        break; 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
------------遇到的问题----
报错1安装报错
[root@439607-db1-crov local]# rpm -ivh percona-xtrabackup-2.1.4-656.rhel5.x86_64.rpm 
warning: percona-xtrabackup-2.1.4-656.rhel5.x86_64.rpm: Header V4 DSA signature: NOKEY, key ID cd2efd2a
error: Failed dependencies:
        perl(DBD::mysql) is needed by percona-xtrabackup-2.1.4-656.rhel5.x86_64
出现上述报错,需要执行“yum install perl-DBD-MySQL”
 
报错2
日志报错Can't connect to local MySQL server through socket '/tmp/mysql.sock'
#cat  xtrabackup_log_2014-11-03_02:10:01InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oyand Percona Ireland Ltd 2009-2012.  All Rights Reserved.This software is published underthe GNU GENERAL PUBLIC LICENSE Version 2, June 1991.141103 02:10:02  innobackupex: Connecting to MySQL server with DSN 'dbi:mysql:;mysql_read_default_file=/etc/my_umc.cnf;mysql_read_default_group=xtrabackup' as 'backupuser'  (using password: YES).ERROR: Failed to connect to MySQL server: DBI connect(';mysql_read_default_file=/etc/my_umc.cnf;mysql_read_default_group=xtrabackup','backupuser',...) failed: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) at /usr/bin/innobackupex line 1384
解决:
备份脚本xtrabackup.sh添加socket="/tmp/mysql_umc.sock",如果还报错,是因为脚本拷贝过程中产生了问题,从正确部署的机器上拷贝即可

报错3, 执行备份脚本报错line 35: break: only meaningful in a `for', `while', or `until' loop

原因:脚本错误,请重新粘贴上述提供的脚本,并注意排版,避免乱行



  
0 0
原创粉丝点击