Skip to content

Instantly share code, notes, and snippets.

@shantanuo
Last active December 14, 2015 03:09
Show Gist options
  • Save shantanuo/5019161 to your computer and use it in GitHub Desktop.
Save shantanuo/5019161 to your computer and use it in GitHub Desktop.
extract data from binary and upload to remote server using curl. The text files used for "load data infile" command are tarred and sql commands are gunzipped.
#!/bin/sh
# call the script as shown below
# sh -xv extract.sh "db_name" "2013-02-23 00:00:00" "2013-02-23 01:59:59"
# or else it will select yesterday binary by default
mystart1=`date -d"1 days ago" +'%Y-%m-%d 00:00:01'`
mystop1=`date +'%Y-%m-%d 00:00:00'`
mystart=${2:-$mystart1}
mystop=${3:-$mystop1}
myfile=`date -d"$mystart" +'%b%d'`
userpass='-uapplication -plistetim'
dbname=`mysqlshow $userpass | egrep -v "(Databases|mysql|test|information_schema|performance_schema|lost)" | awk '{print $2}' | sed '/^$/d'`
mydb=${1:-$dbname}
rm -rf /increment/
mkdir -p /increment/
cd /increment/
time mysqlbinlog /var/log/mysql/mysql-bin.* --local-load=/increment/ --start-datetime="$mystart" --stop-datetime="$mystop" | gzip > /home/binary.$mydb.$myfile.sql.gz 2> /home/binary_extract.err
tar cvfz binary.$mydb.$myfile.txt.tar.gz *
time curl -F "file=@/increment/binary.$mydb.$myfile.txt.tar.gz" http://1.2.3.4/upload.php
time curl -F "file=@/home/binary.$mydb.$myfile.sql.gz" http://1.2.3.4/upload.php
@shantanuo
Copy link
Author

#!/bin/sh
# patch.sh script to upload partial binary to central server
# patch.sh "ETcountry19-Feb-2013-21-45.sql"

backup=${1:-"ETIMdb_name20-Feb-2013-20.sql"}
depotname=`echo "$backup" | sed 's/[0-9][0-9]//' | awk -F"-" '{print $1}' | sed 's/^ET//' `
startdate=`echo "$backup" | sed 's/[Aa-Zz]*//; s/-/ /3; s/-/:/3; s/.sql//' | xargs -0 date '+%F %T' -d`
enddate=`date -d"$startdate" '+%Y-%m-%d 23:59:59'`
depotip='1.2.3.4'

echo "ssh root@$depotip \"sh -xv /usr/backup/extract.sh '$depotname' '$startdate' '$enddate' \" "

@shantanuo
Copy link
Author

#!/bin/sh

backup="ETcountry16-Feb-2013-20-33.sql"
depotname=`echo "$backup" | sed 's/[0-9].*//' | sed 's/^ET//'`
startdate=`echo "$backup" | sed 's/[Aa-Zz]*//; s/-/ /3; s/-/:/3; s/.sql//' | xargs -0 date '+%F %T' -d`
echo $startdate

for i in `seq 20`
do

newdate=`mysql -Bse"select date_format(date(date_add('$startdate' , interval $i day)),'%b%d')"`

echo "tar xvf binary.$depotname.$newdate.txt.tar.gz -C /increment/"
echo "time gunzip -c binary.$depotname.$newdate.sql.gz | mysql $depot"
done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment