备份mysql脚本

    前几天为了给公司的mysql数据库做了个从机,从机直接从主机复制数据过来进行同步。为了能对主数据库数据进行备份,我写了个脚本,想法参考了网上的想法:

即每天0点与12点各做一次全备,每隔两个小时做一次增量备份。
脚本调了好些回,其实原理很简单,最后对备份文件进行隔天压缩来节省空间。里面用的是Innobackupex进行备份。
 
 
#!/bin/bash
# file: mysql_back.sh
# Author: jacky lau 2012-5-21
 
tim=`date +%H%M | cut -c 1,2`
data_path=/home/data/$(date -d "today" +"%m")/$(date -d "today" +"%d")
can=`find $data_path/ -cmin -125 -type d | awk -F/ '{print $6}' | grep -v full | uniq | grep -v "^$"`
inc=`find $data_path/ -cmin -125 -type d -name "*inc" | wc -l`
 
 
# Create directory
 
if [ ! -d $data_path ]
then
        mkdir -p $data_path
else
        echo "目录已经存在,退出。"
fi
 
# Full backup
 
if [ $tim -eq "00" ]
then
        echo -e " #################################################"
        echo -e " #    00:00:00 full backup of all databases     #"
        echo -e " #################################################"
 
        echo "00:00:00 开始全备份..."`date`
 
        /usr/bin/innobackupex --user=bakuser --password=1 --defaults-file=/etc/my.cnf --no-timestamp --socket=/usr/local/mysql/tmp/mysql.sock $data_path/full_1
 
        echo "00:00:00 全备完毕..."`date`
 
elif [ $tim -eq "12" ]
then
        echo -e " #################################################"
        echo -e " #     12:00:00 full backup of all databases    #"
        echo -e " #################################################"
 
        echo "12:00:00 开始全备份..."`date`
 
        /usr/bin/innobackupex --user=bakuser --password=1 --defaults-file=/etc/my.cnf --no-timestamp --socket=/usr/local/mysql/tmp/mysql.sock $data_path/full_2
 
        echo "12:00:00 全备完毕..."`date`
 
fi
 
if [ $tim -ge "00" -a $tim -lt "12" -a $inc -lt "1" ]
then
                echo "0点开始首次增量备份..."`date`
                echo "创建增量备份目录"
 
        /usr/bin/innobackupex --defaults-file=/etc/my.cnf --user=bakuser --password=1 --no-timestamp --socket=/usr/local/mysql/tmp/mysql.sock --incremental --incremental-basedir=$data_path/full_1 $data_path/$(date -d "today" +"%H")-inc
 
        echo "0点首次增量备份结束..."`date`
 
elif [ $tim -ge "12" -a $tim -lt "23" -a $inc -lt "1" ]
then
                echo "12点开始首次增量备份..."`date`
                echo "创建增量备份目录"
 
        /usr/bin/innobackupex --defaults-file=/etc/my.cnf --user=bakuser --password=1 --no-timestamp --socket=/usr/local/mysql/tmp/mysql.sock --incremental --incremental-basedir=$data_path/full_2 $data_path/$(date -d "today" +"%H")-inc
 
        echo "12点首次增量备份结束..."`date`
 
elif [ $tim -ge "1" ]
then
 
        echo -e "########################################"
        echo -e "#        incremental backup            #"
        echo -e "########################################"
        echo -e "开始增量备份..."`date`
 
        /usr/bin/innobackupex --defaults-file=/etc/my.cnf --user=bakuser --password=1 --no-timestamp --socket=/usr/local/mysql/tmp/mysql.sock --incremental --incremental-basedir=$data_path/${can} $data_path/$(date -d "today" +"%H")-inc
 
        echo "增量备份结束..."`date`
else
        echo "退出!"
fi
 
 
 
# compress the data file.
 
if [ $tim -eq "02" ]
then
        echo "开始压缩一天的备份..."
        cd $data_path/
                cd ..
 
        /bin/tar czvf $(date -d "yesterday" +"%Y")-$(date -d "yesterday" +"%m")-$(date -d "yesterday" +"%d")-$(date -d "yesterday" +"%M").tar.gz      $(date -d "yesterday" +"%d")/
 
        echo "压缩完毕!"
 
# Delete the file of one day
 
        echo "删除源文件..."
 
        /bin/rm $(date -d "yesterday" +"%d")/ -rf
 
        echo "删除完毕!"
 
fi

你可能感兴趣的:(mysql,mysql备份脚本,备份脚本)