Skip to content

Commit

Permalink
Update some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Mirocow committed Jul 7, 2013
1 parent 0147bdb commit 670e2ca
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 80 deletions.
45 changes: 26 additions & 19 deletions backup.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/bin/sh

# === CONFIG ===
BCKDIR='/var/backups/mysql'
Expand Down Expand Up @@ -59,37 +59,41 @@ backup()

query="SHOW databases;"
local skip=(
'information_schema'
'performance_schema'
'mysql'
'information_schema'
'performance_schema'
)
array_skip=( ${skip[@]} ${DATABASES_SKIP[@]} )
skip_reg=`array_join "${array_skip[@]}"`
f_log "Skip databases: $skip_reg"

for BDD in `mysql --defaults-extra-file=$MYCNF --skip-column-names -B -e "$query" | egrep -v "$skip_reg"`; do

f_log "* Processing BDD $BDD"
for BDD in $(mysql --defaults-extra-file=$MYCNF --skip-column-names -B -e "$query" | egrep -v "$skip_reg"); do

mkdir -p $DST/$BDD 2>/dev/null 1>&2
chown mysql:mysql $DST/$BDD

query="SHOW CREATE DATABASE \`$BDD\`;"
mysql --defaults-extra-file=$MYCNF --skip-column-names -B -e "$query" | awk -F"\t" '{ print $2 }' > $DST/$BDD/__create.sql
f_log " > Export create"
mysql --defaults-extra-file=$MYCNF --skip-column-names -B -e "$query" | \
awk -F"\t" '{ print $2 }' > $DST/$BDD/__create.sql
if [ -f $DST/$BDD/__create.sql ]; then
f_log " > Export create"
fi

query="SHOW FULL TABLES WHERE Table_type = 'VIEW';"
for viewName in `mysql --defaults-extra-file=$MYCNF $BDD -N -e "$query" | sed 's/|//' | awk '{print $1}'`
do
for viewName in $(mysql --defaults-extra-file=$MYCNF $BDD -N -e "$query" | sed 's/|//' | awk '{print $1}'); do
mysqldump --defaults-file=$MYCNF $BDD $viewName >> $DST/$BDD/__views.sql
done
f_log " > Exports views"
if [ -f $DST/$BDD/__views.sql ]; then
f_log " > Exports views"
fi

mysqldump --defaults-file=$MYCNF --routines --no-create-info --no-data --no-create-db --skip-opt $BDD | sed -e 's/DEFINER=[^*]*\*/\*/' > $DST/$BDD/__routines.sql
f_log " > Exports Routines"
mysqldump --defaults-file=$MYCNF --routines --no-create-info --no-data --no-create-db --skip-opt $BDD | \
sed -e 's/DEFINER=[^*]*\*/\*/' > $DST/$BDD/__routines.sql
if [ -f $DST/$BDD/__routines.sql ]; then
f_log " > Exports Routines"
fi

query="SHOW TABLES;"
for TABLE in `mysql --defaults-extra-file=$MYCNF --skip-column-names -B $BDD -e "$query" | grep -v slow_log | grep -v general_log`; do
for TABLE in $(mysql --defaults-extra-file=$MYCNF --skip-column-names -B $BDD -e "$query" | grep -v slow_log | grep -v general_log); do
f_log " ** Dump $BDD.$TABLE"

mysqldump --defaults-file=$MYCNF -T $DST/$BDD/ $BDD $TABLE
Expand All @@ -104,6 +108,9 @@ backup()

if [ -f "$DST/$BDD/$TABLE.txt" ]; then
f_log " ** bzip2 $BDD/$TABLE.txt in background"
if [ -f "$DST/$BDD/$TABLE.txt.bz2" ]; then
rm $DST/$BDD/$TABLE.txt.bz2
fi
bzip2 $DST/$BDD/$TABLE.txt &
else
f_log " ** WARNING : $DST/$BDD/$TABLE.txt not found"
Expand All @@ -116,15 +123,15 @@ backup()
f_log "** END **"
}

while getopts ":e:r:" opt;
while getopts ":e:r:s" opt;
do
case ${opt} in
e)
e)
exclude=${OPTARG}
IFS=, read -r -a DATABASES_SKIP <<< "$exclude"
;;
*)
usage
*)
usag
exit 1
;;
esac
Expand Down
154 changes: 93 additions & 61 deletions restore.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/bin/sh

# === CONFIG ===
DIR_PWD=$(pwd)
Expand All @@ -7,9 +7,19 @@ MYCNF='/etc/mysql/debian.cnf'
MYDATA='/var/lib/mysql'

# === FUNCTIONS ===
database_exists()
{
RESULT=`mysqlshow --defaults-extra-file=$MYCNF $@| grep -v Wildcard | grep -o $@`
if [ "$RESULT" == "$@" ]; then
echo YES
else
echo NO
fi
}

f_log()
{
logger "$RESTORE: $@"
logger "RESTORE: $@"
}

usage()
Expand All @@ -21,6 +31,7 @@ This script restore databases.
OPTIONS:
-e Exclude databases
-s Selected databases
-c Check innochecksum of table after import
EOF
}
Expand All @@ -44,14 +55,8 @@ restore()
exit 1
fi

f_log "Create databases"
for i in $(ls -1 $DIR_PWD/*/__create.sql); do
if [ -f "$i" ]; then
mysql --defaults-extra-file=$MYCNF < $i 2>/dev/null
fi
done

for i in `ls -1 -d $DIR_PWD/*/`; do
for i in $(ls -1 -d $DIR_PWD/*); do

BDD=$(basename $i)

for skip in "${DATABASES_SKIP[@]}"; do
Expand All @@ -62,88 +67,115 @@ restore()
fi
done

f_log "Import tables into $BDD"
for select in "${DATABASES_SELECTED[@]}"; do
if [ $BDD != $select ]; then
f_log "Skip database $BDD"
unset BDD
break
fi
done

if [ $BDD ]; then

if [ $BDD ]; then
for TABLE in `ls -1 $i | grep -v __ | awk -F. '{print $1}' | sort | uniq`; do
if [ -f $DIR_PWD/$BDD/__create.sql ]; then
f_log "Create database $BDD"
mysql --defaults-extra-file=$MYCNF < $DIR_PWD/$BDD/__create.sql 2>/dev/null
fi

f_log "Create table: $TABLE"
mysql --defaults-extra-file=$MYCNF $BDD -e "SET foreign_key_checks = 0;
DROP TABLE IF EXISTS $TABLE;
SOURCE $DIR_PWD/$BDD/$TABLE.sql;
SET foreign_key_checks = 1;"

if [ -f "$DIR_PWD/$BDD/$TABLE.txt.bz2" ]; then
f_log "< $TABLE"
if [ -f "$DIR_PWD/$BDD/$TABLE.txt" ]; then
rm $DIR_PWD/$BDD/$TABLE.txt
fi
bunzip2 $DIR_PWD/$BDD/$TABLE.txt.bz2
fi

if [ -f "$DIR_PWD/$BDD/$TABLE.txt" ]; then
f_log "+ $TABLE"
if [ $(database_exists $BDD) != "YES" ]; then
f_log "Error: Database $BDD dose not exists";
else

tables=$(ls -1 $i | grep -v __ | awk -F. '{print $1}' | sort | uniq)

f_log "Create tables in $BDD"
for TABLE in $tables; do
f_log "Create table: $BDD/$TABLE"
mysql --defaults-extra-file=$MYCNF $BDD -e "SET foreign_key_checks = 0;
LOAD DATA INFILE '$DIR_PWD/$BDD/$TABLE.txt'
INTO TABLE $TABLE;
DROP TABLE IF EXISTS $TABLE;
SOURCE $DIR_PWD/$BDD/$TABLE.sql;
SET foreign_key_checks = 1;"

if [ ! -f "$DIR_PWD/$BDD/$TABLE.txt.bz2" ]; then
f_log "> $TABLE"
bzip2 $DIR_PWD/$BDD/$TABLE.txt
done

f_log "Import data into $BDD"
for TABLE in $tables; do
f_log "Import data into $BDD/$TABLE"

if [ -f "$DIR_PWD/$BDD/$TABLE.txt.bz2" ]; then
f_log "< $TABLE"
if [ -f "$DIR_PWD/$BDD/$TABLE.txt" ]; then
rm $DIR_PWD/$BDD/$TABLE.txt
fi
bunzip2 $DIR_PWD/$BDD/$TABLE.txt.bz2
fi
fi

if [ $check ]; then
# Check INNODB table
if [ -f "$MYDATA/$BDD/$TABLE.ibd" ]; then
if [ ! `innochecksum $MYDATA/$BDD/$TABLE.ibd` ]; then
f_log "$TABLE [OK]"
else
f_log "$TABLE [ERR]"

if [ -f "$DIR_PWD/$BDD/$TABLE.txt" ]; then
f_log "+ $TABLE"
mysql --defaults-extra-file=$MYCNF $BDD -e "SET foreign_key_checks = 0;
LOAD DATA INFILE '$DIR_PWD/$BDD/$TABLE.txt'
INTO TABLE $TABLE;
SET foreign_key_checks = 1;"

if [ ! -f "$DIR_PWD/$BDD/$TABLE.txt.bz2" ]; then
f_log "> $TABLE"
bzip2 $DIR_PWD/$BDD/$TABLE.txt
fi
fi
fi

done

f_log "Import routines into $BDD"
if [ -f "$DIR_PWD/$BDD/__routines.sql" ]; then
mysql --defaults-extra-file=$MYCNF $BDD < $DIR_PWD/$BDD/__routines.sql 2>/dev/null

if [ $DATABASES_TABLE_CHECK ]; then
# Check INNODB table
if [ -f "$MYDATA/$BDD/$TABLE.ibd" ]; then
if [ ! $(innochecksum $MYDATA/$BDD/$TABLE.ibd) ]; then
f_log "$TABLE [OK]"
else
f_log "$TABLE [ERR]"
fi
fi
fi
done

if [ -f "$DIR_PWD/$BDD/__routines.sql" ]; then
f_log "Import routines into $BDD"
mysql --defaults-extra-file=$MYCNF $BDD < $DIR_PWD/$BDD/__routines.sql 2>/dev/null
fi

if [ -f "$DIR_PWD/$BDD/__views.sql" ]; then
f_log "Import views into $BDD"
mysql --defaults-extra-file=$MYCNF $BDD < $DIR_PWD/$BDD/__views.sql 2>/dev/null
fi

fi

f_log "Import views into $BDD"
if [ -f "$DIR_PWD/$BDD/__views.sql" ]; then
mysql --defaults-extra-file=$MYCNF $BDD < $DIR_PWD/$BDD/__views.sql 2>/dev/null
fi

fi
done

f_log "Flush privileges;"
mysql --defaults-extra-file=$MYCNF -e "flush privileges;"

f_log "** END **"
}

# === EXECUTE ===
while getopts ":e:c:" opt;
while getopts ":e:c:s:" opt;
do
case ${opt} in
e)
exclude=${OPTARG}
IFS=, read -r -a DATABASES_SKIP <<< "$exclude"
;;
s)
selected=${OPTARG}
IFS=, read -r -a DATABASES_SELECTED <<< "$selected"
;;
c)
check=1
DATABASES_TABLE_CHECK=1
;;
*)
usage
exit 1
;;
esac
done

shift "$((OPTIND - 1))"
#shift "$((OPTIND - 1))"

# === AUTORUN ===
restore

0 comments on commit 670e2ca

Please sign in to comment.