嵌入式下MySQL增量备份解决方案

由于以前的环境是在嵌入式系统下,对flash的读写有一定限制,所以对mysql的记录必须使用tmpfs。

这是工作中拼凑出来的一个脚本,记录了辛酸的学习之路,记录一下
引用:
#mount.sh

#!/bin/bash

set -e
dataDir=/opt/lampp/var/mysql
backupDir=/opt/lampp/var/mysqldata
plusDir=/opt/lampp/backup/mysql/daily
SqlUser=root
SqlPass=kai089hong

if [ ! -e $plusDir ]
then
    mkdir -p $plusDir
fi

test -f /etc/init.d/nasa_switch || exit 0
case "$1" in

start)

        echo -n "Starting NASA Server ......"
        if [ -d $dataDir ]
    then
        mount -t tmpfs -o size=32m tmpfs $dataDir
    else
        mkdir -p $dataDir
        mount -t tmpfs -o size=32m tmpfs $dataDir
    fi
    echo -e "\nMount tmpfs successfully!\n"
    cp -R $backupDir/mysql $dataDir/
    chmod -R 777 $dataDir
    chown -R nobody $dataDir
    sleep 4
    /opt/lampp/lampp start >/dev/null 2>&1 && echo -e "\nStarted XAMPP successfully!\n"
    sleep 4
    /opt/lampp/bin/mysqladmin -u$SqlUser -p$SqlPass CREATE nasa > /dev/null
    SqlFile=/opt/lampp/backup/mysql
    SqlName=`cat $SqlFile/lastone`
    /opt/lampp/bin/mysql -u$SqlUser -p$SqlPass nasa < $SqlFile/$SqlName
    /opt/lampp/bin/mysqlbinlog $plusDir/mysql-bin.*|mysql -uroot -pkai089hong nasa > /dev/null 2>&1
    sleep 3
    /opt/lampp/bin/backup.sh
    echo -e "\nRecover data successfully!\n"
        sleep 10
        ;;

stop)

        echo -e "Stopping NASA Server .......\n"
    /opt/lampp/bin/add.sh
    sleep 5
    /opt/lampp/lampp stop >/dev/null 2>&1
    umount $dataDir >/dev/null 2>&1
    #mv $backupDir $dataDir
    #/opt/lampp/lampp restart >/dev/null 2>&1
    #/opt/lampp/bin/mysqlbinlog $plusDir/mysql-bin.*|mysql -uroot -pkai089hong nasa
    #mv $dataDir $backupDir   
        sleep 10
        echo -e "OK\n"

        ;;

*)

        echo "Usage: /etc/init.d/nasa_switch {start|stop}"

        exit 1

esac

exit 0

bakup.sh
#!/bin/bash

BakDir=/opt/lampp/backup/mysql
LogFile=/opt/lampp/backup/mysql/mysqlbak.log
CatFile=/opt/lampp/backup/mysql/lastone
DailyBakDir=$BakDir/daily
DATE=`date +%y%m%d%H%M%S`
SqlUser=root
SqlPass=kai089hong


if [ ! -e $DailyBakDir ]
then
    mkdir -p $DailyBakDir
fi

echo " " >> $LogFile
echo " " >> $LogFile
echo "-------------------------------------------" >> $LogFile
echo $(date +"%y-%m-%d %H:%M:%S") >> $LogFile
echo "--------------------------" >> $LogFile


cd $BakDir
DumpFile=$DATE
echo $DumpFile>$CatFile
/opt/lampp/bin/mysqldump --flush-logs --delete-master-logs --lock-all-tables nasa -u$SqlUser -p$SqlPass>$DumpFile
echo "Dump Done" >> $LogFile

echo "[$DumpFile]Backup Success!" >> $LogFile

cd $DailyBakDir
rm -f *

cd $BakDir
echo "Backup Done!"
echo "please Check $BakDir Directory!"
echo "copy it to your local disk or ftp to somewhere !!!"

ls -al $BakDir

#add.sh

#!/bin/bash
#
# mysql binlog backup script
#


DATADIR=/opt/lampp/var/mysql
BAKDIR=/opt/lampp/backup/mysql/daily
SqlUser=root
SqlPass=kai089hong
/opt/lampp/bin/mysqladmin flush-logs -u$SqlUser -p$SqlPass

if [ ! -e "$BAKDIR" ]
then
    mkdir $BAKDIR
fi

cd $DATADIR

FILELIST=`cat mysql-bin.index`

COUNTER=0
for file in $FILELIST
do
    COUNTER=`expr $COUNTER + 1`
done

NextNum=0
for file in   $FILELIST
do
    base=`basename $file`
    NextNum=`expr $NextNum + 1`
    if [ $NextNum -eq $COUNTER ]
    then
        echo "skip lastest"
    else
        dest=$BAKDIR/$base
        if [ -e $dest ]
        then
            echo "skip exist $base"
        else
            echo "copying $base"
            cp $base $BAKDIR
        fi
    fi
done
#cp $DATADIR/mysql-bin.index $BAKDIR
echo "backup mysql binlog ok"