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

mysql 备份脚本(xtrabackup)

时间:2017-04-28 12:07:56      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:xtrabackup   mysql   incr   

mysql备份脚本,记录一下:

全备:

#!/bin/sh 
set -x                                                            
USER=root 
PASSWORD=123456
time=`date +"%y-%m-%d-%H-%M"`  
date >> /data/mysqlbackup/logs/$time‘_allbackup.log‘ 
 
#1 begin Full Backups 
echo "begin backup--------------------------------" >> /data/mysqlbackup/logs/$time‘_allbackup.log‘ 
   /usr/bin/innobackupex --defaults-file=/etc/my.cnf  --user=$USER --password=$PASSWORD --use-memory=2GB  --parallel=4  --no-timestamp /data/mysqlbackup/full-$time 2>>/data/mysqlbackup/logs/$time‘_allbackup.log‘ 
cp /etc/my.cnf /data/mysqlbackup/full-$time/my.cnf.bk
echo "end backup----------------------------------" >> /data/mysqlbackup/logs/$time‘_allbackup.log‘ 

#2 begin compress 
echo "Begin compress------------------------------" >> /data/mysqlbackup/logs/$time‘_allbackup.log‘ 
    archivename=full-$time
echo "The archive time is  $time" >> /data/mysqlbackup/logs/$time‘_allbackup.log‘
    tarname=$archivename.tar.gz 
echo "The tar name is $tarname" >> /data/mysqlbackup/logs/$time‘_allbackup.log‘
cd /data/mysqlbackup
/bin/tar -zcvf ./$tarname ./$archivename >> /data/mysqlbackup/logs/$time‘_allbackup.log‘ 2>&1
rm -rf /data/mysqlbackup/full/xtrabackup_checkpoints 
cp /data/mysqlbackup/full-$time/xtrabackup_checkpoints /data/mysqlbackup/full/xtrabackup_checkpoints

#3 Delete Document
file=/data/mysqlbackup/$tarname
if [[ -e $file ]]
    then
        echo "The $tarname file exists.begin to perform delete action"   >> /data/mysqlbackup/logs/$time‘_allbackup.log‘
        /bin/rm -rf /data/mysqlbackup/$archivename
    else
        echo "The file is not exists." >> /data/mysqlbackup/logs/$time‘_allbackup.log‘
    fi

date >> /data/mysqlbackup/logs/$time‘_allbackup.log‘

find /data/mysqlbackup/*tar.gz -ctime +1|xargs -I ‘{}‘  mv  {}  /data/ftp

cd /data/ftp

function logftp (){
ftp -v -n xxx.xxx.xxx.xxx << EOF
user username password
binary
hash
cd ftp_mysql
prompt
put /data/mysqlbackup/*tar.gz
ls -la
bye
EOF
}

logftp >>/data/mysqlbackup/logs/to_ftp.log

cd /data/ftp

find /data/ftp/*tar.gz |xargs rm -rf

增量备份:

#!/bin/sh
set -x
USER=root
PASSWORD=123456
time=`date +"%y-%m-%d-%H-%M"`
date >>/data/mysqlbackup/logs/$time‘_incre.log‘

#1 begin Incre Backups
echo "begin backup--------------------------------" >> /data/mysqlbackup/logs/$time‘_incre.log‘
   /usr/bin/innobackupex --defaults-file=/etc/my.cnf  --user=$USER --password=$PASSWORD  --incremental --incremental-basedir=/data/mysqlbackup/full --use-memory=2GB --safe-slave-backup --parallel=4 --slave-info   --no-timestamp /data/mysqlbackup/incre-$time  2>>/data/mysqlbackup/logs/$time‘_incre.log‘
cp /etc/my.cnf /data/mysqlbackup/incre-$time/my.cnf.bk
echo "end backup----------------------------------" >> /data/mysqlbackup/logs/$time‘_incre.log‘

#2 begin compress
echo "Begin compress------------------------------" >> /data/mysqlbackup/logs/$time‘_incre.log‘
    archivename=incre-$time
echo "The archive time is  $time" >> /data/mysqlbackup/logs/$time‘_incre.log‘
    tarname=$archivename.tar.gz
echo "The tar name is $tarname" >> /data/mysqlbackup/logs/$time‘_incre.log‘
cd /data/mysqlbackup/
/bin/tar -zcvf ./$tarname ./$archivename >> /data/mysqlbackup/logs/$time‘_incre.log‘ 2>&1

#3 Delete Document
file=/data/mysqlbackup/$tarname
if [[ -e $file ]]
    then
        echo "The $tarname file exists.begin to perform delete action"   >> /data/mysqlbackup/logs/$time‘_incre.log‘
        /bin/rm -rf /data/mysqlbackup/$archivename
    else
        echo "The file is not exists." >> /data/mysqlbackup/logs/$time‘_incre.log‘
    fi

date >> /data/mysqlbackup/logs/$time‘_incre.log‘

find /data/mysqlbackup/*tar.gz -ctime +1|xargs -I ‘{}‘  mv  {}  /data/ftp

cd /data/ftp

function logftp (){
ftp -v -n xxx.xxx.xxx.xxx << EOF
user username password
binary
hash
cd ftp_mysql
prompt
put /data/mysqlbackup/*tar.gz
ls -la
bye
EOF
}

logftp >>/data/mysqlbackup/logs/to_ftp.log

cd /data/ftp

find /data/ftp/*tar.gz |xargs rm -rf


本文出自 “刚刚出壳的小鸟” 博客,请务必保留此出处http://qhd2004.blog.51cto.com/629417/1920346

mysql 备份脚本(xtrabackup)

标签:xtrabackup   mysql   incr   

原文地址:http://qhd2004.blog.51cto.com/629417/1920346

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