#!/bin/sh -ex
(
PATH=$PATH:/usr/local/mysql/bin:/opt/mysql/meb-3.5/bin/
BASE=$HOME/bug59623
mkdir -p $BASE/full $BASE/incr

# Create a test database
mysql -uroot <<EOF
drop database if exists test1;
create database test1;
use test1;

create table table1 (col1 int) engine=myisam;
insert into table1 values (1), (2), (3), (4);

create table table2 (col1 int) engine=innodb;
insert into table2 values (1), (2), (3), (4);
EOF


# Take a full backup
mysqlbackup --user=root --ibbackup=/opt/mysql/meb-3.5/bin/ibbackup \
	--compress /etc/my.cnf $BASE/full \
	2>&1 | tee full.txt 
FULLDIR=`awk '/^mysqlbackup: Created backup directory/ { print $5 }' < full.txt | sed "s/'//g"`
FULLLSN=`awk '/^ibbackup: Scanned log up to lsn / { print $7 }' < full.txt | sed 's/[^0-9]*//g'`
rm -f $BASE/full/latest
ln -s $FULLDIR $BASE/full/latest

# Drop our test database
mysql -uroot <<EOF
drop database if exists test1;
EOF

# Take an incremental backup
mysqlbackup --user=root --ibbackup=/opt/mysql/meb-3.5/bin/ibbackup \
	--incremental --lsn=$FULLLSN \
	--compress /etc/my.cnf $BASE/incr \
	2>&1 | tee incr.txt 
INCRDIR=`awk '/^mysqlbackup: Created backup directory/ { print $5 }' < incr.txt | sed "s/'//g"`
rm -f $BASE/incr/latest
ln -s $INCRDIR $BASE/incr/latest


# Apply the logs to the full backup
mysqlbackup --user=root --ibbackup=/opt/mysql/meb-3.5/bin/ibbackup \
	--apply-log --uncompress /etc/my.cnf $FULLDIR 

# Apply the logs to the incremental backup
#gdb --args \
mysqlbackup --user=root --ibbackup=/opt/mysql/meb-3.5/bin/ibbackup \
	--apply-log --uncompress --incremental $INCRDIR/backup-my.cnf $FULLDIR/backup-my.cnf


) 2>&1 | tee $0.log
